feat(修改): 修改文件下载

This commit is contained in:
Bunny 2024-10-13 02:41:13 +08:00
parent 0afaf12a81
commit e3a1f75da2
5 changed files with 66 additions and 27 deletions

View File

@ -15,9 +15,9 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
@ -53,9 +53,15 @@ public class FilesController {
}
@Operation(summary = "下载文件", description = "下载文件")
@GetMapping("downloadFiles/{fileId}")
public void downloadFiles(@PathVariable Long fileId, HttpServletResponse response) {
filesService.downloadFiles(response, fileId);
@GetMapping("downloadFilesByFileId/{fileId}")
public ResponseEntity<byte[]> downloadFilesByFileId(@PathVariable Long fileId) {
return filesService.downloadFilesByFileId(fileId);
}
@Operation(summary = "根据文件名下载文件", description = "根据文件名下载文件")
@GetMapping("downloadFilesByFilepath")
public ResponseEntity<byte[]> downloadFilesByFilepath(String filepath) {
return filesService.downloadFilesByFilepath(filepath);
}
@Operation(summary = "更新系统文件表", description = "更新系统文件表")

View File

@ -83,6 +83,7 @@ public class WebSecurityConfig {
"/", "/ws/**",
"/*/*/noAuth/**", "/*/noAuth/**", "/noAuth/**",
"/media.ico", "/favicon.ico", "*.html", "/webjars/**", "/v3/api-docs/**", "swagger-ui/**",
"/*/files/**",
"/error", "/*/i18n/getI18n",
};
return web -> web.ignoring().requestMatchers(annotations)

View File

@ -10,8 +10,8 @@ import cn.bunny.dao.vo.system.files.FileInfoVo;
import cn.bunny.dao.vo.system.files.FilesVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import java.util.List;
@ -64,8 +64,16 @@ public interface FilesService extends IService<Files> {
/**
* * 下载文件
*
* @param fileId 文件名
* @param response response
* @param fileId 文件id
* @return 文件字节数组
*/
void downloadFiles(HttpServletResponse response, Long fileId);
ResponseEntity<byte[]> downloadFilesByFileId(Long fileId);
/**
* * 下载文件
*
* @param filepath 文件路径
* @return 文件字节数组
*/
ResponseEntity<byte[]> downloadFilesByFilepath(String filepath);
}

View File

@ -18,15 +18,16 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
/**
@ -129,11 +130,11 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
/**
* * 下载文件
*
* @param response response
* @param fileId 文件名
* @param fileId 文件id
* @return 文件字节数组
*/
@Override
public void downloadFiles(HttpServletResponse response, Long fileId) {
public ResponseEntity<byte[]> downloadFilesByFileId(Long fileId) {
// 查询数据库文件信息
Files files = getOne(Wrappers.<Files>lambdaQuery().eq(Files::getId, fileId));
@ -144,18 +145,38 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
String filepath = files.getFilepath();
int end = filepath.indexOf("/", 1);
filepath = filepath.substring(end + 1);
byte[] buffer = minioUtil.getBucketObjectByte(filepath);
byte[] bytes = minioUtil.getBucketObjectByte(filepath);
// 设置响应头
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + files.getFilename() + "\"");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", files.getFilename());
// 写入字节数组到输出流
try (OutputStream os = response.getOutputStream()) {
os.write(buffer);
os.flush();
} catch (IOException exception) {
throw new BunnyException(exception.getMessage());
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}
/**
* * 下载文件
*
* @param filepath 文件路径
* @return 文件字节数组
*/
@Override
public ResponseEntity<byte[]> downloadFilesByFilepath(String filepath) {
// 截取文件路径
int start = filepath.indexOf("/", 1);
filepath = filepath.substring(start + 1);
byte[] bytes = minioUtil.getBucketObjectByte(filepath);
// 设置文件名称
int end = filepath.lastIndexOf("/");
String filename = filepath.substring(end + 1);
// 设置响应头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", filename);
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}
}

View File

@ -6,10 +6,13 @@ class FilesServiceImplTest {
@Test
void stringTest() {
String filepath = "/auth-admin/avatar/2024/10-04/5a56ad8f-4468-4780-8a61-424e7de54e04.png";
int end = filepath.indexOf("/", 1);
filepath = filepath.substring(end + 1);
int start = filepath.indexOf("/", 1);
filepath = filepath.substring(start + 1);
System.out.println(filepath);
int end = filepath.lastIndexOf("/");
String filename = filepath.substring(end + 1);
System.out.println(filename);
}
}