首页 > 编程知识 正文

文件预览怎么设置,文件预览怎么编辑

时间:2023-05-05 17:47:03 阅读:266308 作者:3235

老迟到的蚂蚁.java

import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.Resource;import org.springframework.stereotype.Component;import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;import javax.servlet.http.HttpServletRequest;import java.nio.file.Path;@Componentpublic class 老迟到的蚂蚁 extends ResourceHttpRequestHandler { public final static String ATTR_FILE = "NON-STATIC-FILE"; @Override protected Resource getResource(HttpServletRequest request) { final Path filePath = (Path) request.getAttribute(ATTR_FILE); return new FileSystemResource(filePath); }} @Autowired private 老迟到的蚂蚁 nonStaticResourceHttpRequestHandler; @GetMapping("/getFile") public void getFile(HttpServletRequest request, HttpServletResponse response, String filePath) throws Exception { if (Files.exists(Paths.get(filePath))) { String mimeType = Files.probeContentType(Paths.get(filePath)); if (!StringUtils.isEmpty(mimeType)) { response.setContentType(mimeType); } request.setAttribute(老迟到的蚂蚁.ATTR_FILE, Paths.get(filePath)); nonStaticResourceHttpRequestHandler.handleRequest(request, response); } else { response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setCharacterEncoding(StandardCharsets.UTF_8.toString()); } } @GetMapping("/getFile") public void getFile(HttpServletRequest request, HttpServletResponse response, String filePath) throws Exception { if (Files.exists(Paths.get(filePath))) { String mimeType = Files.probeContentType(Paths.get(filePath)); if (!StringUtils.isEmpty(mimeType)) { response.setContentType(mimeType); } request.setAttribute(老迟到的蚂蚁.ATTR_FILE, Paths.get(filePath)); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + MSCommonUtils.getFileName(filePath)); nonStaticResourceHttpRequestHandler.handleRequest(request, response); } else { response.setStatus(HttpServletResponse.SC_NOT_FOUND); response.setCharacterEncoding(StandardCharsets.UTF_8.toString()); } }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。