上传 服务器 端php 代码如下 : ? php $ target_path = ./tmp/ ;// 接收文件目录 $ target_path = $ target_path .( $ _files [ 'file' ][ 'name' ] ); $ target_path = iconv ( utf-8 , gb2312 , $ target_path ); if ( move_uploaded_file ( $ _files [ 'f
上传
服务器端php代码如下 :
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'];    }?>
监控进度实现首先定义监听器接口,如下所示:
//进度监听器接口public interface progresslistener {    public void transferred(long transferedbytes);}
实现监控进度的关键部分就在于记录已传输字节数,所以我们需重载filteroutputstream,重写其中的关键方法,实现进度监听的功能,如下所示,本例中首先重载的是httpentitywrapper,顾名思义,就是将需发送的httpentity打包,以便计算总字节数,代码如下:
 // 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异步上传。
public class fileuploadasynctask extends asynctaskfile, 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();    }         //上传文件到服务器  //服务器地址     //文件    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
   
 
   