Sending file > 1MB using HTTP POST

这一生的挚爱 提交于 2019-12-01 07:26:29

问题


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?


回答1:


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);
}



回答2:


Look at this example:

Uploading files to HTTP server using POST on Android.




回答3:


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.



来源:https://stackoverflow.com/questions/9745514/sending-file-1mb-using-http-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!