改了一下下载路径
This commit is contained in:
parent
9afac81d92
commit
1c7695c067
|
|
@ -36,7 +36,7 @@ Node 安装 18 及以上
|
||||||
4. 导出功能的依赖
|
4. 导出功能的依赖
|
||||||
```shell
|
```shell
|
||||||
npm install xlsx file-saver
|
npm install xlsx file-saver
|
||||||
5. 展示图标的依赖
|
5. 展示图表的依赖
|
||||||
```shell
|
```shell
|
||||||
npm install echarts
|
npm install echarts
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,13 @@ export default {
|
||||||
responseType: 'blob',
|
responseType: 'blob',
|
||||||
params: config,
|
params: config,
|
||||||
}),
|
}),
|
||||||
|
downloadAttachment: (config) =>
|
||||||
|
request({
|
||||||
|
url: '/downloadFile/downloadAttachment',
|
||||||
|
method: 'get',
|
||||||
|
responseType: 'blob',
|
||||||
|
params: config,
|
||||||
|
}),
|
||||||
getAssistantByClassIdPage: (data) => request.post('/adminusers/getAssistantByClassIdPage', data),
|
getAssistantByClassIdPage: (data) => request.post('/adminusers/getAssistantByClassIdPage', data),
|
||||||
deleteClassAssistant: (data) => request.post('/assistantClass/deleteClassAssistant', data),
|
deleteClassAssistant: (data) => request.post('/assistantClass/deleteClassAssistant', data),
|
||||||
findAdminuserByAccount: (data) =>
|
findAdminuserByAccount: (data) =>
|
||||||
|
|
|
||||||
|
|
@ -143,7 +143,7 @@ const problemReviewValue=ref({
|
||||||
|
|
||||||
const down = async() =>{
|
const down = async() =>{
|
||||||
try {
|
try {
|
||||||
const res = await api.download({
|
const res = await api.downloadAttachment({
|
||||||
fileName: problemReviewValue.value.attachment
|
fileName: problemReviewValue.value.attachment
|
||||||
})
|
})
|
||||||
const contentDisposition = res.headers["content-disposition"]
|
const contentDisposition = res.headers["content-disposition"]
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
package com.example.aes.user.controller;
|
package com.example.aes.user.controller;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import java.nio.file.Files;
|
||||||
import cn.hutool.core.io.resource.UrlResource;
|
import java.nio.file.Path;
|
||||||
|
import org.springframework.core.io.UrlResource;
|
||||||
import com.example.aes.global.model.RespBean;
|
import com.example.aes.global.model.RespBean;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
|
@ -86,6 +87,41 @@ public class DownLoadFileController {
|
||||||
return RespBean.ok("importClassStudentModel");
|
return RespBean.ok("importClassStudentModel");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/downloadAttachment")
|
||||||
|
public RespBean downloadAttachment(String fileName, HttpServletResponse response) throws IOException {
|
||||||
|
// 设置响应头
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||||
|
|
||||||
|
// 构建文件路径(根目录下的 upload 目录)
|
||||||
|
String root = System.getProperty("user.dir"); // 获取项目根目录
|
||||||
|
String filePath = root + File.separator + "upload" + File.separator + fileName;
|
||||||
|
File file = new File(filePath);
|
||||||
|
|
||||||
|
// 检查文件是否存在
|
||||||
|
if (!file.exists()) {
|
||||||
|
// 返回错误信息
|
||||||
|
response.setStatus(HttpServletResponse.SC_NOT_FOUND); // 设置 HTTP 状态码为 404
|
||||||
|
OutputStream outputStream = response.getOutputStream();
|
||||||
|
outputStream.write(("文件未找到: " + fileName).getBytes());
|
||||||
|
outputStream.flush();
|
||||||
|
outputStream.close();
|
||||||
|
return RespBean.error("文件未找到");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下载文件
|
||||||
|
try (InputStream inputStream = new FileInputStream(file);
|
||||||
|
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
|
||||||
|
OutputStream outputStream = response.getOutputStream()) {
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
|
||||||
|
outputStream.write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
outputStream.flush();
|
||||||
|
}
|
||||||
|
return RespBean.ok(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/downloadTestOut")
|
@GetMapping("/downloadTestOut")
|
||||||
public RespBean downloadTestOut(String fileNameOut, String fileNameIn, HttpServletResponse response) throws IOException{
|
public RespBean downloadTestOut(String fileNameOut, String fileNameIn, HttpServletResponse response) throws IOException{
|
||||||
String prefix = "dhuoj://";
|
String prefix = "dhuoj://";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue