springmvc-lastModify支持

  1. 示例代码如下
@Component("/file/download")
public class FileDownloadHandler extends AbstractController implements LastModified {

    private Map<String, Long> cacheMap = new ConcurrentHashMap<>() ;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");
        //设置响应头,浏览器作缓存
        response.setHeader("Cache-Control", "public, max-age=60, must-revalidate");
        response.setContentType("application/octet-stream");
        // todo 业务逻辑处理,比如将文件输出到流中
        this.doBusi(response.getWriter(), code);
        outputStream.close();
        return null;
    }

    // 业务逻辑处理
    private void doBusi(PrintWriter writer, String code){
        // todo 业务逻辑处理,比如将文件输出到流中
        writer.write("

hello world

"
); } @Override public long getLastModified(HttpServletRequest request) { String code = request.getParameter("code"); cacheMap.computeIfAbsent(code, key -> this.toEpochMilli(LocalDateTime.now())) ; return cacheMap.get(code); } public static long toEpochMilli(LocalDateTime localDateTime) { return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(); } }

你可能感兴趣的:(spring,缓存,java)