在JDK17下,如下代码引用了cn.hutool.extra.servlet包下的 ServletUtil工具类的ServletUtil.write方法,执行时报”Cannot resolve method 'write(jakarta.servlet.http.HttpServletResponse, java.io.File)'“BUG,download方法通过cn.hutool.extra.servlet.ServletUtil的方法
来实现文件下载的功能。
1 /** 2 * 录音下载 3 * @return 4 */ 5 @Override 6 public void download(String id, HttpServletResponse response){ 7 if (StrUtil.isEmpty(id)) { 8 throw new CrmException(CrmCodeEnum.CRM_CALL_DATA_QUERY_ERROR,"请求的数据格式"); 9 } 10 //查询通话记录 11 CallRecord callRecord = baseMapper.selectById(id); 12 if(callRecord == null){ 13 throw new CrmException(CrmCodeEnum.CRM_DATA_DELETED,"此通话记录"); 14 } 15 if (StrUtil.isEmpty(callRecord.getFilePath())){ 16 throw new CrmException(CrmCodeEnum.CRM_CALL_DOWNLOAD_ERROR); 17 } 18 if (Objects.equals(UploadFileEnum.LOCAL.getConfig(), callRecord.getCallUpload())) { 19 ServletUtil.write(response, FileUtil.file(callRecord.getFilePath())); 20 return; 21 } 22 UploadEntity entity = new UploadEntity(callRecord.getCallRecordId() + "", callRecord.getFileName(), callRecord.getSize().longValue(), callRecord.getBatchId(),"0"); 23 entity.setPath(callRecord.getFilePath()); 24 InputStream inputStream = FileServiceFactory.build().downFile(entity); 25 if (inputStream != null) { 26 final String contentType = ObjectUtil.defaultIfNull(FileUtil.getMimeType(callRecord.getFileName()), "application/octet-stream"); 27 ServletUtil.write(response, inputStream, contentType, callRecord.getFileName()); 28 } 29 }若不使用ServletUtil工具类,可使用Spring的
org.springframework.http.HttpHeaders
和org.springframework.util.StreamUtils重写它来
实现文件下载:1 import org.springframework.http.HttpHeaders; 2 import org.springframework.util.StreamUtils; 3 4 public void download(String id, HttpServletResponse response) { 5 if (StrUtil.isEmpty(id)) { 6 throw new CrmException(CrmCodeEnum.CRM_CALL_DATA_QUERY_ERROR, "请求的数据格式"); 7 } 8 // 查询通话记录 9 CallRecord callRecord = baseMapper.selectById(id); 10 if (callRecord == null) { 11 throw new CrmException(CrmCodeEnum.CRM_DATA_DELETED, "此通话记录"); 12 } 13 if (StrUtil.isEmpty(callRecord.getFilePath())) { 14 throw new CrmException(CrmCodeEnum.CRM_CALL_DOWNLOAD_ERROR); 15 } 16 if (Objects.equals(UploadFileEnum.LOCAL.getConfig(), callRecord.getCallUpload())) { 17 File file = new File(callRecord.getFilePath()); 18 try (InputStream inputStream = new FileInputStream(file)) { 19 response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + callRecord.getFileName()); 20 response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); 21 StreamUtils.copy(inputStream, response.getOutputStream()); 22 response.flushBuffer(); 23 } catch (IOException e) { 24 throw new CrmException(CrmCodeEnum.CRM_CALL_DOWNLOAD_ERROR, "文件下载失败"); 25 } 26 return; 27 } 28 UploadEntity entity = new UploadEntity(callRecord.getCallRecordId() + "", callRecord.getFileName(), callRecord.getSize().longValue(), callRecord.getBatchId(), "0"); 29 entity.setPath(callRecord.getFilePath()); 30 InputStream inputStream = FileServiceFactory.build().downFile(entity); 31 if (inputStream != null) { 32 try { 33 response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + callRecord.getFileName()); 34 response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); 35 StreamUtils.copy(inputStream, response.getOutputStream()); 36 response.flushBuffer(); 37 } catch (IOException e) { 38 throw new CrmException(CrmCodeEnum.CRM_CALL_DOWNLOAD_ERROR, "文件下载失败"); 39 } 40 } 41 }
标签:ServletUtil,cn,extra,callRecord,throw,new,response,CRM From: https://www.cnblogs.com/simyeo/p/17825901.html