從移動應用程序中將文件(圖像,文本等)上傳(從移動設備的SD卡)上載到文件服務器位置的功能通過SIM卡/ WiFi使用以下代碼。將移動設備的SDCard中的文件上傳到文件服務器時的進度條問題
文件上傳功能與以下代碼正常工作,只有progressdialog的更新在這裏是一個問題。的AsyncTask
的
uploadFile()從doInBackground調用(字符串... PARAMS)保護無效onProgressUpdate(字符串...值)處理progressdialog百分比更新
下面的代碼負責文件上傳
問題面臨以下代碼:
I>的程序控制ess對話框以適當的間隔從0%到100%更新&然後等待很長時間,然後返回文件狀態(布爾值)。
protected boolean uploadFile(String serverUrl, String filePath) {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int serverResponseCode;
String serverResponseMessage;
boolean uploadstatus = false;
int count;
long lengthOfFile;
try {
FileInputStream fileInputStream;
fileInputStream = new FileInputStream(new File(filePath));
URL url = new URL(serverUrl);
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=\"upload\";filename=\""
+ Utils.getInstance().getFileName(filePath) + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
lengthOfFile = new File(filePath).length();// length of file
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[1024];
bytesRead = 0;
String progressMsg = "";
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
total += bytesRead;
progressMsg = new StringBuffer("").append((int) ((total * 100)/totalLengthOfFile))
.toString();
prgressBarMsg[0] = progressMsg;
publishProgress(prgressBarMsg);
outputStream.write(buffer);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
if (serverResponseCode == 200)// HTTP OK Message from server
{
uploadstatus = true;
} else {
uploadstatus = false;
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
connection.disconnect();
}
return uploadstatus;
}
在我看來,該上progressdialog顯示的百分比實際上是從文件(位於SD卡)讀入緩衝區&不是數據,從發送到文件服務器的百分比數據移動設備通過SIM卡/ WiFi。在進度對話框顯示100%後的長時間延遲是我擔心的原因。
請確認源代碼是否顯示更新progressdialog的正確方法。此外,橫向樣式的progressdialog顯示值100% & 100/100。
如何刪除100/100的顯示?
歡迎任何提示/建議。