成都网站制作方案,聚名网怎么注销账号,企业信息管理系统er图,p2p金融网站开发设置Content-Disposition响应头类型 inline查看预览 #xff1b; attachment下载#xff1b; inline#xff1a;表示回复中的消息体会以页面的一部分或者整个页面的形式展示
attchment#xff1a;以附件形式被下载到本地#xff1b;/*** 文件或图…设置Content-Disposition响应头类型 inline查看预览 attachment下载 inline表示回复中的消息体会以页面的一部分或者整个页面的形式展示
attchment以附件形式被下载到本地/*** 文件或图片预览/下载工具类* author zh、* data 2024/1/11 18:35*/
Component
Slf4j
public class FileHttpUtil {/*** 根据物理路径文件 获取 下载/预览 文件* param file 文件* param type 设置响应头类型 inline查看 attachment下载* param fileName 文件名 * return 对应类型响应文件*/public static ResponseEntity? getResponseEntity(byte[] file , String type , String fileName ){ResponseEntity.BodyBuilder responseEntity ResponseEntity.ok();HttpHeaders httpHeaders new HttpHeaders();Tika tika new Tika();String mediaType tika.detect(file);httpHeaders.setContentType(MediaType.parseMediaType(mediaType));httpHeaders.setContentDisposition(ContentDisposition.builder(type).filename(URLEncoder.encode(fileName )).build());httpHeaders.setCacheControl(CacheControl.noCache());//httpHeaders.setCacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES));return responseEntity.headers(httpHeaders).body(file );}需要的pom依赖文件dependencygroupIdorg.apache.tika/groupIdartifactIdtika-core/artifactIdversion1.28.4/version/dependency
接口调用或测试 /*** 查询文件* param filePath文件地址 物理路径* param type 设置响应头类型 inline查看 attachment下载* return 响应文件* throws IOException*/GetMapping(value /file)public ResponseEntity? file(String filePath,String type){//根据文件路径去文件服务获取文件File file new File(filePath);try (FileInputStream fileInputStream new FileInputStream(file)) {byte[] buf new byte[fileInputStream.available()];fileInputStream.read(buf);return FileHttpUtil.getResponseEntity(buf, type,file .getName());} catch (IOException e) {e.printStackTrace();}}