使用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();
	}

}




你可能感兴趣的:(使用HttpClient进行资源下载和压缩,提供终端版本和web版本实现)