我已经编写了以下代码来通过服务器上传wav文件,我面临的问题是进度条移动得太快,直到99%并完成服务器返回200 OK响应。
但我在Dropbox中看到,当我上传文件时,我可以看到进度条逐渐移动并且看起来很有说服力。
任何人都知道如何展示无缝进度条。
@Override
protected String doInBackground(Void...args) {
// TODO Auto-generated method stub
System.out.println(uploadLink);
FileInputStream sourceFile = null;
try {
sourceFile = new FileInputStream(to);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
URL url;
try {
byte[] bytes = new byte[(int) to.length()];
sourceFile.read(bytes);
url = new URL(uploadLink);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
out = new DataOutputStream( connection.getOutputStream() );
out.writeBytes(twoHyphens + boundary + lineEnd);
out.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + templateID + ".wav" + "\"" + lineEnd);
out.writeBytes(lineEnd);
bytesAvailable = sourceFile.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
Log.d("BYTES" , bytesAvailable + " "+bufferSize +" "+ bytes.length);
int bufferLength = 1024;
for (int i = 0; i < bytes.length; i += bufferLength) {
int progress = (int)((i / (float) bytes.length) * 100);
publishProgress(progress);
if (bytes.length - i >= bufferLength) {
out.write(bytes, i, bufferLength);
} else {
out.write(bytes, i, bytes.length - i);
}
}
out.writeBytes(lineEnd);
out.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
sourceFile.close();
out.flush();
out.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
try {
in = new DataInputStream ( connection.getInputStream() );
String str;
Log.d("STATUS",connection.getResponseCode()+ " ");
if(connection.getResponseCode() == 200)
{
while (( str = in.readLine()) != null)
{
Log.e("Debug","Server Response "+str);
publishProgress(100);
}
}
in.close();
}
catch (IOException ioex){
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
// Get the source File
return "success";
}
答案 0 :(得分:1)
我也很难做到这一点,客户总是说进展并不真实。
我找到的解决方案是在setFixedLengthStreamingMode
对象中使用HttpURLConnection
(请参阅有关setFixedLengthStreamingMode
here的详情)。
基本上这样做的是Content-Length
标头设置得更快,因此URLConnection
可以更频繁地刷新。
您的代码应该是这样的:
String header = twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + templateID + ".wav" + "\"" + lineEnd + lineEnd;
String closingHeader = lineEnd + twoHyphens + boundary + twoHyphens + lineEnd;
int bytesToBeSent = header.lenght() + closingHeader.lenght() + (int)to.length();
connection.setFixedLengthStreamingMode(bytesToBeSent);
必须在使用setRequestMethod()
之前设置此项。
答案 1 :(得分:0)
如果速度快,那就快,不是吗?我建议的唯一方法就是在完成上传后拨打publishProgress(100);
,直到收到回复为止。
答案 2 :(得分:-1)
键是setChunkedStreamingMode()。您需要在setRequestMethod()之前将其设置为文件长度。这将以块的形式发送字节,并让您了解进度条中的进度。设置完毕后,您可以使用问题中提供的代码。