文件上传进度上传前跳跃到100% [英] file upload progress jumping to 100% before upload
本文介绍了文件上传进度上传前跳跃到100%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法做到进度百分比这个文件上传功能。我GOOGLE了很多我无法找到解决办法
I am not able to do progress percentage to this file upload function. I googled it a lot i couldn't find solution
这是类
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
notification.contentView.setProgressBar(R.id.progressBar, 10, progress[0], false);
contentView.setTextViewText(R.id.text, "changed");
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
@Override
protected String doInBackground(String... urls) {
String upLoadServerUri = upurl;
String fileName = urls[0];
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 100 * 1024 * 1024;
File sourceFile = new File(fileName);
int sentBytes = 0;
long fileSize = sourceFile.length();
try
{
FileInputStream fileInputStream = new FileInputStream(new File(urls[0]));
URL url = new URL(upurl);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// 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)
{
// Update progress dialog
sentBytes += bytesRead;
publishProgress((int)(sentBytes * 100 / fileSize));
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
Log.v("Available",bytesAvailable+"");
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
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();
String serverResponseMessage = connection.getResponseMessage();
if(serverResponseCode == 200)
{
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(BabupMain.this, "server say :" + response , Toast.LENGTH_SHORT).show();
Toast.makeText(BabupMain.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
}
});
}
fileInputStream.close();
outputStream.flush();
outputStream.close();
} catch (MalformedURLException ex) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(BabupMain.this, "Error 1 ", Toast.LENGTH_SHORT).show();
}
});
} catch (final Exception e) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(BabupMain.this, "Error 2 "+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
//publishProgress();
return null;
}
任何想法!
推荐答案
很难说真的,但考虑更改
Hard to say really, but consider changing
publishProgress((int)(sentBytes * 100 / fileSize))
要
double progress = (double)sentBytes / fileSize;
publishProgress((int)progress);
修改
我要改变这一点:
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
// Update progress dialog
sentBytes += bytesRead;
publishProgress((int)(sentBytes * 100 / fileSize));
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
Log.v("Available",bytesAvailable+"");
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
要
缓冲=新的字节[MAXBUFFERSIZE];
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);
}
如果您遇到这样的问题,我想放弃了 FileInputStream.available()
的办法,从的 File.length()
并打破循环时读取动作方式> = File.length()
If you're having issues with this, I'd abandon the FileInputStream.available()
approach, get the total number of bytes from File.length()
and break out of the loop when bytesRead >= File.length()
.
这篇关于文件上传进度上传前跳跃到100%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文