文件上传
This commit is contained in:
@@ -33,6 +33,10 @@
|
|||||||
<groupId>org.dromara</groupId>
|
<groupId>org.dromara</groupId>
|
||||||
<artifactId>ruoyi-common-log</artifactId>
|
<artifactId>ruoyi-common-log</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dromara</groupId>
|
||||||
|
<artifactId>ruoyi-system</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@@ -0,0 +1,81 @@
|
|||||||
|
package org.dromara.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.dromara.common.Result;
|
||||||
|
import org.dromara.common.core.exception.file.FileException;
|
||||||
|
import org.dromara.system.service.ISysOssService;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件相关的接口
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/files")
|
||||||
|
public class FileController {
|
||||||
|
// System.getProperty("user.dir") 获取到你当前项目的根路径
|
||||||
|
private static final String filePath = System.getProperty("user.dir")+"/files/";
|
||||||
|
@Resource
|
||||||
|
private ISysOssService sysOssService;
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/upload")
|
||||||
|
public Result upload(MultipartFile file){ //文件流的形式接收前端发送过来的文件
|
||||||
|
// String originalFilename = file.getOriginalFilename(); // xxx.png
|
||||||
|
// if(!FileUtil.isDirectory(filePath)){ // 如果目录不存在,需要先创建目录
|
||||||
|
// FileUtil.mkdir(filePath);//创建一个files 目录
|
||||||
|
// }
|
||||||
|
// // 提供文件存储的完整的路径
|
||||||
|
// // 给文件名加一个唯一的标识
|
||||||
|
// String fileName = System.currentTimeMillis()+ "_"+ originalFilename;
|
||||||
|
// String realPath = filePath + fileName; // 完整的文件路径
|
||||||
|
// try {
|
||||||
|
// FileUtil.writeBytes(file.getBytes(),realPath);
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// throw new FileException("500",new String[]{"文件上传失败"});
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // 返回一个网络链接
|
||||||
|
// // http://localhost:9090/files/download/xxxx.jpg
|
||||||
|
// String url = "http://localhost:8888/files/" + fileName;
|
||||||
|
// return Result.success(url);
|
||||||
|
return Result.success(sysOssService.upload(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件下载
|
||||||
|
*
|
||||||
|
* @param fileName
|
||||||
|
* @param response
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/download/{fileName}")
|
||||||
|
public Result download(@PathVariable String fileName , HttpServletResponse response){
|
||||||
|
|
||||||
|
try {
|
||||||
|
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
|
||||||
|
response.setContentType("application/octet-stream");
|
||||||
|
OutputStream os = response.getOutputStream();
|
||||||
|
String realPath = filePath + fileName; // 完整的文件路径
|
||||||
|
// 获取到文件的字节流数组
|
||||||
|
byte[] bytes = FileUtil.readBytes(realPath);
|
||||||
|
os.write(bytes);
|
||||||
|
os.flush();
|
||||||
|
os.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new FileException("500",new String[]{"文件下载失败"});
|
||||||
|
}
|
||||||
|
String url = "http://localhost:90/files/download/" + fileName;
|
||||||
|
return Result.success(url);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user