jsp - 下载gzip压缩文件

out.clearBuffer();
//获取文件地址
String fileName = request.getParameter("f");
File file = new File(fileName);

response.setContentType("application/x-download");//设置为下载application/x-download
response.addHeader("Content-Disposition","attachment;filename=" + file.getName()+".zip");
FileInputStream in = null;
OutputStream outp = null;
GZIPOutputStream gzout = null;
try{
	in = new FileInputStream(file);
	outp = response.getOutputStream();
	gzout = new GZIPOutputStream(outp);
	byte[] buf = new byte[1024];
	int i = 0;
	while ((i = in.read(buf)) != -1){
		gzout.write(buf,0,i);
	}
	outp.flush();
}catch (Exception e){
	System.out.println("Error!");
	e.printStackTrace();
}finally {
	if(gzout != null) gzout.close();
	if(outp != null) outp.close();
	if(in != null) in.close();
}
return;


你可能感兴趣的:(jsp)