如何解决在Android中使用http put -T上传文件?
| 我有一个使用http来调用REST Web服务的android类。但是它创建了空文件夹,没有内容,我该怎么办? 当我使用curl命令测试Web服务时,它可以正常工作并且可以成功上传。 curl -v -TC:\\ downloads \\ a.jpg -H \“ Content-Type:application / vnd.org.snia.cdmi.dataobject \” http://192.168.37.241:8080/kyaw/ktest/a .jpg 公共类putandroid扩展Activity { public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(getInputStreamFromUrl(\"http://192.168.37.241:8080/kyaw/ktest/a.jpg\"));
}
public static String getInputStreamFromUrl(String url) {
String content = null;
HttpResponse response = null;
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPut put=new HttpPut(url);
File f=new File(\"C:\\\\downloads\\\\a.jpg\");
FileEntity reqEntity=new FileEntity(f,\"images/jpg\");
put.setEntity(reqEntity);
put.setHeader(\"Content-Type\",\"application/vnd.org.snia.cdmi.dataobject\");
response = httpclient.execute(put);
HttpEntity resEntity=response.getEntity();
if(resEntity!=null)
content = (EntityUtils.toString(resEntity));
}catch (Exception e) {
Log.e(\"PUT Exception\",\"java.lang.NullPointerException\");
}
String result=response.getStatusLine().toString()+\"\\n\"+content;
return result;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。