使用HttpClient进行资源下载和压缩,提供终端版本和web版本实现

HttpClient使用时需要的包如下
commons-httpclient-3.1.jar
commons-logging-1.0.4.jar
commons-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();
	}

}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值