Sending file > 1MB using HTTP POST

2019-02-19 13:30发布

I'm sending video and audio files from my Android application to Wampserver, some of these can get quite large and I tend to get OutofMemory issues when the file is approximately over 1MB in size.

I convert each file individually into a byte stream. I think the byte stream is too large hence the OutofMemory.

How can I stop this error from occurring?

3条回答
贪生不怕死
2楼-- · 2019-02-19 13:40

When you have large size of data like big file or big image/good quality image to send to server then you can send it in parts/chunks.

First make Http Connection with the Server and then send chunks with DataOutputStream Class. On the server side same, you need to implement code for receiving these chunks and make them in one file once you get all the chunks.

查看更多
贼婆χ
4楼-- · 2019-02-19 13:58

Using the link Maxium suggested here:

Uploading files to HTTP server using POST on Android.

I then found this Out of Memory error in android to fix the error.

Replace:

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

with:

while (bytesRead > 0){
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    byte byt[]=new byte[bufferSize];
    fileInputStream.read(byt);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    outputStream.write(buffer, 0, bufferSize);
}
查看更多
登录 后发表回答