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) {