From 05a2b48e141fee2a695bc5d11081c3b9d3d10621 Mon Sep 17 00:00:00 2001 From: ky <592468495@qq.com> Date: Fri, 17 Jul 2020 16:29:59 +0800 Subject: [PATCH] p --- .../com/yhjd/controller/UploadController.java | 38 +----- src/main/java/com/yhjd/util/DownloadUtil.java | 108 ++++++++++++++++++ 2 files changed, 113 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/yhjd/util/DownloadUtil.java diff --git a/src/main/java/com/yhjd/controller/UploadController.java b/src/main/java/com/yhjd/controller/UploadController.java index e2ea558..2cdb3b2 100644 --- a/src/main/java/com/yhjd/controller/UploadController.java +++ b/src/main/java/com/yhjd/controller/UploadController.java @@ -1,5 +1,6 @@ package com.yhjd.controller; +import com.yhjd.util.DownloadUtil; import io.swagger.annotations.Api; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,7 +37,7 @@ public class UploadController { try { file.transferTo(dest); LOGGER.info("上传成功"); - return "上传成功"; + return fileName; } catch (IOException e) { LOGGER.error(e.toString(), e); } @@ -69,40 +70,11 @@ public class UploadController { } - @PostMapping("/download") + @GetMapping("/download") @ResponseBody - public void download(HttpServletRequest request, HttpServletResponse response) + public void download(String fileName,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - //获取下载文件,绝对路径 - String fileName = request.getParameter("fileName"); String filePath = System.getProperty("user.home") + File.separator + "img" + File.separator; - //解码 - fileName = URLDecoder.decode(fileName, "UTF-8"); - //文件名 - String realName = fileName.substring(fileName.lastIndexOf("/")+1); - System.out.println(fileName+"--->"+realName); - File file = new File(filePath+fileName); - if(!file.exists()){ - request.setAttribute("message", "资源已删除"); - request.getRequestDispatcher("/WEB-INF/download.jsp").forward(request, response); - return; - } - //设置响应头,控制浏览器下载该文件 - realName = new String(realName.getBytes("UTF-8"),"ISO-8859-1"); - response.setHeader("content-disposition", "attachment;filename=" + realName); - //response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realName, "UTF-8")); //编码 - //读取要下载的文件,保存到文件输入流 - FileInputStream in = new FileInputStream(fileName); - //输出流 - OutputStream out = response.getOutputStream(); - //缓冲区 - byte buffer[] = new byte[4096]; - int len = 0; - while((len=in.read(buffer))>0){ - out.write(buffer, 0, len); - } - in.close(); - out.close(); - + DownloadUtil.downloadFile(filePath+fileName,fileName,response,request); } } diff --git a/src/main/java/com/yhjd/util/DownloadUtil.java b/src/main/java/com/yhjd/util/DownloadUtil.java new file mode 100644 index 0000000..2c7dc7c --- /dev/null +++ b/src/main/java/com/yhjd/util/DownloadUtil.java @@ -0,0 +1,108 @@ +package com.yhjd.util; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; + +/*** + * 将文件内容响应到浏览器 + */ +public class DownloadUtil { + + // 字符编码格式 + private static String charsetCode = "utf-8"; + + + /** + * 文件的内容类型 + */ + private static String getFileContentType(String name){ + String result = ""; + String fileType = name.toLowerCase(); + if (fileType.endsWith(".png")) { + result = "image/png"; + } else if (fileType.endsWith(".gif")) { + result = "image/gif"; + } else if (fileType.endsWith(".jpg") || fileType.endsWith(".jpeg")) { + result = "image/jpeg"; + } else if(fileType.endsWith(".svg")){ + result = "image/svg+xml"; + }else if (fileType.endsWith(".doc")) { + result = "application/msword"; + } else if (fileType.endsWith(".xls")) { + result = "application/x-excel"; + } else if (fileType.endsWith(".zip")) { + result = "application/zip"; + } else if (fileType.endsWith(".pdf")) { + result = "application/pdf"; + } else { + result = "application/octet-stream"; + } + return result; + } + + /** + * 下载文件 + * @param path 文件的位置 + * @param fileName 自定义下载文件的名称 + * @param resp http响应 + * @param req http请求 + */ + public static void downloadFile(String path, String fileName, HttpServletResponse resp, HttpServletRequest req){ + + try { + File file = new File(path); + /** + * 中文乱码解决 + */ + String type = req.getHeader("User-Agent").toLowerCase(); + if(type.indexOf("firefox")>0 || type.indexOf("chrome")>0){ + /** + * 谷歌或火狐 + */ + fileName = new String(fileName.getBytes(charsetCode), "iso8859-1"); + }else{ + /** + * IE + */ + fileName = URLEncoder.encode(fileName, charsetCode); + } + // 设置响应的头部信息 + resp.setHeader("content-disposition", "attachment;filename=" + fileName); + // 设置响应内容的类型 + resp.setContentType(getFileContentType(fileName)+"; charset=" + charsetCode); + // 设置响应内容的长度 + resp.setContentLength((int) file.length()); + // 输出 + outStream(new FileInputStream(file), resp.getOutputStream()); + } catch (Exception e) { + System.out.println("执行downloadFile发生了异常:" + e.getMessage()); + } + } + + /** + * 基础字节数组输出 + */ + private static void outStream(InputStream is, OutputStream os) { + try { + byte[] buffer = new byte[10240]; + int length = -1; + while ((length = is.read(buffer)) != -1) { + os.write(buffer, 0, length); + os.flush(); + } + } catch (Exception e) { + System.out.println("执行 outStream 发生了异常:" + e.getMessage()); + } finally { + try { + os.close(); + } catch (IOException e) { + } + try { + is.close(); + } catch (IOException e) { + } + } + } + +}