使用apache httpclient上传文件

时间:2015-07-16 14:46:52

标签: java android django

我正在尝试在android中编写一个程序,将文件上传到服务器。我想使用apache httpclient。

这是我的代码:

String url = "http://192.168.1.103:8080";
    File file = new File("/sdcard/alireza/ali.txt");
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
        reqEntity.setContentType("binary/octet-stream");
        reqEntity.setChunked(true); // Send in multiple parts if needed
        httppost.setEntity(reqEntity);
        Toast.makeText(getApplicationContext(),"executing...",Toast.LENGTH_LONG).show();
        HttpResponse response = httpclient.execute(httppost);
        Toast.makeText(getApplicationContext(),"done",Toast.LENGTH_LONG).show();
        //Do something with response...
    }catch (Exception e){

    }

完成吐司并没有出现。

0 个答案:

没有答案