feat(修改): 修改文件下载
This commit is contained in:
parent
0afaf12a81
commit
e3a1f75da2
|
@ -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.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@ -53,9 +53,15 @@ public class FilesController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "下载文件", description = "下载文件")
|
@Operation(summary = "下载文件", description = "下载文件")
|
||||||
@GetMapping("downloadFiles/{fileId}")
|
@GetMapping("downloadFilesByFileId/{fileId}")
|
||||||
public void downloadFiles(@PathVariable Long fileId, HttpServletResponse response) {
|
public ResponseEntity<byte[]> downloadFilesByFileId(@PathVariable Long fileId) {
|
||||||
filesService.downloadFiles(response, fileId);
|
return filesService.downloadFilesByFileId(fileId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "根据文件名下载文件", description = "根据文件名下载文件")
|
||||||
|
@GetMapping("downloadFilesByFilepath")
|
||||||
|
public ResponseEntity<byte[]> downloadFilesByFilepath(String filepath) {
|
||||||
|
return filesService.downloadFilesByFilepath(filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(summary = "更新系统文件表", description = "更新系统文件表")
|
@Operation(summary = "更新系统文件表", description = "更新系统文件表")
|
||||||
|
|
|
@ -83,6 +83,7 @@ public class WebSecurityConfig {
|
||||||
"/", "/ws/**",
|
"/", "/ws/**",
|
||||||
"/*/*/noAuth/**", "/*/noAuth/**", "/noAuth/**",
|
"/*/*/noAuth/**", "/*/noAuth/**", "/noAuth/**",
|
||||||
"/media.ico", "/favicon.ico", "*.html", "/webjars/**", "/v3/api-docs/**", "swagger-ui/**",
|
"/media.ico", "/favicon.ico", "*.html", "/webjars/**", "/v3/api-docs/**", "swagger-ui/**",
|
||||||
|
"/*/files/**",
|
||||||
"/error", "/*/i18n/getI18n",
|
"/error", "/*/i18n/getI18n",
|
||||||
};
|
};
|
||||||
return web -> web.ignoring().requestMatchers(annotations)
|
return web -> web.ignoring().requestMatchers(annotations)
|
||||||
|
|
|
@ -10,8 +10,8 @@ import cn.bunny.dao.vo.system.files.FileInfoVo;
|
||||||
import cn.bunny.dao.vo.system.files.FilesVo;
|
import cn.bunny.dao.vo.system.files.FilesVo;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -64,8 +64,16 @@ public interface FilesService extends IService<Files> {
|
||||||
/**
|
/**
|
||||||
* * 下载文件
|
* * 下载文件
|
||||||
*
|
*
|
||||||
* @param fileId 文件名
|
* @param fileId 文件id
|
||||||
* @param response response
|
* @return 文件字节数组
|
||||||
*/
|
*/
|
||||||
void downloadFiles(HttpServletResponse response, Long fileId);
|
ResponseEntity<byte[]> downloadFilesByFileId(Long fileId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 下载文件
|
||||||
|
*
|
||||||
|
* @param filepath 文件路径
|
||||||
|
* @return 文件字节数组
|
||||||
|
*/
|
||||||
|
ResponseEntity<byte[]> downloadFilesByFilepath(String filepath);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,15 +18,16 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.springframework.beans.BeanUtils;
|
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.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -129,11 +130,11 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, Files> implements
|
||||||
/**
|
/**
|
||||||
* * 下载文件
|
* * 下载文件
|
||||||
*
|
*
|
||||||
* @param response response
|
* @param fileId 文件id
|
||||||
* @param fileId 文件名
|
* @return 文件字节数组
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void downloadFiles(HttpServletResponse response, Long fileId) {
|
public ResponseEntity<byte[]> downloadFilesByFileId(Long fileId) {
|
||||||
// 查询数据库文件信息
|
// 查询数据库文件信息
|
||||||
Files files = getOne(Wrappers.<Files>lambdaQuery().eq(Files::getId, 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();
|
String filepath = files.getFilepath();
|
||||||
int end = filepath.indexOf("/", 1);
|
int end = filepath.indexOf("/", 1);
|
||||||
filepath = filepath.substring(end + 1);
|
filepath = filepath.substring(end + 1);
|
||||||
byte[] buffer = minioUtil.getBucketObjectByte(filepath);
|
byte[] bytes = minioUtil.getBucketObjectByte(filepath);
|
||||||
|
|
||||||
// 设置响应头
|
// 设置响应头
|
||||||
response.setContentType("application/octet-stream");
|
HttpHeaders headers = new HttpHeaders();
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + files.getFilename() + "\"");
|
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
headers.setContentDispositionFormData("attachment", files.getFilename());
|
||||||
|
|
||||||
// 写入字节数组到输出流
|
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
|
||||||
try (OutputStream os = response.getOutputStream()) {
|
|
||||||
os.write(buffer);
|
|
||||||
os.flush();
|
|
||||||
} catch (IOException exception) {
|
|
||||||
throw new BunnyException(exception.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 下载文件
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,13 @@ class FilesServiceImplTest {
|
||||||
@Test
|
@Test
|
||||||
void stringTest() {
|
void stringTest() {
|
||||||
String filepath = "/auth-admin/avatar/2024/10-04/5a56ad8f-4468-4780-8a61-424e7de54e04.png";
|
String filepath = "/auth-admin/avatar/2024/10-04/5a56ad8f-4468-4780-8a61-424e7de54e04.png";
|
||||||
int end = filepath.indexOf("/", 1);
|
int start = filepath.indexOf("/", 1);
|
||||||
|
filepath = filepath.substring(start + 1);
|
||||||
filepath = filepath.substring(end + 1);
|
|
||||||
System.out.println(filepath);
|
System.out.println(filepath);
|
||||||
|
|
||||||
|
int end = filepath.lastIndexOf("/");
|
||||||
|
String filename = filepath.substring(end + 1);
|
||||||
|
System.out.println(filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue