获取上传进度

编程入门 行业动态 更新时间:2024-10-12 05:45:00
本文介绍了获取上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的上传方法如下:

private void upload(String Server, String FilePath, String SavePath, String NewName) { String end = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; try { URL url = new URL(ActionUrl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestMethod("POST"); con.setRequestProperty("Connection", "Keep-Alive"); con.setRequestProperty("Accept", "text/*"); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); ds = new DataOutputStream(con.getOutputStream()); ds.writeBytes(twoHyphens + boundary + end); ds.writeBytes("Content-Disposition: form-data;" + "name=\"folder\"" + end + end); ds.write(SavePath.getBytes("UTF-8")); ds.writeBytes(end); ds.writeBytes(twoHyphens + boundary + end); ds.writeBytes("Content-Disposition: form-data;" + "name=\"Filedata\"; filename=\""); ds.write(NewName.getBytes("UTF-8")); ds.writeBytes("\"" + end); ds.writeBytes(end); FileInputStream fStream = new FileInputStream(uploadFile); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int length = -1; while((length = fStream.read(buffer)) != -1) { ds.write(buffer, 0, length); } ds.writeBytes(end); ds.writeBytes(twoHyphens + boundary + twoHyphens + end); fStream.close(); ds.flush(); InputStream is = con.getInputStream(); int ch; StringBuffer b = new StringBuffer(); while((ch = is.read()) != -1) { b.append((char)ch); } System.out.println("UPLOAD" + "SUCCESS"); ds.close(); } catch(Exception e) { e.printStackTrace(); } }

我发现行 InputStream为= con.getInputStream(); 花大部分的时间。但如何获得它的进步派?或如何修改我的方法的部分上传文件?

I found the line InputStream is = con.getInputStream(); spend most time. But how to get the progress of it send? Or how to modify my method to upload file "by parts"?

推荐答案

您可以尝试以下code,

You can try following code,

先显示一个进度对话框致电上传方法之前,

First display a Progress Dialog Before calling your upload method,

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true); new Thread ( new Runnable() { public void run() { // your uploading code goes here } }).start(); Handler progressHandler = new Handler() { public void handleMessage(Message msg1) { progDailog.dismiss(); } }

更多推荐

获取上传进度

本文发布于:2023-11-26 04:32:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度   上传

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!