修改上传文件目录配置,使其支持使用相对路径
This commit is contained in:
@@ -1,12 +1,12 @@
|
|||||||
package cn.somkit.fmt.action;
|
package cn.somkit.fmt.action;
|
||||||
|
|
||||||
import cn.somkit.fmt.annotation.ApiOperate;
|
import cn.somkit.fmt.annotation.ApiOperate;
|
||||||
|
import cn.somkit.fmt.config.FmtConfig;
|
||||||
import cn.somkit.fmt.config.RocksDBConfig;
|
import cn.somkit.fmt.config.RocksDBConfig;
|
||||||
import cn.somkit.fmt.utils.MD5Utils;
|
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 cn.somkit.fmt.utils.RocksDBUtils;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
@@ -30,19 +30,11 @@ import java.util.zip.ZipOutputStream;
|
|||||||
@RequestMapping("/download")
|
@RequestMapping("/download")
|
||||||
public class DownloadAction {
|
public class DownloadAction {
|
||||||
|
|
||||||
@Value("${somkit.upload.path.windows}")
|
|
||||||
private String windows_path;
|
|
||||||
|
|
||||||
@Value("${somkit.upload.path.linux}")
|
|
||||||
private String linux_path;
|
|
||||||
|
|
||||||
@GetMapping("/index")
|
@GetMapping("/index")
|
||||||
@ApiOperate(description = "下载页面跳转")
|
@ApiOperate(description = "下载页面跳转")
|
||||||
public ModelAndView index(String keyboard) throws Exception{
|
public ModelAndView index(String keyboard) throws Exception{
|
||||||
String path = OsInfoUtil.isWindows() ? windows_path :
|
PathUtils.directory(FmtConfig.Upload_File_Path);
|
||||||
OsInfoUtil.isLinux() ? linux_path : null;
|
File folder = new File(FmtConfig.Upload_File_Path);
|
||||||
assert path != null;
|
|
||||||
File folder = new File(path);
|
|
||||||
File[] listOfFiles = folder.listFiles();
|
File[] listOfFiles = folder.listFiles();
|
||||||
List<Map<String, Object>> list = new ArrayList<>();
|
List<Map<String, Object>> list = new ArrayList<>();
|
||||||
List<String> keys = RocksDBUtils.getAllKey(RocksDBConfig.RocksDB_Column_Family);
|
List<String> keys = RocksDBUtils.getAllKey(RocksDBConfig.RocksDB_Column_Family);
|
||||||
@@ -132,18 +124,16 @@ public class DownloadAction {
|
|||||||
@ApiOperate(description = "批量文件打包下载")
|
@ApiOperate(description = "批量文件打包下载")
|
||||||
public Map<String, Object> packZip(String filenames) throws Exception{
|
public Map<String, Object> packZip(String filenames) throws Exception{
|
||||||
try {
|
try {
|
||||||
String path = OsInfoUtil.isWindows() ? windows_path :
|
PathUtils.directory(FmtConfig.Upload_File_Path);
|
||||||
OsInfoUtil.isLinux() ? linux_path : null;
|
|
||||||
assert path != null;
|
|
||||||
//临时文件目录
|
//临时文件目录
|
||||||
String temp = "temp";
|
String temp = "temp";
|
||||||
String zipPath = path + File.separator + temp;
|
String zipPath = FmtConfig.Upload_File_Path + File.separator + temp;
|
||||||
File zipFile = new File(zipPath);
|
File zipFile = new File(zipPath);
|
||||||
if (!zipFile.exists()) zipFile.mkdirs();
|
if (!zipFile.exists()) zipFile.mkdirs();
|
||||||
String zipName = String.valueOf(System.currentTimeMillis());
|
String zipName = String.valueOf(System.currentTimeMillis());
|
||||||
String zipDir = zipPath + File.separator + zipName + ".zip";
|
String zipDir = zipPath + File.separator + zipName + ".zip";
|
||||||
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipDir));
|
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipDir));
|
||||||
File folder = new File(path);
|
File folder = new File(FmtConfig.Upload_File_Path);
|
||||||
File[] listOfFiles = folder.listFiles();
|
File[] listOfFiles = folder.listFiles();
|
||||||
assert listOfFiles != null;
|
assert listOfFiles != null;
|
||||||
for (File file : listOfFiles) {
|
for (File file : listOfFiles) {
|
||||||
@@ -185,10 +175,8 @@ public class DownloadAction {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ApiOperate(description = "批量文件删除")
|
@ApiOperate(description = "批量文件删除")
|
||||||
public String batchDel(String filenames) throws Exception{
|
public String batchDel(String filenames) throws Exception{
|
||||||
String path = OsInfoUtil.isWindows() ? windows_path :
|
PathUtils.directory(FmtConfig.Upload_File_Path);
|
||||||
OsInfoUtil.isLinux() ? linux_path : null;
|
File folder = new File(FmtConfig.Upload_File_Path);
|
||||||
assert path != null;
|
|
||||||
File folder = new File(path);
|
|
||||||
File[] listOfFiles = folder.listFiles();
|
File[] listOfFiles = folder.listFiles();
|
||||||
assert listOfFiles != null;
|
assert listOfFiles != null;
|
||||||
for (File file : listOfFiles) {
|
for (File file : listOfFiles) {
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package cn.somkit.fmt.action;
|
package cn.somkit.fmt.action;
|
||||||
|
|
||||||
import cn.somkit.fmt.annotation.ApiOperate;
|
import cn.somkit.fmt.annotation.ApiOperate;
|
||||||
|
import cn.somkit.fmt.config.FmtConfig;
|
||||||
import cn.somkit.fmt.config.RocksDBConfig;
|
import cn.somkit.fmt.config.RocksDBConfig;
|
||||||
import cn.somkit.fmt.utils.MD5Utils;
|
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 cn.somkit.fmt.utils.RocksDBUtils;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@@ -23,12 +23,6 @@ import java.util.*;
|
|||||||
@RequestMapping("/upload")
|
@RequestMapping("/upload")
|
||||||
public class UploadAction {
|
public class UploadAction {
|
||||||
|
|
||||||
@Value("${somkit.upload.path.windows}")
|
|
||||||
private String windows_path;
|
|
||||||
|
|
||||||
@Value("${somkit.upload.path.linux}")
|
|
||||||
private String linux_path;
|
|
||||||
|
|
||||||
@GetMapping("/index")
|
@GetMapping("/index")
|
||||||
@ApiOperate(description = "上传页面跳转")
|
@ApiOperate(description = "上传页面跳转")
|
||||||
public String index() throws Exception{
|
public String index() throws Exception{
|
||||||
@@ -41,11 +35,12 @@ public class UploadAction {
|
|||||||
public Map<String, Object> execute(HttpServletRequest request) throws Exception{
|
public Map<String, Object> execute(HttpServletRequest request) throws Exception{
|
||||||
//多个文件上传 就只是简单的多文件上传保存在本地的磁盘
|
//多个文件上传 就只是简单的多文件上传保存在本地的磁盘
|
||||||
if (request instanceof MultipartHttpServletRequest mrequest) {
|
if (request instanceof MultipartHttpServletRequest mrequest) {
|
||||||
|
PathUtils.directory(FmtConfig.Upload_File_Path);
|
||||||
List<MultipartFile> files = mrequest.getFiles("file");
|
List<MultipartFile> files = mrequest.getFiles("file");
|
||||||
// 取出每一个上传文件
|
// 取出每一个上传文件
|
||||||
for (MultipartFile file : files) {
|
for (MultipartFile file : files) {
|
||||||
try {
|
try {
|
||||||
this.saveFile(file); // 保存上传信息
|
this.saveFile(file);// 保存上传信息
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -63,16 +58,13 @@ public class UploadAction {
|
|||||||
* @throws Exception 上传异常
|
* @throws Exception 上传异常
|
||||||
*/
|
*/
|
||||||
public void saveFile(MultipartFile file) 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 = "";
|
String filePath = "";
|
||||||
if (file != null && file.getSize() > 0) { // 有文件上传
|
if (file != null && file.getSize() > 0) {// 有文件上传
|
||||||
String fileName = verify(file, null);// 创建文件名称
|
String fileName = verify(file, null);// 创建文件名称
|
||||||
if(StringUtils.hasText(fileName)){
|
if(StringUtils.hasText(fileName)){
|
||||||
String hash = MD5Utils.md5HashCode(file.getInputStream());
|
String hash = MD5Utils.md5HashCode(file.getInputStream());
|
||||||
RocksDBUtils.put(RocksDBConfig.RocksDB_Column_Family, fileName, hash);
|
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 saveFile = new File(filePath) ;
|
||||||
file.transferTo(saveFile); // 文件保存
|
file.transferTo(saveFile); // 文件保存
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/main/java/cn/somkit/fmt/config/FmtConfig.java
Normal file
28
src/main/java/cn/somkit/fmt/config/FmtConfig.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,9 +18,7 @@ spring:
|
|||||||
|
|
||||||
somkit:
|
somkit:
|
||||||
upload:
|
upload:
|
||||||
path:
|
path: ./data/files #上传文件路径 可以使用绝对路径,也可使用相对路径
|
||||||
windows: D://data/install/upload
|
|
||||||
linux: /mnt/files
|
|
||||||
cache:
|
cache:
|
||||||
rocks-db:
|
rocks-db:
|
||||||
path: ./cache/rocksdb # RocksDB缓存路径 可以使用绝对路径,也可使用相对路径
|
path: ./cache/rocksdb # RocksDB缓存路径 可以使用绝对路径,也可使用相对路径
|
||||||
|
|||||||
@@ -20,4 +20,5 @@
|
|||||||
> v1.3.0
|
> v1.3.0
|
||||||
```
|
```
|
||||||
修改RocksDB文件目录配置,使其支持使用相对路径
|
修改RocksDB文件目录配置,使其支持使用相对路径
|
||||||
|
修改上传文件目录配置,使其支持使用相对路径
|
||||||
```
|
```
|
||||||
Reference in New Issue
Block a user