修改上传文件目录配置,使其支持使用相对路径

This commit is contained in:
WIN-VSNMD38DUOC\Administrator
2024-04-28 16:50:07 +08:00
parent 3e8bc50d1b
commit 5f3e17f0cf
5 changed files with 45 additions and 38 deletions

View File

@@ -1,12 +1,12 @@
package cn.somkit.fmt.action;
import cn.somkit.fmt.annotation.ApiOperate;
import cn.somkit.fmt.config.FmtConfig;
import cn.somkit.fmt.config.RocksDBConfig;
import cn.somkit.fmt.utils.MD5Utils;
import cn.somkit.fmt.utils.OsInfoUtil;
import cn.somkit.fmt.utils.PathUtils;
import cn.somkit.fmt.utils.RocksDBUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
@@ -30,19 +30,11 @@ import java.util.zip.ZipOutputStream;
@RequestMapping("/download")
public class DownloadAction {
@Value("${somkit.upload.path.windows}")
private String windows_path;
@Value("${somkit.upload.path.linux}")
private String linux_path;
@GetMapping("/index")
@ApiOperate(description = "下载页面跳转")
public ModelAndView index(String keyboard) throws Exception{
String path = OsInfoUtil.isWindows() ? windows_path :
OsInfoUtil.isLinux() ? linux_path : null;
assert path != null;
File folder = new File(path);
PathUtils.directory(FmtConfig.Upload_File_Path);
File folder = new File(FmtConfig.Upload_File_Path);
File[] listOfFiles = folder.listFiles();
List<Map<String, Object>> list = new ArrayList<>();
List<String> keys = RocksDBUtils.getAllKey(RocksDBConfig.RocksDB_Column_Family);
@@ -132,18 +124,16 @@ public class DownloadAction {
@ApiOperate(description = "批量文件打包下载")
public Map<String, Object> packZip(String filenames) throws Exception{
try {
String path = OsInfoUtil.isWindows() ? windows_path :
OsInfoUtil.isLinux() ? linux_path : null;
assert path != null;
PathUtils.directory(FmtConfig.Upload_File_Path);
//临时文件目录
String temp = "temp";
String zipPath = path + File.separator + temp;
String zipPath = FmtConfig.Upload_File_Path + File.separator + temp;
File zipFile = new File(zipPath);
if (!zipFile.exists()) zipFile.mkdirs();
String zipName = String.valueOf(System.currentTimeMillis());
String zipDir = zipPath + File.separator + zipName + ".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipDir));
File folder = new File(path);
File folder = new File(FmtConfig.Upload_File_Path);
File[] listOfFiles = folder.listFiles();
assert listOfFiles != null;
for (File file : listOfFiles) {
@@ -185,10 +175,8 @@ public class DownloadAction {
@ResponseBody
@ApiOperate(description = "批量文件删除")
public String batchDel(String filenames) throws Exception{
String path = OsInfoUtil.isWindows() ? windows_path :
OsInfoUtil.isLinux() ? linux_path : null;
assert path != null;
File folder = new File(path);
PathUtils.directory(FmtConfig.Upload_File_Path);
File folder = new File(FmtConfig.Upload_File_Path);
File[] listOfFiles = folder.listFiles();
assert listOfFiles != null;
for (File file : listOfFiles) {

View File

@@ -1,12 +1,12 @@
package cn.somkit.fmt.action;
import cn.somkit.fmt.annotation.ApiOperate;
import cn.somkit.fmt.config.FmtConfig;
import cn.somkit.fmt.config.RocksDBConfig;
import cn.somkit.fmt.utils.MD5Utils;
import cn.somkit.fmt.utils.OsInfoUtil;
import cn.somkit.fmt.utils.PathUtils;
import cn.somkit.fmt.utils.RocksDBUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
@@ -23,12 +23,6 @@ import java.util.*;
@RequestMapping("/upload")
public class UploadAction {
@Value("${somkit.upload.path.windows}")
private String windows_path;
@Value("${somkit.upload.path.linux}")
private String linux_path;
@GetMapping("/index")
@ApiOperate(description = "上传页面跳转")
public String index() throws Exception{
@@ -41,11 +35,12 @@ public class UploadAction {
public Map<String, Object> execute(HttpServletRequest request) throws Exception{
//多个文件上传 就只是简单的多文件上传保存在本地的磁盘
if (request instanceof MultipartHttpServletRequest mrequest) {
PathUtils.directory(FmtConfig.Upload_File_Path);
List<MultipartFile> files = mrequest.getFiles("file");
// 取出每一个上传文件
for (MultipartFile file : files) {
try {
this.saveFile(file); // 保存上传信息
this.saveFile(file);// 保存上传信息
} catch (Exception e) {
e.printStackTrace();
}
@@ -63,16 +58,13 @@ public class UploadAction {
* @throws Exception 上传异常
*/
public void saveFile(MultipartFile file) throws Exception {
String path = OsInfoUtil.isWindows() ? windows_path :
OsInfoUtil.isLinux() ? linux_path : null;
assert path != null;
String filePath = "";
if (file != null && file.getSize() > 0) { // 有文件上传
if (file != null && file.getSize() > 0) {// 有文件上传
String fileName = verify(file, null);// 创建文件名称
if(StringUtils.hasText(fileName)){
String hash = MD5Utils.md5HashCode(file.getInputStream());
RocksDBUtils.put(RocksDBConfig.RocksDB_Column_Family, fileName, hash);
filePath = path + File.separator + fileName;
filePath = FmtConfig.Upload_File_Path + File.separator + fileName;
File saveFile = new File(filePath) ;
file.transferTo(saveFile); // 文件保存
}

View File

@@ -0,0 +1,28 @@
package cn.somkit.fmt.config;
import cn.somkit.fmt.utils.PathUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
/**
* 系统参数配置
*/
@Component
public class FmtConfig {
public static String System_Cf_Name = "system";
public static String Upload_File_Path = "./data/files";
@Value("${somkit.upload.path}")
public void setUpload_File_Path(String upload_file_path) {
if(StringUtils.hasText(upload_file_path)){
//如果使用相对路径,转换为绝对路径
if(upload_file_path.startsWith("./")){
upload_file_path = PathUtils.resolve(upload_file_path);
}
Upload_File_Path = upload_file_path;
}
}
}

View File

@@ -18,9 +18,7 @@ spring:
somkit:
upload:
path:
windows: D://data/install/upload
linux: /mnt/files
path: ./data/files #上传文件路径 可以使用绝对路径,也可使用相对路径
cache:
rocks-db:
path: ./cache/rocksdb # RocksDB缓存路径 可以使用绝对路径,也可使用相对路径

View File

@@ -20,4 +20,5 @@
> v1.3.0
```
修改RocksDB文件目录配置使其支持使用相对路径
修改上传文件目录配置,使其支持使用相对路径
```