使用FixedLengthStreamingMode的HTTPURLConnection进行multipart/form-data POST上传大文件

7

我正在尝试发送一个包含大型图像文件的multipart/form-data POST请求。我无法预先将文件转换为字节数组,否则我的应用程序会崩溃并出现OutOfMemory异常,因此我必须直接将文件内容写入连接的outputstream中。此外,我的服务器不支持分块模式,因此在发送数据之前必须计算内容长度,并使用连接的setFixedLengthStreamingMode方法。

public void createImagePostWithToken(String accessToken, String text,
        String type, String imagePath) {

    URL imageUrl = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";

    // generating byte[] boundary here

    HttpURLConnection conn = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null; 

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
        long contentLength;
        int serverResponseCode;
        String serverResponseMessage;
        File file = new File(imagePath);            
        FileInputStream fileInputStream = new FileInputStream(file);
        imageUrl = buildUri("posts").toURL();
        conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setDoOutput(true);
        conn.setDoInput(true);         
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);            

        String stringForLength = new String();  

        stringForLength += "Content-Type: multipart/form-data;boundary=" + boundary;

        stringForLength += twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"access_token\"" + lineEnd;
        stringForLength += "Content-Type: text/plain;charset=UTF-8" + lineEnd + "Content-Length: " + accessToken.length() + lineEnd + lineEnd;
        stringForLength += accessToken + lineEnd + twoHyphens + boundary + lineEnd;

        stringForLength += "Content-Disposition: form-data; name=\"text\"" + lineEnd;
        stringForLength += "Content-Type: text/plain;charset=UTF-8" + lineEnd + "Content-Length: " + text.length() + lineEnd + lineEnd;
        stringForLength += text + lineEnd + twoHyphens + boundary + lineEnd;

        stringForLength += "Content-Disposition: form-data; name=\"type\"" + lineEnd;
        stringForLength += "Content-Type: text/plain;charset=UTF-8" + lineEnd + "Content-Length: " + type.length() + lineEnd + lineEnd;
        stringForLength += type + lineEnd + twoHyphens + boundary + lineEnd;

        stringForLength += twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"image\"" + lineEnd;
        stringForLength += "Content-Type: application/octet-stream" + lineEnd + "Content-Length: " + file.length() + lineEnd + lineEnd;
        stringForLength += lineEnd + twoHyphens + boundary + twoHyphens + lineEnd;

        int totalLength = stringForLength.length() + (int)file.length();           
        conn.setFixedLengthStreamingMode(totalLength); 


        outputStream = new DataOutputStream( conn.getOutputStream() );          
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);

        // access token 

        outputStream.writeBytes("Content-Disposition: form-data; name=\"access_token\"" + lineEnd);
        outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
        outputStream.writeBytes("Content-Length: " + accessToken.length() + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(accessToken + lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);

        // text 

        outputStream.writeBytes("Content-Disposition: form-data; name=\"text\"" + lineEnd);
        outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
        outputStream.writeBytes("Content-Length: " + text.length() + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(text + lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);

        // type 

        outputStream.writeBytes("Content-Disposition: form-data; name=\"type\"" + lineEnd);
        outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
        outputStream.writeBytes("Content-Length: " + type.length() + lineEnd);
        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(type + lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);

        // image

        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"image\"" + lineEnd);
        //outputStream.writeBytes(lineEnd);
        outputStream.writeBytes("Content-Type: application/octet-stream" + lineEnd);
        outputStream.writeBytes("Content-Length: " + file.length() + lineEnd);
        outputStream.writeBytes(lineEnd);           

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        // Read file
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
        outputStream.write(buffer, 0, bufferSize);          
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes(lineEnd);
        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        Log.d("posttemplate", "connection outputstream size is " + outputStream.size());

        // finished with POST request body


     // Responses from the server (code and message)
        serverResponseCode = conn.getResponseCode();
        serverResponseMessage = conn.getResponseMessage();

        Log.d("posttemplate", "server response code "+ serverResponseCode);
        Log.d("posttemplate", "server response message "+ serverResponseMessage);

        fileInputStream.close();
        conn.disconnect();
        outputStream.flush();
        outputStream.close();


    } catch (MalformedURLException e)
    {
        Log.d("posttemplate", "malformed url", e);
        //TODO: catch exception;
    } catch (IOException e)
    {
        Log.d("posttemplate", "ioexception", e);
        //TODO: catch exception
    }        

}

很不幸,我的应用在 outputStream.close() 处崩溃并抛出 IOException 异常,我不知道原因是什么:

03-16 13:56:51.035: D/posttemplate(6479): java.io.IOException: unexpected end of stream
03-16 13:56:51.035: D/posttemplate(6479):   at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthOutputStream.close(FixedLengthOutputStream.java:57)
03-16 13:56:51.035: D/posttemplate(6479):   at java.io.FilterOutputStream.close(FilterOutputStream.java:66)
03-16 13:56:51.035: D/posttemplate(6479):   at com.futubra.api.impl.PostTemplate.createImagePostWithToken(PostTemplate.java:282)
03-16 13:56:51.035: D/posttemplate(6479):   at com.futubra.FutubraNewPostActivity.createPost(FutubraNewPostActivity.java:128)
03-16 13:56:51.035: D/posttemplate(6479):   at com.futubra.FutubraNewPostActivity_.access$2(FutubraNewPostActivity_.java:1)
03-16 13:56:51.035: D/posttemplate(6479):   at com.futubra.FutubraNewPostActivity_$5.run(FutubraNewPostActivity_.java:141)
03-16 13:56:51.035: D/posttemplate(6479):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-16 13:56:51.035: D/posttemplate(6479):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-16 13:56:51.035: D/posttemplate(6479):   at java.lang.Thread.run(Thread.java:1019)
4个回答

5
outputStream.close()之后,使用conn.disconnect()

2

HTTP头部不是正文的一部分,因此在计算正文长度时不应将其纳入考虑。

 stringForLength += "Content-Type: multipart/form-data;boundary=" + boundary;

删除上面的行,这样您的内容长度就会正确。


1

提供信息,您的代码应该可以正常使用HTTP URL连接,但是HTTPS会触发内存溢出错误,因为在Android 2.3.4中的HttpsURLConnectionImpl.java存在一个错误(我已经在我的平板电脑上验证过),而这个错误已经在Android 4.1中得到了修复(我已经检查过源代码)。


你能告诉我你提到的那个bug在哪里吗?我似乎在Gingerbread上使用HttpsUrlConnection时遇到了相同的问题。 - HungryTux

0
   int totalLength = stringForLength.length() + (int)file.length();  

你上面的代码是错误的。

你应该使用字符串的字节长度,如下所示

 int totalLength = stringForLength.getBytes().length + (int)file.length();  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接