如何在单个帖子中发送文件和数据?

时间:2014-10-12 03:25:08

标签: java android http multipartform-data

我正在使用来自客户端的http-mime库来发送POST到服务器,并使用multipart post发送文件和数据

但是我不确定如何在单个POST请求中使用File发送数据(params)

我将此作为参考:http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

1 个答案:

答案 0 :(得分:1)

我使用mime4j库执行了以下操作:

//Imports
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.james.mime4j.stream.NameValuePair;

//Adding data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new NameValuePair("caption", sCaption));// String
    nameValuePairs.add(new NameValuePair("email", device_id));// String
    nameValuePairs.add(new NameValuePair("uploadedfile", path));//File path
    post(URLs.photoUpload, nameValuePairs);// Calling post function

//post function
    public void post(final String url, final List<NameValuePair> nameValuePairs) {
        // Setting progressDialog properties
        final ProgressDialog progressDialog = ProgressDialog.show(
                PhotoActivity.this, "", "Posting To Server...");

        final Handler mHandler = new Handler();
        // Function to run after thread
        final Runnable mUpdateResults = new Runnable() {
            public void run() {

                if (server) {
                    server = false;
                    Toast.makeText(getBaseContext(), "Posted To Server!",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), "Error!",
                            Toast.LENGTH_SHORT).show();
                }
            }
        };
        new Thread() {
            @Override
            public void run() {

                System.out.println("URL:::" + url);
                System.out.println("SIZE:::" + nameValuePairs.size());
                // ///////////////
                HttpClient httpClient = new DefaultHttpClient();
                HttpContext localContext = new BasicHttpContext();
                HttpPost httpPost = new HttpPost(url);

                try {
                    MultipartEntity entity = new MultipartEntity(
                            HttpMultipartMode.BROWSER_COMPATIBLE);

                    for (int index = 0; index < nameValuePairs.size(); index++) {
                        if (nameValuePairs.get(index).getName()
                                .equalsIgnoreCase("uploadedfile")) {
                            // If the key equals to "image", we use FileBody
                            // to transfer the data
                            entity.addPart(
                                    nameValuePairs.get(index).getName(),
                                    new FileBody(new File(nameValuePairs.get(
                                            index).getValue())));
                        } else {
                            // Normal string data
                            entity.addPart(nameValuePairs.get(index).getName(),
                                    new StringBody(nameValuePairs.get(index)
                                            .getValue()));
                        }
                    }

                    httpPost.setEntity(entity);

                    HttpResponse response = httpClient.execute(httpPost,
                            localContext);
                    System.out.println(response.toString());
                    server = true;
                } catch (IOException e) {
                    e.printStackTrace();
                    server = false;
                }

                // dismiss the progress dialog
                progressDialog.dismiss();
                // Calling handler's post function
                mHandler.post(mUpdateResults);

            }
        }.start();

    }