qiucuiling 2015-08-20 07:47 采纳率: 0%
浏览 2198
已结题

Android httppost 上传文件 中文乱码

利用CustomMultipartEntity附加的内容;
按照网上的指示,为httppost设置编码,new FileBody时转码,统统不好使!
真心求助!!
核心代码如下:
@Override
protected String doInBackground(String... params) {
String serverResponse = null;

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext httpContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
        ProgressListener listenner = new ProgressListener() {
            @Override
            public void transferred(long num) {
                publishProgress((int) ((num / (float) totalSize) * 100));
            }
        };
        CustomMultipartEntity multipartContent = new CustomMultipartEntity(listenner);

        // We use FileBody to transfer an image

         if (paraMap != null && !paraMap.isEmpty()) {
             for (Map.Entry<String, String> entry : paraMap.entrySet()) {
                 multipartContent.addPart(entry.getKey(), new StringBody(entry.getValue()));
             }
         }
         multipartContent.addPart("data", new FileBody(file,HTTP.UTF_8));
        totalSize = multipartContent.getContentLength();

        // Send it
        httpPost.setEntity(multipartContent);
        HttpResponse response = httpClient.execute(httpPost);
        serverResponse = EntityUtils.toString(response.getEntity());
        JSONObject jsons = new JSONObject(serverResponse);
         zid = jsons.optString("zid");
         filepath = jsons.optString("filepath");
         guid = jsons.optString("guid");
         //file.delete();    
    } catch (Exception e) {
        e.printStackTrace();
    }

    return serverResponse;
}
  • 写回答

4条回答 默认 最新

  • devmiao 2015-08-20 07:51
    关注

    multipartContent.addPart(entry.getKey(), new StringBody(entry.getValue()));
    这里不对,应该按照二进制上传

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题