在android中使用http put -T上传文件?

时间:2011-05-24 06:44:04

标签: java android web-services rest file-upload


   我有一个使用http put来调用REST Web服务的android类。但它创建空文件夹没有内容我该怎么办? 当我使用curl命令测试Web服务时,它可以正常上传。

curl -v -T C:\ downloads \ a.jpg -H“Content-Type:application / vnd.org.snia.cdmi.dataobject”http://192.168.37.241:8080/kyaw/ktest/a.jpg

public class 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;
        }

0 个答案:

没有答案