当我上传超过5MB的文件时,进度条在达到4%时自动重置,并且每10秒从0开始。但是,对于5MB以下的文件,进度条可以正常工作并达到100%进度条重置自己?
这是因为maxBufferSize
?服务器的最大发布大小是512MB,其他的都是无限的,所以问题必须来自我的代码,但我不知道在哪里。
这里是我的代码
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
//update Dialog box progress
mProgressDialog.setProgress(progress[0]);
if (nofti_appended)
{
//update Notification progress
mBuilder.setProgress(100, progress[0], false);
}
}
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 = 1*1024*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);
bytesAvailable = fileInputStream.available();
Log.v("Size",bytesAvailable+"");
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
if(isCancelled()){
break;
}
sentBytes += bytesRead;
publishProgress((int)(sentBytes * 100/fileSize));
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
if(isCancelled()){
return "faild";
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Scanner s;
s = new Scanner(connection.getInputStream());
s.useDelimiter("\\Z");
final String response = s.next();
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
fileInputStream.close();
outputStream.flush();
outputStream.close();
if(serverResponseCode == 200)
{
return response;
}
} catch (MalformedURLException ex) {
} catch (final Exception e) {
}
return "faild";
}
任何想法?
您需要修剪这个删除所有这一切无关与实际上传过程中的额外的代码。内容类型和其他所有内容都不需要,并且使您的实际问题难以回答。请[编辑您的问题]删除除了与您在此询问的问题相关的代码之外的所有内容,以便我们尝试帮助您找到解决方案。谢谢。 :-) – 2013-04-04 22:47:52
现在呢?我的问题很难理解!所有我需要知道为什么进度条重置为0时,其大文件达到4% – 2013-04-05 00:49:41
publishProgress((int)(sentBytes * 100/fileSize));如果您的进度条重置,那么该行必须设置为0。你为什么乘着sentBytes * 100? – 2013-04-05 01:03:53