FileUtils文件下载工具

文件下载:download(String fileName,Boolean delete,HttpServletResponse,HttpServletRequest)

参数一,文件路径
参数二,response响应输出
FileUtils.writeBytes(filePath,response.getOutputStream())

配置文件application-dev.yml

# 本地文件上传路径
dfs:
  path: E:\tmp\uploadPath\
  domain: http://localhost:9227
  staticPath: E:\work\ruoyi-ics\ruoyi-web\src\main\resources\static
  sesPath: \ses\

springboot对配置文件内容进行组件化装载到类中

@Data
@Component
@ConfigurationProperties(prefix = "dfs")
public class DfsConfig{
	
	private String path;//路径
	
	private String domain;//域名

	
}

文件路径:

String filePath = dfsConfig.getPath() + fileName;

构造response

String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);

response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
public class FileUtils{
	
	 /**
     * 下载文件名重新编码
     *
     * @param request  请求对象
     * @param fileName 文件名
     * @return 编码后的文件名
     */
     public static String setFileDownloadHeader(HttpServletRequest request, String fileName)throws UnsupportedEncodingException{
		
		final String agent = request.getHeader("USER-AGENT");
		String filename = fileName;

		//根据浏览器进行文件名的替换
		if(agent.contains("MSIE")){
			//IE浏览器
			filename = URLEncoder.encode(filename,"utf-8");
			filename = filename.replace("+"," ");
		}else if(agent.contains("Firefox")){
			//火狐浏览器
			filename = new String(fileName.getBytes(),"ISO8859-1");
		}else if(agent.contains("Chrome")){
			//谷歌浏览器
			filename = URLEncoder.encode(filename,"utf-8");
		}else{
			// 其它浏览器
            filename = URLEncoder.encode(filename, "utf-8");
		}
		return filename;
	}
	
	/**
     * 输出指定文件的byte数组
     *
     * @param filePath 文件路径
     * @param os       输出流
     * @return
     */
     public static void writeBytes(String filePath,OutputStream os)throws IOWxception{
		
		FileInputStream fis = null;
		try{
			 File file = new File(filePath);
            if (!file.exists()) {
                throw new FileNotFoundException(filePath);
            }
            fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int length;
            while ((length = fis.read(b)) > 0) {
                os.write(b, 0, length);
            }
		}catch(IOException e){
			throw e;
		}finally{
			if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
		}
	}
}


你可能感兴趣的:(下载)