需要上传使用Android的文件HttpPost [英] Need to upload a file using HttpPost in android

查看:109
本文介绍了需要上传使用Android的文件HttpPost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我想上传一个name.txt文件,而无需尤斯MultiEntitiy导入大文件作为我的文件体积小......我写了下面的code但无法上传文件。对于您的关注谢谢...

网址URL =新的URL(strUrl);
    HttpURLConnection的康恩=(HttpURLConnection类)url.openConnection();
    conn.setDoInput(真正的); //允许输入
    conn.setDoOutput(真正的); //允许输出
    conn.setUseCaches(假); //不要使用缓存副本
    conn.setRequestMethod(POST);
    conn.setRequestProperty(userfile的,strEmailAddress +名.txt);
    DOS =新DataOutputStream类(conn.getOutputStream());
    dos.writeBytes(内容处置:表格数据;名称= \paramName配置\);
    Helper.printLogD(连接+ conn.getContent()的toString());

字符串临时= getStringFromInputStream(conn.getInputStream());
Helper.printLogI(TEMP+温度);
 

解决方案

我用下面的code:

 公共静态的JSONObject uploadImageToServer(URL字符串,字符串路径,
        字符串usuario){
    HttpClient的HttpClient的=新DefaultHttpClient();
    HttpContext的localContext =新BasicHttpContext();
    HttpPost httpPost =新HttpPost(URL);

    尝试 {
        MultipartEntity实体=新MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        名单<的NameValuePair> namevaluepairs中=新的ArrayList<的NameValuePair>(2);
        nameValuePairs.add(新BasicNameValuePair(userfile的,路径));
        nameValuePairs.add(新BasicNameValuePair(tmp_name的值,usuario));

        对于(INT指数= 0;指数< nameValuePairs.size();指数++){
            如果(nameValuePairs.get(指数).getName()
                    .equalsIgnoreCase(userfile的)){
                //如果密钥等于形象,我们用FileBody转移
                // 数据
                entity.addPart(nameValuePairs.get(指数).getName()
                        新FileBody(新文件(nameValuePairs.get(指数)
                                .getValue())));
            } 其他 {
                //普通字符串数据
                entity.addPart(
                        nameValuePairs.get(指数).getName()
                        新StringBody(nameValuePairs.get(指数).getValue()));
            }
        }

        httpPost.setEntity(实体);

        HTT presponse响应= httpClient.execute(httpPost,localContext);

        HttpEntity responseEntity = response.getEntity();

        如果(responseEntity!= NULL){
            InputStream的河道= responseEntity.getContent();
            字符串resultString = convertStreamToString(河道);
            instream.close();
            //转换字符串到JSONObject的
            JSONObject的jsonObjRecv = NULL;
            尝试 {
                jsonObjRecv =新的JSONObject(resultString);
            }赶上(JSONException E){
                e.printStackTrace();
            }
            返回jsonObjRecv;
        }

    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(ClientProtocolException E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    返回null;
}
 

您还需要在图书馆的 httpmime ,以使其发挥作用。

希望能帮助:)

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);

解决方案

I use the following code:

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

You need also the library httpmime to make it work.

Hope to help :)

这篇关于需要上传使用Android的文件HttpPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆