Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
2016-01-28 10:26
1951 查看
服务器端PHP
代码如下 :
监控进度实现
首先定义监听器接口,如下所示:
实现监控进度的关键部分就在于记录已传输字节数,所以我们需重载FilterOutputStream,重写其中的关键方法,实现进度监听的功能,如下所示,本例中首先重载的是HttpEntityWrapper,顾名思义,就是将需发送的HttpEntity打包,以便计算总字节数,
代码:
实现的类和Httpclient进行上传并显示进度的功能,非常简单,代码如下,使用AsyncTask异步上传。
//上传文件到服务器
//服务器地址
//文件
转载请注明:安度博客 » Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
备份下载地址:http://download.csdn.net/detail/jdsjlzx/8486479
代码如下 :
[code]<?php $target_path = "./tmp/";//接收文件目录 $target_path = $target_path.($_FILES['file']['name']); $target_path = iconv("UTF-8","gb2312", $target_path); if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo "The file ".( $_FILES['file']['name'])." has been uploaded."; }else{ echo "There was an error uploading the file, please try again! Error Code: ".$_FILES['file']['error']; } ?>
监控进度实现
首先定义监听器接口,如下所示:
[code]//进度监听器接口 public interface ProgressListener { public void transferred(long transferedBytes); }
实现监控进度的关键部分就在于记录已传输字节数,所以我们需重载FilterOutputStream,重写其中的关键方法,实现进度监听的功能,如下所示,本例中首先重载的是HttpEntityWrapper,顾名思义,就是将需发送的HttpEntity打包,以便计算总字节数,
代码:
[code] // ProgressOutHttpEntity:输出流(OutputStream)时记录已发送 public class ProgressOutHttpEntity extends HttpEntityWrapper { private final ProgressListener listener; public ProgressOutHttpEntity(final HttpEntity entity, final ProgressListener listener) { super(entity); this.listener = listener; } public static class CountingOutputStream extends FilterOutputStream { private final ProgressListener listener; private long transferred; CountingOutputStream(final OutputStream out, final ProgressListener listener) { super(out); this.listener = listener; this.transferred = 0; } @Override public void write(final byte[] b, final int off, final int len) throws IOException { // NO, double-counting, as super.write(byte[], int, int) // delegates to write(int). // super.write(b, off, len); out.write(b, off, len); this.transferred += len; this.listener.transferred(this.transferred); } @Override public void write(final int b) throws IOException { out.write(b); this.transferred++; this.listener.transferred(this.transferred); } } @Override public void writeTo(final OutputStream out) throws IOException { this.wrappedEntity.writeTo(out instanceof CountingOutputStream ? out : new CountingOutputStream(out, this.listener)); } }
实现的类和Httpclient进行上传并显示进度的功能,非常简单,代码如下,使用AsyncTask异步上传。
[code]public class FileUploadAsyncTask extends AsyncTask<File, Integer, String> { private String url = "http://192.168.83.213/receive_file.php"; private Context context; private ProgressDialog pd; private long totalSize; public FileUploadAsyncTask(Context context) { this.context = context; } @Override protected void onPreExecute() { pd = new ProgressDialog(context); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("上传中...."); pd.setCancelable(false); pd.show(); } @Override protected String doInBackground(File... params) { // 保存需上传文件信息 MultipartEntityBuilder entitys = MultipartEntityBuilder.create(); entitys.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); entitys.setCharset(Charset.forName(HTTP.UTF_8)); File file = params[0]; entitys.addPart("file", new FileBody(file)); HttpEntity httpEntity = entitys.build(); totalSize = httpEntity.getContentLength(); ProgressOutHttpEntity progressHttpEntity = new ProgressOutHttpEntity( httpEntity, new ProgressListener() { @Override public void transferred(long transferedBytes) { publishProgress((int) (100 * transferedBytes / totalSize)); } }); return uploadFile(url, progressHttpEntity); } @Override protected void onProgressUpdate(Integer... progress) { pd.setProgress((int) (progress[0])); } @Override protected void onPostExecute(String result) { pd.dismiss(); Toast.makeText(context, result, Toast.LENGTH_SHORT).show(); }
//上传文件到服务器
//服务器地址
//文件
[code] public static String uploadFile(String url, ProgressOutHttpEntity entity) { HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); // 设置连接超时时间 httpClient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entity); try { HttpResponse httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return "文件上传成功"; } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ConnectTimeoutException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (httpClient != null && httpClient.getConnectionManager() != null) { httpClient.getConnectionManager().shutdown(); } } return "文件上传失败"; } }
转载请注明:安度博客 » Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
备份下载地址:http://download.csdn.net/detail/jdsjlzx/8486479
相关文章推荐
- Android 扩展OkHttp支持请求优先级调度
- TCP长连接与短连接的区别
- 2016太原网络营销师郭文军分享所带学员在达内的成长?
- okhttp的简单介绍(二)之简单封装
- C语言中printf输出的奇怪错误 http://segmentfault.com/q/1010000002534752
- Android的网络应用
- TCP,IP,端口,地址(初步理解)
- php cURL模拟https请求报错
- 使用afnetworking和网络服务接口及soap发送并接收http/https请求:
- 【HTTP】Fiddler(三)- Fiddler命令行和HTTP断点调试
- 【HTTP】Fiddler(二) - 使用Fiddler做抓包分析
- 【HTTP】Fiddler(一) - Fiddler简介
- 网络流算法
- 理解并使用.NET 4.5中的HttpClient
- Cross-compiling GNU libmicrohttpd for OpenWRT
- AFNetworking 3.0简单应用
- 仿netcat,手写tcp通道,创建监听拥有命令行权限!
- 机器学习中的神经网络Neural Networks for Machine Learning:Final Exam
- 深度学习(二十八)基于多尺度深度网络的单幅图像深度估计-NIPS 2014
- 深度学习(二十七)可视化理解卷积神经网络-ECCV 2014