我无法对此文件上传功能进行进度百分比。我google了很多我找不到解决方案
这是我的代码:
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 = 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;
}