• 热门专题

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

作者:  发布日期:2015-06-25 20:15:27
Tag标签:文件下载  
  • 使用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();
    		
    		
    	}
    
    	
    
    }
    


About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规