需要在android中使用HttpPost上传文件

时间:2012-07-21 07:40:33

标签: android

I would like to upload a name.txt file without ussing MultiEntitiy "importing a large file" as my file is small in size... I have written the following code but unable to upload a file.. thanks for your concern...

URL url = new URL(strUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoInput(true); // Allow Inputs
    conn.setDoOutput(true); // Allow Outputs
    conn.setUseCaches(false); // Don't use a Cached Copy
    conn.setRequestMethod("POST");
    conn.setRequestProperty("userfile", strEmailAddress + ".txt");
    dos = new DataOutputStream(conn.getOutputStream());
    dos.writeBytes("Content-Disposition: form-data; name=\"paramName\"");
    Helper.printLogD(" connection " + conn.getContent().toString());

String temp = getStringFromInputStream(conn.getInputStream());
Helper.printLogI("temp" + temp);

1 个答案:

答案 0 :(得分:1)

我使用以下代码:

public static JSONObject uploadImageToServer(String url, String path,
        String usuario) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

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

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("userfile", path));
        nameValuePairs.add(new BasicNameValuePair("tmp_name", usuario));

        for (int index = 0; index < nameValuePairs.size(); index++) {
            if (nameValuePairs.get(index).getName()
                    .equalsIgnoreCase("userfile")) {
                // 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);

        HttpEntity responseEntity = response.getEntity();

        if (responseEntity != null) {
            InputStream instream = responseEntity.getContent();
            String resultString = convertStreamToString(instream);
            instream.close();
            // Transform the String into a JSONObject
            JSONObject jsonObjRecv = null;
            try {
                jsonObjRecv = new JSONObject(resultString);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jsonObjRecv;
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

您还需要库 httpmime 才能使其正常运行。

希望能提供帮助:)