Java 上传文件超过300M时应用程序崩溃
我有问题我的应用程序崩溃时,我选择了400米的文件上传,不到300米是上传罚款 我改变了这个Java 上传文件超过300M时应用程序崩溃,java,android,Java,Android,我有问题我的应用程序崩溃时,我选择了400米的文件上传,不到300米是上传罚款 我改变了这个 int maxBufferSize = 10 * 10241 * 1024; 到 还是一样的问题 我错过什么了吗 protected String doInBackground(String... urls) { String upLoadServerUri = upApi.uploadUrl; String fileName = this.file_path; HttpU
int maxBufferSize = 10 * 10241 * 1024;
到
还是一样的问题
我错过什么了吗
protected String doInBackground(String... urls) {
String upLoadServerUri = upApi.uploadUrl;
String fileName = this.file_path;
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 10 * 10241 * 1024;
File sourceFile = new File(fileName);
int sentBytes = 0;
long fileSize = sourceFile.length();
try
{
FileInputStream fileInputStream = new FileInputStream(new File(fileName));
URL url = new URL(upLoadServerUri);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1024);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"file[]\";filename=\""+ fileName + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
buffer = new byte[maxBufferSize];
while (true)
{
int bytesToRead = Math.min(maxBufferSize, fileInputStream.available());
// Break if complete
if(bytesToRead == 0){ break; }
// Read bytes
bytesRead = fileInputStream.read(buffer, 0, bytesToRead);
// Write bytes
outputStream.write(buffer, 0, bytesRead);
// Update progress dialog
sentBytes += bytesRead;
// Publish progress
double progress = (double)sentBytes / fileSize;
publishProgress((int)progress);
}
您试图分配的缓冲区太大(
10*10241*1024=104867840字节
)。甚至10241*1024
也太大(10486784字节
)。尝试将其减小到1024*1024
(1048576
)或更小;您是否能够在网络连接上以足够快的速度传输数据,从而需要如此大的缓冲区,这是值得怀疑的。您试图分配的缓冲区太大(10*10241*1024=104867840字节
)。甚至10241*1024
也太大(10486784字节
)。尝试将其减小到1024*1024
(1048576
)或更小;您是否能够在网络连接上以足够快的速度传输数据,从而需要一个如此大的缓冲区,这是值得怀疑的。如果您试图分配太大的缓冲区(10*10241*1024=104867840字节
),请提供程序“崩溃”时的具体输出。甚至10241*1024
也太大(10486784字节
)。另外,请你的问题解释一下“我的应用程序崩溃”是什么意思-你的日志告诉你什么?@KenWhite you so ri8我加10*1024*1024;如果您试图分配太大的缓冲区(10*10241*1024=104867840字节
),请提供程序“崩溃”时的具体输出。甚至10241*1024
也太大(10486784字节
)。另外,请你的问题解释一下“我的应用程序崩溃”是什么意思-你的日志告诉你什么?@KenWhite you so ri8我加10*1024*1024;当我使用2*1024*1024时,400米的进度条重置为0,当它丰富时,4%为什么!这将是一个完全不同的问题,你应该把它作为一个问题发布。(您可以在引用中链接回这个,只需在新的引用中发布计算进度百分比的代码。)这里的问题是“为什么我的应用程序崩溃了?”,我回答了这个问题。:-)每篇文章一个问题是这里的一般规则,评论仅用于要求澄清此问题或答案,而不是提出新问题。:-)当我使用2*1024*1024时,400米的进度条重置为0,当其富4%为什么!这将是一个完全不同的问题,你应该把它作为一个问题发布。(您可以在引用中链接回这个,只需在新的引用中发布计算进度百分比的代码。)这里的问题是“为什么我的应用程序崩溃了?”,我回答了这个问题。:-)每篇文章一个问题是这里的一般规则,评论仅用于要求澄清此问题或答案,而不是提出新问题。:-)
protected String doInBackground(String... urls) {
String upLoadServerUri = upApi.uploadUrl;
String fileName = this.file_path;
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 10 * 10241 * 1024;
File sourceFile = new File(fileName);
int sentBytes = 0;
long fileSize = sourceFile.length();
try
{
FileInputStream fileInputStream = new FileInputStream(new File(fileName));
URL url = new URL(upLoadServerUri);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1024);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"file[]\";filename=\""+ fileName + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
buffer = new byte[maxBufferSize];
while (true)
{
int bytesToRead = Math.min(maxBufferSize, fileInputStream.available());
// Break if complete
if(bytesToRead == 0){ break; }
// Read bytes
bytesRead = fileInputStream.read(buffer, 0, bytesToRead);
// Write bytes
outputStream.write(buffer, 0, bytesRead);
// Update progress dialog
sentBytes += bytesRead;
// Publish progress
double progress = (double)sentBytes / fileSize;
publishProgress((int)progress);
}
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): ION: alloc_data: handle(0xE4904BC0), len(368640), align(8192), flags(0x2000100), fd_data: handle(0xe4904bc0), fd(0x46)
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Allocate done for all i/p buffers
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Allocate done for all o/p buffers
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): OMX_CommandStateSet complete, m_state = 2
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command: Recieved a Command from Client
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command_proxy(): cmd = 0, Current State 2, Expected State 3
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command_proxy(): OMX_CommandStateSet issued
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Current State 2, Expected State 3
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command_proxy(): Idle-->Executing
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): send_command: Command Processed
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Rxd OMX_COMPONENT_GENERATE_START_DONE
04-04 23:11:24.664: E/OMX-VDEC-1080P(217): Rxd i/p EOS, Notify Driver that EOS has been reached
04-04 23:11:24.694: E/OMX-VDEC-1080P(217): Output EOS has been reached
04-04 23:11:24.694: E/OMX-VDEC-1080P(217): Rxd OMX_COMPONENT_GENERATE_EOS_DONE
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command: Recieved a Command from Client
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): cmd = 0, Current State 3, Expected State 2
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): OMX_CommandStateSet issued
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Current State 3, Expected State 2
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Command Recieved in OMX_StateExecuting
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): Executing --> Idle
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Driver flush i/p Port complete
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Initiate Input Flush
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Reset all the variables before flusing
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Initialize parser
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): OMX flush i/p Port complete PenBuf(0)
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Driver flush o/p Port complete
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Initiate Output Flush
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): OMX flush o/p Port complete PenBuf(0)
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Rxd OMX_COMPONENT_GENERATE_STOP_DONE
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command: Command Processed
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command: Recieved a Command from Client
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): cmd = 0, Current State 2, Expected State 1
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): OMX_CommandStateSet issued
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): Current State 2, Expected State 1
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command_proxy(): Idle-->Loaded-Pending
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): send_command: Command Processed
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ION: free: handle(0xEB8C3D00), len(2097152), fd(0x41)
04-04 23:11:24.704: E/OMXNodeInstance(217): OMX_FreeBuffer for buffer header 0x43f34670 successful
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ION: free: handle(0xDCAB0D00), len(2097152), fd(0x3f)
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ALL input buffers are freed/released
04-04 23:11:24.704: E/OMXNodeInstance(217): OMX_FreeBuffer for buffer header 0x43f34620 successful
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ION: free: handle(0xE4904BC0), len(368640), fd(0x46)
04-04 23:11:24.704: E/OMX-VDEC-1080P(217): ION: free: handle(0xD31AE040), len(73728), fd(0x44)
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): ALL output buffers are freed/released
04-04 23:11:24.784: E/OMXNodeInstance(217): OMX_FreeBuffer for buffer header 0x43f348f8 successful
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): OMX_CommandStateSet complete, m_state = 1
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): Playback Ended - PASSED
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): ALL output buffers are freed/released
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): Error in ioctl read next msg
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): omx_vdec: Async thread stop
04-04 23:11:24.784: E/OMX-VDEC-1080P(217): Close the driver instance
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): omx_vdec::component_deinit() complete
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): In OMX vdec Destructor
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): omx_vdec: message thread stop
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): Waiting on OMX Msg Thread exit
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): Waiting on OMX Async Thread exit
04-04 23:11:24.804: E/OMX-VDEC-1080P(217): Exit OMX vdec Destructor