From f3765cadc9bc0d76bec5c0b6602602518dec2773 Mon Sep 17 00:00:00 2001 From: wanggeng <450292408@qq.com> Date: Wed, 1 Sep 2021 11:55:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=9D=99=E6=80=81=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E8=AF=B7=E6=B1=82=E5=B7=A5=E5=85=B7=E7=B1=BB=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81jar=E5=8C=85=E4=B8=AD=E7=9A=84=E9=9D=99?= =?UTF-8?q?=E6=80=81=E8=B5=84=E6=BA=90=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/StaticResourceRequestUtil.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 basic-util/src/main/java/ink/wgink/util/request/StaticResourceRequestUtil.java diff --git a/basic-util/src/main/java/ink/wgink/util/request/StaticResourceRequestUtil.java b/basic-util/src/main/java/ink/wgink/util/request/StaticResourceRequestUtil.java new file mode 100644 index 00000000..99c70aea --- /dev/null +++ b/basic-util/src/main/java/ink/wgink/util/request/StaticResourceRequestUtil.java @@ -0,0 +1,140 @@ +package ink.wgink.util.request; + +import ink.wgink.exceptions.ParamsException; +import org.apache.commons.lang3.StringUtils; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; + +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URLEncoder; + +/** + * @ClassName: StaticResourceRequestUtil + * @Description: 静态资源请求 + * @Author: wanggeng + * @Date: 2021/9/1 10:50 上午 + * @Version: 1.0 + */ +public class StaticResourceRequestUtil { + + /** + * 下载静态资源 + * + * @param response 响应 + * @param outFile 输出的文件 + * @param outFileName 输出的名字 + */ + public static void download(HttpServletResponse response, File outFile, String outFileName) throws IOException { + if (outFile == null) { + throw new ParamsException("输出文件不能为空"); + } + if (StringUtils.isBlank(outFileName)) { + throw new ParamsException("文件不能名为空"); + } + String[] fileNameArray = outFileName.split("\\."); + if (fileNameArray.length < 1) { + throw new ParamsException("文件无后缀名"); + } + try (FileInputStream fileInputStream = new FileInputStream(outFile); + OutputStream outputStream = response.getOutputStream()) { + response.setHeader("Content-Length", String.valueOf(outFile.length())); + response.setContentType(getContentType(fileNameArray[fileNameArray.length - 1])); + response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(outFileName, "UTF-8")); + response.setHeader("Expires", DateTime.now().plusDays(7).toDateTime(DateTimeZone.forID("GMT")).toString()); + // 一小时之内不发送新请求 + response.setHeader("max-age", "3600"); + byte[] buf = new byte[1024]; + for (int readLength; (readLength = fileInputStream.read(buf)) > 0; ) { + outputStream.write(buf, 0, readLength); + } + outputStream.flush(); + } + } + + /** + * 文件类型 + * + * @param fileType + * @return + */ + public static String getContentType(String fileType) { + if (StringUtils.isBlank(fileType)) { + throw new ParamsException("文件类型不能为空"); + } + String contentType; + switch (fileType) { + case "png": + contentType = "image/png"; + break; + case "blob": + case "jpg": + case "jpeg": + contentType = "image/jpeg"; + break; + case "gif": + contentType = "image/gif"; + break; + case "mp4": + contentType = "video/mp4"; + break; + case "rmvb": + contentType = "application/vnd.rn-realmedia-vbr"; + break; + case "mp3": + contentType = "audio/mp3"; + break; + case "wmv": + contentType = "video/x-ms-wmv"; + break; + case "wav": + contentType = "audio/wav"; + break; + case "doc": + contentType = "application/msword"; + break; + case "docx": + contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; + break; + case "xls": + contentType = "application/vnd.ms-excel"; + break; + case "xlsx": + contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; + break; + case "ppt": + contentType = "application/vnd.ms-powerpoint"; + break; + case "pptx": + contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; + break; + case "txt": + contentType = "text/plain"; + break; + case "apk": + contentType = "application/vnd.android.package-archive"; + break; + case "pdf": + contentType = "application/pdf"; + break; + case "css": + contentType = "text/css"; + break; + case "js": + contentType = "application/x-javascript"; + break; + case "htm": + case "htx": + case "html": + contentType = "text/html"; + break; + default: + contentType = "application/octet-stream"; + } + return contentType; + } + +}