servlet文件下载2(单文件下载和批量下载)

使用servlet完毕单文件下载和批量文件下载。批量下载的原理是先将文件打包成zip , 然后再下载。

之前也转载过一篇文件下载的博客,地址:http://blog.csdn.net/ch717828/article/details/42809999

这篇的demo要更简单一些,须要深入了解更參数含义的能够看第一篇博客

单文件下载:

<a href="servlet/SmartDownLoadFile?

filename=gui.jpg">附件</a>


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SmartDownLoadFile extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String path = getServletContext().getRealPath("/");
		String filename = request.getParameter("filename");
		File file = new File(path + filename);
		
		/*下载文件*/
		if(file.exists()){
			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition", "attachment;filename="" + filename + """);
			InputStream inputStream = new FileInputStream(file);
			ServletOutputStream ouputStream = response.getOutputStream();
			try{
				byte b[] = new byte[1024];
				int n ;
				while((n = inputStream.read(b)) != -1){
					ouputStream.write(b,0,n);
				}
			}finally
			{
				ouputStream.close();
				inputStream.close();
			}
		}
		else
		{
			System.out.println("文件不存在");
		}
	}
	
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req,resp);
	}

	

}

文件批量下载:
 <form action="servlet/BatchDownloadServlet" method="post">
  	 	<input type="checkbox"  name="filename" value="gui.jpg" />Image
  	 	<input type="checkbox"  name="filename" value="gui2.jpg" />Image2
  	 	<input type="checkbox"  name="filename" value="gui3.jpg" />Image3<br>
  	 	<input type="submit" value="下载">
  	 </form> 

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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;


public class BatchDownloadServlet extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("application/x-msdownload");
		response.setHeader("Content-Disposition", "attachment;filename=test.zip"); //filename參数指定下载的文件名称
		String path = getServletContext().getRealPath("/");
		/*将要下载的文件打包成zip再下载*/
		String[] filenames  = request.getParameterValues("filename"); //获取要请求下载的文件名称
		ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); 
		for(String filename : filenames){
			File file = new File(path + filename);
			zos.putNextEntry(new ZipEntry(filename));
			FileInputStream fis = new FileInputStream(file);
			try
			{
				byte b[] = new byte[1024];
				int n = 0;
				while((n = fis.read(b)) != -1){
					zos.write(b, 0, n);
				}
			}finally
			{
				zos.flush();
				fis.close();
			}
		}
		zos.flush();
		zos.close();
		
		
	}

	

}

完整Demo:http://download.csdn.net/detail/ch717828/8837101

原文地址:https://www.cnblogs.com/yangykaifa/p/7244771.html