添加图片缓存

This commit is contained in:
wenc000 2020-11-27 12:30:58 +08:00
parent 94ac42ed23
commit 4e91931b9e

View File

@ -15,7 +15,6 @@ import com.cm.common.plugin.service.file.IFileService;
import com.cm.common.pojo.ListPage; import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.FileDTO; import com.cm.common.pojo.dtos.FileDTO;
import com.cm.common.pojo.pos.FilePO; import com.cm.common.pojo.pos.FilePO;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData; import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList; import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.DateUtil; import com.cm.common.utils.DateUtil;
@ -30,10 +29,11 @@ import net.coobird.thumbnailator.Thumbnails;
import org.apache.catalina.connector.ClientAbortException; import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile; 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")); response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8"));
} else { } else {
response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(filePO.getFileName(), "UTF-8")); 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; String rangeString = null;
if (canRange && request != null) { if (canRange && request != null) {