zoukankan      html  css  js  c++  java
  • Java Web文件下载

    Web文件下载有两种。一种是文件在站点文件夹下。在浏览器中直接输入文件路径就可以下载。如http://www.xxx.com/file.zip。第二种是文件不在站点文件夹下或者文件是动态生成的(导出报表或者导出excel等),这样的情况须要通过response的OutputStream实现文件的下载。

    DownloadUtils是一个Java Web文件下载工具类,提供多种静态方法实现文件下载。

    package com.rhui.util;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URLEncoder;
    
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.lang3.StringUtils;
    
    /**
     * 文件下载类
     */
    public class DownloadUtils {
    	/**
    	 * 文件下载编码
    	 * 该编码告诉浏览器文件名称的编码方式,以防下载中文文件名称时有乱码
    	 */
    	private static String encoding = "utf-8";
    	
    	/**
    	 * 文件下载
    	 * @param response
    	 * @param filePath 文件在server上的路径,包含文件名称
    	 */
    	public static void download(HttpServletResponse response, String filePath){
    		File file = new File(filePath.toString());
    		download(response, file, null, encoding);
    	}
    	
    	/**
    	 * 文件下载
    	 * @param response
    	 * @param filePath 文件在server上的路径。包含文件名称称
    	 * @param fileName 文件下载到浏览器的名称。假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
    	 */
    	public static void download(HttpServletResponse response, String filePath, String fileName){
    		File file = new File(filePath.toString());
    		download(response, file, fileName, encoding);
    	}
    	
    	/**
    	 * 文件下载
    	 * @param response
    	 * @param filePath 文件在server上的路径,包含文件名称称
    	 * @param fileName 文件下载到浏览器的名称,假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
    	 * @param encoding 文件名称称编码
    	 */
    	public static void download(HttpServletResponse response, String filePath, String fileName, String encoding){
    		File file = new File(filePath.toString());
    		download(response, file, fileName, encoding);
    	}
    	
    	/**
    	 * 文件下载
    	 * @param response
    	 * @param file 文件
    	 * @param fileName 文件下载到浏览器的名称,假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
    	 */
    	public static void download(HttpServletResponse response, File file) {
    		download(response, file, null, encoding);
    	}
    	
    	/**
    	 * 文件下载
    	 * @param response
    	 * @param file 文件
    	 * @param fileName 文件下载到浏览器的名称,假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
    	 */
    	public static void download(HttpServletResponse response, File file, String fileName) {
    		download(response, file, fileName, encoding);
    	}
    	
    	/**
    	 * 文件下载
    	 * @param response
    	 * @param file 文件
    	 * @param fileName 文件下载到浏览器的名称,假设不想让浏览器下载的文件名称称和server上的文件名称称一样,请设置该參数
    	 * @param encoding 文件名称称编码
    	 */
    	public static void download(HttpServletResponse response, File file, String fileName, String encoding) {
    		if(file == null || !file.exists() || file.isDirectory()){
    			return;
    		}
    		
    		// 假设不指定文件下载到浏览器的名称,则使用文件的默认名称
    		if (StringUtils.isBlank(fileName)) {
    			fileName = file.getName();
    		}
    
    		try {
    			InputStream is = new FileInputStream(file);
    			download(response, is, fileName, encoding);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	/**
    	 * 文件下载
    	 * @param response
    	 * @param is 文件输入流
    	 * @param fileName 下载的文件名称称
    	 * @throws IOException
    	 */
    	public static void download(HttpServletResponse response, InputStream is, String fileName){
    		download(response, is, fileName, encoding);
    	}
    	
    	/**
    	 * 文件下载
    	 * @param response
    	 * @param is 文件输入流
    	 * @param fileName 下载的文件名称称
    	 * @param encoding 编码格式
    	 */
    	public static void download(HttpServletResponse response, InputStream is, String fileName, String encoding){
    		if(is == null || StringUtils.isBlank(fileName)){
    			return;
    		}
    		
    		BufferedInputStream bis = null;
    		OutputStream os = null;
    		BufferedOutputStream bos = null;
    		
    		try{
    			bis = new BufferedInputStream(is);
    			os = response.getOutputStream();
    			bos = new BufferedOutputStream(os);
    			response.setContentType("application/octet-stream;charset=" + encoding);
    			response.setCharacterEncoding(encoding);
    	        response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, encoding));
    	        byte[] buffer = new byte[1024];
    	        int len = bis.read(buffer);
    	        while(len != -1){
    	        	bos.write(buffer, 0, len);
    	        	len = bis.read(buffer);
    	        }
    	        
    	        bos.flush();
    		}catch(IOException e){
    			e.printStackTrace();
    		}finally{
    			if(bis != null){
    				try{
    					bis.close();
    				}catch(IOException e){}
    			}
    			
    			if(is != null){
    				try{
    					is.close();
    				}catch(IOException e){}
    			}
    		}
    	}
    
    	public static String getEncoding() {
    		return encoding;
    	}
    
    	public static void setEncoding(String encoding) {
    		DownloadUtils.encoding = encoding;
    	}
    }
    

    假设文件保存在server的非站点文件夹下

    String filePath = "c:\file.zip";
    DownloadUtils.download(response, filePath);


    假设文件是输入流

    // is为文件输入流
    // fileName为浏览器下载的文件名
    // encoding为文件名编码,预防文件里有中文的时候产生乱码
    String fileName = "file.zip";
    String encoding = "utf-8";
    DownloadUtils.download(response, is, fileName, encoding);

    Servlet中文件下载

    package com.rhui.web.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.rhui.util.DownloadUtils;
    
    @WebServlet("/download/servlet")
    public class DownloadServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	
    	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		String filePath = "c:\file.zip";
    		DownloadUtils.download(response, filePath);
    	}
    
    }


  • 相关阅读:
    redis 学习笔记
    导数据方法
    数据库常用操作
    zepto.js
    shopnc
    vue.js
    laravel
    mysql进阶学习
    Python基础------生成器表达式形式、面向过程编程、内置函数部分
    Python基础----生成器、三元表达式、列表生成式、生成器表达式
  • 原文地址:https://www.cnblogs.com/yfceshi/p/6904565.html
Copyright © 2011-2022 走看看