package com.hand.attDownload; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; public class attDownload { public static void downloadFile(HttpServletRequest request, HttpServletResponse response,String filePath,String fileName) throws IOException { // 本地文件地址,文件名称,我是在本机运行,如果是服务器的话,地址可能是 ../fileServer/file/deploy2.sh // String filePath = "D:\\project\\hl_leasing\\file\\2023\\02\\8DAA574FA8714BE9B22B861A6F55AE01"; // String fileName = "8DAA574FA8714BE9B22B861A6F55AE01"; // 获取浏览器的信息 String agent = request.getHeader("USER-AGENT"); if (agent != null && agent.toLowerCase().indexOf("FIRE_FOX") > 0) { //火狐浏览器自己会对URL进行一次URL转码所以区别处理 response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1")); } else if (agent.toLowerCase().indexOf("SAFARI") > 0) { //苹果浏览器需要用ISO 而且文件名得用UTF-8 response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1")); } else { //其他的浏览器 response.setHeader("Content-Disposition", "attachment; filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8")); } response.setContentType("charset=utf-8"); String result = "{\"statusCode\":\"00000\",\"message\":\"success\"}"; // 返回数据 response.getWriter().print(result); File file = new File(filePath); FileInputStream fileInputStream = new FileInputStream(file); byte[] fileByte = new byte[(int) file.length()]; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len; while ((len = fileInputStream.read(bytes, 0, bytes.length)) != -1) { byteArrayOutputStream.write(bytes, 0, len); } byteArrayOutputStream.close(); fileByte = byteArrayOutputStream.toByteArray(); OutputStream outputStream = null; outputStream = response.getOutputStream(); outputStream.write(fileByte); outputStream.flush(); outputStream.close(); } public static void downloadUitl(HttpServletResponse response,String filePath,String fileName) throws IOException { // 本地文件地址,文件名称,我是在本机运行,如果是服务器的话,地址可能是 ../fileServer/file/deploy2.sh // String filePath = "D:\\project\\hl_leasing\\file\\2023\\02\\8DAA574FA8714BE9B22B861A6F55AE01"; // String fileName = "8DAA574FA8714BE9B22B861A6F55AE01"; File file = new File(filePath); FileInputStream fileInputStream = new FileInputStream(file); byte[] fileByte = new byte[(int) file.length()]; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int len; while ((len = fileInputStream.read(bytes, 0, bytes.length)) != -1) { byteArrayOutputStream.write(bytes, 0, len); } byteArrayOutputStream.close(); fileByte = byteArrayOutputStream.toByteArray(); OutputStream outputStream = null; outputStream = response.getOutputStream(); outputStream.write(fileByte); outputStream.flush(); outputStream.close(); } public static void main(String[] args) throws Exception { File file=new File("D:\\aurora_reference.pdf"); String filePath = "D:\\project\\hl_leasing\\file\\2023\\02\\8DAA574FA8714BE9B22B861A6F55AE01"; String fileName = "8DAA574FA8714BE9B22B861A6F55AE01"; } }