android multipart通过ArrayList <img/>发送到服务器图像

时间:2016-01-01 12:04:56

标签: android image multipart

我想通过byte []将多个图像发送到服务器。但只有一个图像到达服务器。

这是我的代码,请让我知道我做错了什么。

protected String doInBackground(String... url) {

    String result = null;
    URL httpurl;
    HttpURLConnection conn;
    DataOutputStream dos;

    try {

        httpurl = new URL(url[0]);
        conn = (HttpURLConnection) httpurl.openConnection();
        conn.setDefaultUseCaches(false);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-type", "multipart/form-data;boundary="+ HTTP_FILE_BOUNDARY);

        dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes("--" + HTTP_FILE_BOUNDARY + "\r\n");

        dos.writeBytes("Content-Disposition: form-data; name=\"key\""  + "\r\n\r\n"); 
        dos.writeBytes(params.key);
        dos.writeBytes("\r\n");
        dos.writeBytes("--" + HTTP_FILE_BOUNDARY + "\r\n");

        dos.writeBytes("Content-Disposition: form-data; name=\"memo\""  + "\r\n\r\n"); 
        dos.write(params.memo.getBytes("UTF-8"));
        dos.writeBytes("\r\n");
        dos.writeBytes("--" + HTTP_FILE_BOUNDARY + "\r\n");

        dos.writeBytes("Content-Disposition: form-data; name=\"list_menu\""  + "\r\n\r\n"); 
        dos.write(params.list_menu.getBytes("UTF-8"));
        dos.writeBytes("\r\n");
        dos.writeBytes("--" + HTTP_FILE_BOUNDARY + "\r\n");

        dos.writeBytes("Content-Disposition: form-data; name=\"lat\""  + "\r\n\r\n"); 
        dos.write(params.lat.getBytes("UTF-8"));
        dos.writeBytes("\r\n");
        dos.writeBytes("--" + HTTP_FILE_BOUNDARY + "\r\n");

        dos.writeBytes("Content-Disposition: form-data; name=\"lon\""  + "\r\n\r\n"); 
        dos.write(params.lon.getBytes("UTF-8"));
        dos.writeBytes("\r\n");
        dos.writeBytes("--" + HTTP_FILE_BOUNDARY + "\r\n");

        dos.writeBytes("Content-Disposition: form-data; name=\"update\""  + "\r\n\r\n"); 
        dos.writeBytes(params.update);
        dos.writeBytes("\r\n");
        dos.writeBytes("--" + HTTP_FILE_BOUNDARY + "\r\n");

        for(int i=0; i<params.photolist.size(); i++){
            dos.writeBytes("Content-Disposition: form-data; name=\"photo\""+ i +;" filename=\"" + params.fileName + "\"" + "\r\n");
            dos.writeBytes("\"" + "\r\n");
            dos.writeBytes("Content-Type: image/jpeg" + "\r\n\r\n");
            if ( params.photolist.get(i) != null ) {
                dos.write(params.photolist.get(i));
            }
            dos.writeBytes("\r\n");

            dos.writeBytes("--" + HTTP_FILE_BOUNDARY+ "--" + "\r\n"); 
        }

        dos.flush();

        InputStream is = conn.getInputStream();
        String line = null;
        sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } 

    if(sb != null){
        return sb.toString();
    }
    return result;
}

0 个答案:

没有答案