使用httpurlconnection从android上传zip文件

时间:2016-03-16 07:07:37

标签: android file-upload asp.net-web-api2

获取" MIME多部分流的意外结束。 MIME多部分邮件不完整"使用httpurlconnection

从客户端上传zip文件时服务器端的异常

以下是上传代码

            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setRequestProperty("Content-Type", "multipart/form-   data; boundary=" + boundary);
            connection.setRequestProperty("Accept-Encoding","gzip, deflate");
            connection.setRequestProperty("Accept","*/*");
            connection.setRequestProperty("Accept-Language","en-GB,en-US; q=0.8,en; q=0.6");
            outputStream = new DataOutputStream(connection.getOutputStream());


            outputStream.writeBytes(boundary + LINE_FEED);
            String [] q = fileName.split("/");
            int ind = q.length-1;
            outputStream.writeBytes("Content-Disposition: form-data; name=\"logs\"; filename=\"" +q[ind] + "\""+ LINE_FEED);
            outputStream.writeBytes("Content-Type: application/octet-stream "+LINE_FEED);

            outputStream.writeBytes(LINE_FEED);

            FileInputStream is = new FileInputStream(uploadFile);
            byte[] buffer = new byte[4096];
            int bytes_read = -1;
            while((bytes_read = is.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytes_read);
            }
            outputStream.writeBytes(LINE_FEED);
            outputStream.writeBytes(boundary);

            responseMsg = connection.getResponseMessage();

使用邮递员上传同一文件时,文件上传成功

0 个答案:

没有答案