From 4e91931b9ee2ade1593e9e21ca714220847e9ace Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Fri, 27 Nov 2020 12:30:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BE=E7=89=87=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/file/impl/FileServiceImpl.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java index 32d33ce..3f3ac3c 100644 --- a/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java +++ b/cloud-common-plugin/src/main/java/com/cm/common/plugin/service/file/impl/FileServiceImpl.java @@ -15,7 +15,6 @@ import com.cm.common.plugin.service.file.IFileService; import com.cm.common.pojo.ListPage; import com.cm.common.pojo.dtos.FileDTO; import com.cm.common.pojo.pos.FilePO; -import com.cm.common.result.SuccessResult; import com.cm.common.result.SuccessResultData; import com.cm.common.result.SuccessResultList; import com.cm.common.utils.DateUtil; @@ -30,10 +29,11 @@ import net.coobird.thumbnailator.Thumbnails; import org.apache.catalina.connector.ClientAbortException; import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.lang3.StringUtils; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.parameters.P; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; @@ -343,6 +343,20 @@ public class FileServiceImpl extends AbstractService implements IFileService { response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); } else { response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); + // 如果是图片资源,开启缓存 + if (isImageFile(filePO.getFileType())) { + // 如果存在校验修改时间且未做修改,返回304,使用本地资源 + String ifModifiedSince = request.getHeader("If-Modified-Since"); + if (StringUtils.isNotBlank(ifModifiedSince) && StringUtils.equalsIgnoreCase(ifModifiedSince, filePO.getGmtModified())) { + response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + return; + } + // 缓存有效时间为7天 + response.setHeader("Expires", DateTime.now().plusDays(7).toDateTime(DateTimeZone.forID("GMT")).toString()); + // 60秒之内不发送新请求 + response.setHeader("max-age", "60"); + response.setHeader("Last-Modified", filePO.getGmtModified()); + } } String rangeString = null; if (canRange && request != null) {