Android中的文件上传下载

一、文件上传

1. 使用HttpUrlConnection

//此处省略线程操作和Handler通信File file = new File(etUpload.getText().toString());FileInputStream in = new FileInputStream(file);URL url = new URL(path);// 开启连接HttpURLConnection conn = (HttpURLConnection) url.openConnection();// 设置请求方式conn.setRequestMethod("POST");//设置文件大小conn.setChunkedStreamingMode(0);// 设置超时conn.setConnectTimeout(10000);// 设置发送内容conn.setDoInput(true);byte[] buffer = new byte[1024];int len = 0;while ((len = in.read(buffer)) != -1) {    conn.getOutputStream().write(buffer, 0, len);}in.close();

2. 使用AsyncHttpClient

//创建clientAsyncHttpClient client = new AsyncHttpClient();//准备请求RequestParams params = new RequestParams();params.put("file", new File(etUpload.getText().toString()));//发出请求client.post(path, params, new AsyncHttpResponseHandler(){    @Override    public void onSuccess(int statusCode, String content) {        //获取响应体        Toast.makeText(MainActivity02.this, "发送成功!", Toast.LENGTH_SHORT).show();    }    @Override    public void onFailure(Throwable error, String content) {        //获取响应体        Toast.makeText(MainActivity02.this, "发送失败!", Toast.LENGTH_SHORT).show();    }});

二、文件下载

1. 多线程下载过程

  1. 获取连接
  2. 准备请求信息
  3. 获取服务端文件大小
  4. 客户端创建同等大小的随机访问文件
  5. 创建多线程,分配任务
  6. 分段请求资源并写入到随机访问文件中
  7. 通知下载完毕并断开连接

2. 多线程下载(HttpUrlConnection)

public void downloadImage(View v) {    new Thread() {        public void run() {            HttpURLConnection conn = null;            try {                // 获取连接                URL url = new URL(path);                conn = (HttpURLConnection) url.openConnection();                // 准备请求信息,请求方式,最大延时                conn.setRequestMethod("GET");                conn.setReadTimeout(10000);                // 获取响应信息                if (conn.getResponseCode() == 200) {                    // 获取服务端文件大小                    int length = conn.getContentLength();                    // 客户端新建同等大小的文件                    RandomAccessFile raf = new RandomAccessFile(fileName,                            "rw");                    raf.setLength(length);                    raf.close();                    // 创建多线程,分配任务                    int threadCount = 3;// 线程数量                    int blockSize = length / threadCount;// 每个线程负责的任务量                    for (int i = 0; i < threadCount; i++) {                        int start = i * blockSize;// 开始读取位置                        int end = (i + 1) * blockSize - 1;// 结束读取位置                        if (i == threadCount - 1) {                            end = length;                        }                        // 多线程下载                        new AsyncDownload(i, start, end).start();                    }                }            } catch (Exception e) {                e.printStackTrace();            } finally {                if (conn != null){                    conn.disconnect();// 断开连接                }            }        }    }.start();}private class AsyncDownload extends Thread {    int threadIndex;    int start;    int end;    AsyncDownload(int threadIndex, int start, int end) {        this.threadIndex = threadIndex;        this.start = start;        this.end = end;    }    @Override    public void run() {        HttpURLConnection conn=null;        try {            URL url = new URL(path);            // 获取连接            conn = (HttpURLConnection) url.openConnection();            // 准备请求            conn.setRequestMethod("GET");            conn.setRequestProperty("range", "bytes=" + start + "-" + end);            conn.setReadTimeout(10000);            // 获取响应,请求码表示请求一段信息完毕            if (conn.getResponseCode() == 206) {                // 获取对应的字节流并写入文件                InputStream is = conn.getInputStream();                RandomAccessFile raf = new RandomAccessFile(fileName, "rw");                raf.seek(start);                byte[] buff = new byte[1024];                int len = 0;                while ((len = is.read(buff)) != -1) {                    raf.write(buff, 0, len);                }                // 关闭流                raf.close();                System.out.println("第" + threadIndex + "个线程下载完毕");            }        } catch (Exception e) {            e.printStackTrace();        } finally {            if (conn != null) {                // 关闭连接                conn.disconnect();            }            //使用唯一的对象加同步,保证runningThread数据的正确性            synchronized (MainActivity.class) {                runningThread--;                if (runningThread == 0) {                    Message msg = new Message();                    msg.what = DOWNLOAD_FINISH;                    mHandler.sendMessage(msg);                }            }        }    }}

3. 多线程下载(AsyncHttpClient)

使用BinaryHttpResponseHandler

public void downloadImage(View v) {// 建立一个访问clientAsyncHttpClient client = new AsyncHttpClient();// 发出请求client.get(path, new BinaryHttpResponseHandler(){    @Override    public void onSuccess(byte[] binaryData) {            try {                //获取文件并保存                FileOutputStream out = new FileOutputStream(new File(fileName));                out.write(binaryData);                out.close();            } catch (Exception e) {                e.printStackTrace();            }        }    });}

更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  2. ionic爬过的各种坑;(持续更新)
  3. 最新eclipse中android插件安装下载地址
  4. Android根据经纬度获取位置信息
  5. android客户端从服务器端下载文件,服务端返回文件流(文件不在项目
  6. Android(安卓)广播获取短信内容
  7. Android之基于HTTP协议的下载
  8. android 随手记之文件+参数上传请求
  9. Android(安卓)startActivityForResult的使用

随机推荐

  1. android应用程序抓包
  2. Android(安卓)apk 二次打包植入广告
  3. android list排序
  4. Android摄像头开发:实时摄像头视频预览帧
  5. 开源项目之Android(安卓)GreenDroid(界面
  6. Android(安卓)HIDL理解(基于Android(安卓)
  7. Android(安卓)WebView安全研究
  8. 实现ListView的弹性滚动
  9. eclipse build 不生成apk的办法
  10. Nexus s Android(安卓)4.1.1 通过USB共享