cxdxiaodou 2015-01-15 13:22 采纳率: 0%
浏览 1777

Android:文件上传只能上传一张,后面的都是不可以

上传的第一张图片是可以的,但是后面的都是不可以,后台返回的都是第一张的信息;分一张张传也不行;只有退出当前activity之后再进去传才能传新的;这是怎么回事???

 public String uploadFile(Map<String, File> files)  {    
        String data = ""; 
        String actionUrl = URL+"file/uploadClientPic";  
//      Map<String, String> params = new HashMap<String, String>();        
//        params.put("t", ""+System.currentTimeMillis()); 

        String BOUNDARY = java.util.UUID.randomUUID().toString();  
        String PREFIX = "--", LINEND = "\r\n";  
        String MULTIPART_FROM_DATA = "multipart/form-data";  
        String CHARSET = "UTF-8";  
        URL uri;
try {
uri = new URL(actionUrl);
 HttpURLConnection conn = (HttpURLConnection) uri.openConnection();  
        conn.setReadTimeout(10 * 1000);  
        conn.setDoInput(true);// 允许输入  
        conn.setDoOutput(true);// 允许输出  
        conn.setUseCaches(true);  
        conn.setRequestMethod("POST"); // Post方式  
        conn.setRequestProperty("connection", "keep-alive");  
        conn.setRequestProperty("Charsert", "UTF-8");  
        conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA  
                + ";boundary=" + BOUNDARY);  

        // 首先组拼文本类型的参数  
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "******";
//          StringBuilder sb = new StringBuilder();  
//          for (Map.Entry<String, String> entry : params.entrySet()) {  
//              sb.append(PREFIX);  
//              sb.append(BOUNDARY);  
//              sb.append(LINEND);  
//              sb.append("Content-Disposition: form-data; name=\""  
//                      + entry.getKey() + "\"" + LINEND);  
//              sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);  
//              sb.append("Content-Transfer-Encoding: 8bit" + LINEND);  
//              sb.append(LINEND);  
//              sb.append(entry.getValue());  
//              sb.append(LINEND);  
//          }  

        DataOutputStream outStream = new DataOutputStream(conn  
                .getOutputStream());  
//          outStream.write(sb.toString().getBytes());  
        outStream.writeBytes(twoHyphens + boundary + end);
        // 发送文件数据  
        if (files != null)  
            for (Map.Entry<String, File> file : files.entrySet()) {  
                StringBuilder sb1 = new StringBuilder();  
                sb1.append(PREFIX);  
                sb1.append(BOUNDARY);  
                sb1.append(LINEND);  
                sb1  
                        .append("Content-Disposition: form-data; name=\"file\"; filename=\""  
                                + file.getKey() + "\"" + LINEND);  
                sb1.append("Content-Type: application/octet-stream; charset="  
                        + CHARSET + LINEND);  
                sb1.append(LINEND);  
                outStream.write(sb1.toString().getBytes());  
                InputStream is = new FileInputStream(file.getValue());  
                byte[] buffer = new byte[1024];  
                int len = 0;  
                while ((len = is.read(buffer)) != -1) {  
                    outStream.write(buffer, 0, len);  
                }  

                is.close();  
                outStream.write(LINEND.getBytes());  
            }  

        // 请求结束标志  
        byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();  
        outStream.write(end_data);  
        outStream.flush();  

        // 得到响应码  
        int res = conn.getResponseCode();  
        InputStream in = conn.getInputStream();  
        InputStreamReader isReader = new InputStreamReader(in);  
        BufferedReader bufReader = new BufferedReader(isReader);  
        String line = null;  


        while ((line = bufReader.readLine()) != null)  
        {
            data += line;
        }  

        if (res == 200) {  
            int ch;  
            StringBuilder sb2 = new StringBuilder();  
            while ((ch = in.read()) != -1) {  
                sb2.append((char) ch);  
            }  
        }  
        outStream.close();  
        conn.disconnect();  

} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}  

        return data;
    }    
  • 写回答

1条回答 默认 最新

  • 91program 博客专家认证 2015-01-15 23:52
    关注

    首先,终端判断每次传输的是不是同一个文件,且将每个文件的第一包数据通过 LOG 输入。这个输出,最好是在即将要发送数据时。
    其次,也有可能是后台处理出错了,不是终端的问题。所以需要一起分析。

    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?