Android 上传文件时进度条卡住且移动过快
我已经编写了以下代码通过服务器上传wav文件,我面临的问题是进度条移动太快,直到99%并在服务器返回200 OK响应后完成 但我在dropbox中看到,当我上传一个文件时,我可以看到进度条正在逐渐移动,看起来令人信服 任何人都知道如何显示无缝进度条Android 上传文件时进度条卡住且移动过快,android,android-progressbar,Android,Android Progressbar,我已经编写了以下代码通过服务器上传wav文件,我面临的问题是进度条移动太快,直到99%并在服务器返回200 OK响应后完成 但我在dropbox中看到,当我上传一个文件时,我可以看到进度条正在逐渐移动,看起来令人信服 任何人都知道如何显示无缝进度条 @Override protected String doInBackground(Void...args) { // TODO Auto-generated method stub
@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";
}
@覆盖
受保护字符串doInBackground(无效…参数){
//TODO自动生成的方法存根
System.out.println(上传链接);
FileInputStream sourceFile=null;
试一试{
sourceFile=新文件输入流(到);
}捕获(FileNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
网址;
试一试{
字节[]字节=新字节[(int)到.length()];
读取(字节);
url=新url(上传链接);
connection=(HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setUseCaches(false);
connection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
connection.setRequestProperty(“内容类型”、“多部分/表单数据;边界=“+boundary”);
out=新的DataOutputStream(connection.getOutputStream());
out.writeBytes(两个连字符+边界+行结束);
out.writeBytes(“内容处理:表单数据;名称=\”uploadedfile\“文件名=\”+模板ID+”.wav“+”\”+行结束);
out.writeBytes(lineEnd);
bytesAvailable=sourceFile.available();
bufferSize=Math.min(字节可用,maxBufferSize);
buffer=新字节[bufferSize];
Log.d(“BYTES”,bytesavable+“”+bufferSize+“”+BYTES.length);
int bufferLength=1024;
for(int i=0;i=bufferLength){
out.write(字节、i、缓冲区长度);
}否则{
out.write(字节,i,bytes.length-i);
}
}
out.writeBytes(lineEnd);
out.writeBytes(两个连字符+边界+两个连字符+行结束);
sourceFile.close();
out.flush();
out.close();
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
返回null;
}
试一试{
in=新的DataInputStream(connection.getInputStream());
字符串str;
Log.d(“STATUS”,connection.getResponseCode()+”);
if(connection.getResponseCode()==200)
{
而((str=in.readLine())!=null)
{
Log.e(“调试”、“服务器响应”+str);
出版进度(100);
}
}
in.close();
}
捕获(IOException ioex){
Log.e(“调试”,“错误:”+ioex.getMessage(),ioex);
}
//获取源文件
返回“成功”;
}
如果它快,它就快,不是吗?我唯一的建议是调用publishProgress(100)代码>一旦你完成上传,直到你得到回复。我也很难做到这一点,客户总是说进展不真实
我找到的解决方案是在HttpURLConnection
对象中使用setFixedLengthStreamingMode
(请参阅有关setFixedLengthStreamingMode
的更多信息)。
基本上,这是因为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()
之前设置此项。键为setChunkedStreamingMode()。在setRequestMethod()之前,需要将其设置为文件长度。这将以块的形式发送字节,并让您感觉到进度条中的进度。设置好后,可以使用问题中提供的代码 那么我们如何才能确保文件上传成功,如果我在上传之前设置usepublishProgress
,它会非常快,并且在一秒钟内完成发布进度将只更新进度条。。。你仍然可以有一些东西说它仍然在“上传”或“等待服务器响应”。在任何情况下,你能测量的东西都不存在了。我已经说过了,但这不是问题,问题是它看起来没有说服力。那么,如果现实对你来说太快了,添加一个线程。sleep()
!所以你的意思是说,在服务器确认文件已上传之前,我们无法显示进度条。我尝试了你的代码,但我在out.writeBytes(两个连字符+边界+两个连字符+行结束)处出错代码>错误为java.io.IOException:预期为9个字节,但收到11个字符串closingHeader=lineEnd+twoHyphens+boundary+twoHyphens+lineEnd代码>我缺少两个连字符,但此解决方案的底线是,您需要将要发送的所有字节传递给setFixedLengthStreamingMode。为什么是你