使用HttpClient进行资源下载和压缩,提供终端版本和web版本实现
来源:互联网 发布:淘宝店仓库管理 编辑:程序博客网 时间:2024/06/07 08:12
HttpClient使用时需要的包如下
commons-httpclient-3.1.jarcommons-logging-1.0.4.jarcommons-codec-1.2.jar
终端版本
package TestHttpclient;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.GetMethod;public class App {public static void main(String[] args) throws IOException {// 多个文件打包下载String[] uris = {"http://b.55show.com/uploads/day_20090308/face_1EIeBR.gif","http://b.55show.com/uploads/day_20090308/face_hXAI5T.gif","http://b.55show.com/uploads/day_20090307/face_EdALSr.gif" };OutputStream os = new FileOutputStream("C:/Users/Steven.Yang/temp.zip");ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(os));HttpClient hc = new HttpClient();GetMethod gm = null;for (String uri : uris) {gm = new GetMethod(uri);hc.executeMethod(gm);InputStream is = gm.getResponseBodyAsStream();int signIndex = uri.lastIndexOf("/");String name = uri.substring(signIndex + 1);out.putNextEntry(new ZipEntry(name));int c = -1;while ((c = is.read()) != -1) {out.write(c);}is.close();}out.flush();out.close();// 单个文件下载// String uri = "http://192.168.1.3:8080/TgsPic/kakou.jpg";//// HttpClient hc = new HttpClient();// GetMethod gm = new GetMethod(uri);//// hc.executeMethod(gm);//// InputStream is = gm.getResponseBodyAsStream();// OutputStream os = new// FileOutputStream("C:/Users/Steven.Yang/temp.jpg");//// int c = -1;// while ((c = is.read()) != -1) {// os.write(c);// }// is.close();// os.flush();// os.close();}}
web servlet版本
package cn.steven.servlet;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.net.URLEncoder;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.GetMethod;public class Downloadservlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("application/zip");String filename = URLEncoder.encode("test测试test.zip", "UTF-8");response.setHeader("Content-disposition", "attachment; filename="+ filename + ";");// 多个文件打包下载String[] uris = {"http://b.55show.com/uploads/day_20090308/face_1EIeBR.gif","http://b.55show.com/uploads/day_20090308/face_hXAI5T.gif","http://b.55show.com/uploads/day_20090307/face_EdALSr.gif" };// OutputStream os = new// FileOutputStream("C:/Users/Steven.Yang/temp.zip");ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(response.getOutputStream()));HttpClient hc = new HttpClient();GetMethod gm = null;for (String uri : uris) {gm = new GetMethod(uri);hc.executeMethod(gm);InputStream is = gm.getResponseBodyAsStream();int signIndex = uri.lastIndexOf("/");String name = uri.substring(signIndex + 1);out.putNextEntry(new ZipEntry(name));int c = -1;while ((c = is.read()) != -1) {out.write(c);}is.close();}out.flush();out.close();}}
- 使用HttpClient进行资源下载和压缩,提供终端版本和web版本实现
- MeeGo 进行中: 提供源代码和开发者版本
- MeeGo 进行中: 提供源代码和开发者版本
- android 实现版本更新和下载安装
- 使用GitHub和Git进行版本控制
- 使用Gitlab和SourceTree进行版本控制
- 使用github和git进行版本控制
- web-start 进行版本下载,如何对比版本策略
- 移动终端和pc浏览器版本检测
- gradle版本 gradle各种版本下载提供
- eclipse下载和版本问题
- 提供《使用OpenSER构建电话通信系统》中文翻译版本下载
- 使用diff和patch进行简单的文件版本管理
- 使用diff和patch进行简单的文件版本管理
- 使用diff和patch进行简单的文件版本管理
- 使用git和github 进行代码的版本控制
- Android源码学习 使用Git和Repo进行版本管理
- 使用libsvm进行分类之python和java版本
- 看了半小时的MVC,总结下
- springMVC基础
- PL/SQL开发中动态SQL的使用方法(原文http://dev.yesky.com/187/2029687.shtml)
- EM算法
- 装机时遇到的英语
- 使用HttpClient进行资源下载和压缩,提供终端版本和web版本实现
- Objective-C内存管理教程和原理剖析
- 如何使用Core Text计算一段文本绘制在屏幕上之后的高度
- 汉诺塔问题
- 基于内存数据库的分布式数据库架构
- Steps to install JDK7 on Redhat Enterprise 5
- 关于Apache CXF
- 程序员编程标准
- zend studio不能打开remote file