Post multipart request with Android SDK

前端 未结 12 1991
余生分开走
余生分开走 2020-11-22 04:38

I\'m trying to do something I thought would be relatively simple: Upload an image to a server with the Android SDK. I\'m found a lot of example code:

http://groups.g

12条回答
  •  梦如初夏
    2020-11-22 05:26

    public class Multipart{
        private final Map headrs;
        private String url;
        private HttpURLConnection con;
        private OutputStream os;
    
        private String delimiter = "--";
        private String boundary = "TRR" + Long.toString(System.currentTimeMillis()) + "TRR";
    
        public Multipart (String url, Map headers) {
            this.url = url;
            this.headrs = headers;
        }
    
        public void connectForMultipart() throws Exception {
            con = (HttpURLConnection) (new URL(url)).openConnection();
            con.setRequestMethod("POST");
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestProperty("Connection", "Keep-Alive");
            for (Map.Entry entry : headrs.entrySet()) {
                con.setRequestProperty(entry.getKey(), entry.getValue());
            }
            con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
            con.connect();
            os = con.getOutputStream();
        }
    
        public void addFormPart(String paramName, String value) throws Exception {
            writeParamData(paramName, value);
        }
    
        public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
            os.write((delimiter + boundary + "\r\n").getBytes());
            os.write(("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n").getBytes());
            os.write(("Content-Type: application/octet-stream\r\n").getBytes());
            os.write(("Content-Transfer-Encoding: binary\r\n").getBytes());
            os.write("\r\n".getBytes());
    
            os.write(data);
    
            os.write("\r\n".getBytes());
        }
    
        public void finishMultipart() throws Exception {
            os.write((delimiter + boundary + delimiter + "\r\n").getBytes());
        }
    
    
        public String getResponse() throws Exception {
            InputStream is = con.getInputStream();
            byte[] b1 = new byte[1024];
            StringBuffer buffer = new StringBuffer();
    
            while (is.read(b1) != -1)
                buffer.append(new String(b1));
    
            con.disconnect();
    
            return buffer.toString();
        }
    
    
        private void writeParamData(String paramName, String value) throws Exception {
    
    
            os.write((delimiter + boundary + "\r\n").getBytes());
            os.write("Content-Type: text/plain\r\n".getBytes());//;charset=utf-8
            os.write(("Content-Disposition: form-data; name=\"" + paramName + "\"\r\n").getBytes());
            ;
            os.write(("\r\n" + value + "\r\n").getBytes());
    
    
        }
    }
    

    Then call below

    Multipart multipart = new Multipart(url__, map);
                multipart .connectForMultipart();
    multipart .addFormPart(entry.getKey(), entry.getValue());
    multipart .addFilePart(KeyName, "FileName", imagedata);
    multipart .finishMultipart();
    

提交回复
热议问题