添加文件管理功能代码
parent
079d3b0862
commit
2a8c4d6805
@ -0,0 +1,43 @@
|
|||||||
|
package com.supervision.controller;
|
||||||
|
|
||||||
|
import com.supervision.dto.R;
|
||||||
|
import com.supervision.service.FileService;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件管理
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/file")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class FileController {
|
||||||
|
|
||||||
|
private final FileService fileService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
* @param file 文件
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/upload")
|
||||||
|
public R<String> upload(@RequestPart("file") MultipartFile file) throws IOException {
|
||||||
|
|
||||||
|
String fileId = fileService.upload(file);
|
||||||
|
return R.ok(fileId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
* @param fileId
|
||||||
|
* @param response
|
||||||
|
*/
|
||||||
|
@GetMapping("/download")
|
||||||
|
public void downLoad(@RequestParam("fileId") String fileId, HttpServletResponse response) {
|
||||||
|
fileService.downLoad(fileId,response);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.supervision.service;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface FileService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
* @param file 文件
|
||||||
|
* @return 文件ID
|
||||||
|
*/
|
||||||
|
String upload(MultipartFile file) throws IOException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载文件
|
||||||
|
* @param fileId 文件ID
|
||||||
|
* @param response HTTP响应对象
|
||||||
|
*/
|
||||||
|
void downLoad(String fileId, HttpServletResponse response);
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.supervision.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import com.supervision.domain.SysByteArray;
|
||||||
|
import com.supervision.service.FileService;
|
||||||
|
import com.supervision.service.SysByteArrayService;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SysFileServiceImpl implements FileService {
|
||||||
|
|
||||||
|
private final SysByteArrayService sysByteArrayService;
|
||||||
|
@Override
|
||||||
|
public String upload(MultipartFile file) throws IOException {
|
||||||
|
|
||||||
|
SysByteArray byteArray = new SysByteArray();
|
||||||
|
byteArray.setFileName(file.getOriginalFilename());
|
||||||
|
byteArray.setBytes(file.getBytes());
|
||||||
|
byteArray.setSize(file.getSize());
|
||||||
|
byteArray.setContentType(file.getContentType());
|
||||||
|
sysByteArrayService.save(byteArray);
|
||||||
|
return byteArray.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void downLoad(String fileId, HttpServletResponse response) {
|
||||||
|
Assert.notEmpty(fileId, "文件ID不能为空");
|
||||||
|
SysByteArray byteArray = sysByteArrayService.getById(fileId);
|
||||||
|
Assert.notNull(byteArray, "文件不存在或已被删除");
|
||||||
|
response.setContentType(byteArray.getContentType());
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + byteArray.getFileName() + "\"");
|
||||||
|
try {
|
||||||
|
response.getOutputStream().write(byteArray.getBytes());
|
||||||
|
response.getOutputStream().flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("文件下载失败: {}", e.getMessage(), e);
|
||||||
|
throw new RuntimeException("文件下载失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue