Browse Source

2025.1.3 项目信息 新增模态框优化

询价 询价审批 下载文件优化
java8
yuejiayang 1 year ago
parent
commit
c5ead63fad
  1. 5
      src/main/java/com/xujie/sys/modules/oss/controller/OssController.java
  2. 3
      src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java
  3. 48
      src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java

5
src/main/java/com/xujie/sys/modules/oss/controller/OssController.java

@ -41,4 +41,9 @@ public class OssController {
public void previewOssFileById(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception {
sysOssService.previewOssFileById(id,response);
}
@PostMapping("/2/{id}")
public void previewOssFileById2(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception {
sysOssService.previewOssFileById2(id,response);
}
}

3
src/main/java/com/xujie/sys/modules/oss/service/SysOssService.java

@ -53,4 +53,7 @@ public interface SysOssService extends IService<SysOssEntity> {
List<SysOssEntity> queryOssFile(SysOssEntity oss);
void previewOssFileById(Integer id, HttpServletResponse response) throws Exception;
void previewOssFileById2(Integer id, HttpServletResponse response) throws Exception;
}

48
src/main/java/com/xujie/sys/modules/oss/service/impl/SysOssServiceImpl.java

@ -153,7 +153,53 @@ public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> impl
}
@Override
public void previewOssFileById(Integer id, HttpServletResponse response) throws Exception {
public void previewOssFileById(Integer id, HttpServletResponse response) {
SysOssEntity oss = this.getById(id);
if (Objects.isNull(oss)) {
throw new IllegalArgumentException("文件不存在");
}
// 创建文件对象
File file = new File(oss.getUrl());
if (!file.exists()) {
throw new IllegalArgumentException("文件不存在");
}
String encodedFileName;
try {
encodedFileName = URLEncoder.encode(oss.getFileName(), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("文件名编码失败", e);
}
// 设置响应头
if ("txt".equals(oss.getFileSuffix())){
response.setContentType("text/plain;charset=utf-8");
}else {
response.setContentType("application/octet-stream;charset=utf-8");
}
response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName);
try (FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
ServletOutputStream os = response.getOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
// 循环读取文件内容并写入到响应流中
while ((bytesRead = bis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw new RuntimeException("文件下载失败", e);
}
}
@Override
public void previewOssFileById2(Integer id, HttpServletResponse response) throws Exception {
SysOssEntity oss = this.getById(id);
if (Objects.isNull(oss)) {
throw new IllegalArgumentException("文件不存在");

Loading…
Cancel
Save