[新增功能](hyx_changeFileUpload_2024-9-10)
ClearFilesJob.java - 定时任务改为每日清理30天前的临时文件 DateUtil.java - 新增 获取30天之前的日期字符串 方法hyx_changeFileUpload_2024-9-10
parent
785cb0b4e9
commit
876b63f0e6
|
@ -81,36 +81,22 @@ public class ImgFilesController extends BaseController {
|
|||
public Object finish(
|
||||
@RequestParam(value="FFILE",required=false) MultipartFile[] files,
|
||||
@RequestParam(value="TYPE",required=false) String TYPE,
|
||||
@RequestParam(value="FOREIGN_KEY",required=false) String FOREIGN_KEY
|
||||
@RequestParam(value="FOREIGN_KEY",required=false) String FOREIGN_KEY,
|
||||
@RequestParam(value="temporaryFilePath",required=false) String[] temporaryFilePath
|
||||
) throws Exception{
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
System.out.println("+++++++++++++++"+files);
|
||||
System.out.println("==================="+temporaryFilePath);
|
||||
String errInfo = "success";
|
||||
if (files != null && files.length > 0) {
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
String suffixName = files[i].getOriginalFilename().substring(files[i].getOriginalFilename().lastIndexOf(".")+1).toLowerCase();
|
||||
if (temporaryFilePath != null && temporaryFilePath.length > 0) {
|
||||
for (int i = 0; i < temporaryFilePath.length; i++) {
|
||||
String suffixName = temporaryFilePath[i].substring(temporaryFilePath[i].lastIndexOf(".")+1).toLowerCase();
|
||||
if (!"json".equals(suffixName) && !"pdf".equals(suffixName) && !"jpg".equals(suffixName) && !"jpeg".equals(suffixName) && !"png".equals(suffixName) && !"mp4".equals(suffixName)) {
|
||||
errInfo = "fail";
|
||||
map.put("result", errInfo);
|
||||
map.put("msg", "文件格式不正确!");
|
||||
return map;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < files.length; i++) {
|
||||
MultipartFile file = files[i];
|
||||
// 保存文件
|
||||
File tempFile = new File(file.getOriginalFilename());
|
||||
FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);
|
||||
if(!FileUpload.isImage(tempFile) && !TYPE.equals("2")) {//四色图改为json文件,不用判断类型
|
||||
tempFile.delete();
|
||||
map.put("result", "failed");
|
||||
map.put("exception", "上传图片格式不正确,请重新上传");
|
||||
return map;
|
||||
}
|
||||
if (tempFile.exists()) {
|
||||
tempFile.delete();
|
||||
}
|
||||
|
||||
if(TYPE.equals("2")) {//先删除原四色图
|
||||
PageData pd2 = new PageData();
|
||||
pd2.put("FOREIGN_KEY",FOREIGN_KEY);
|
||||
|
@ -122,20 +108,65 @@ public class ImgFilesController extends BaseController {
|
|||
imgfilesService.delete(pageData);
|
||||
}
|
||||
}
|
||||
String ffile = DateUtil.getDays();
|
||||
// String filePath = PathUtil.getProjectpath() + Const.FILEPATHFILE + ffile; //文件上传路径
|
||||
// String fileName = FileUpload.fileUp(file, filePath, this.get32UUID()); //执行上传
|
||||
String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
||||
Smb.sshSftp(file, fileName,Const.FILEPATHYHTP + Jurisdiction.getCORPINFO_ID() + "/" + ffile);
|
||||
|
||||
// String productPath = FileUploadUtil.copyTemporaryFileToProduct(temporaryFilePath[i]);
|
||||
PageData pd = new PageData();
|
||||
pd.put("IMGFILES_ID", this.get32UUID());
|
||||
pd.put("FILEPATH", Const.FILEPATHYHTP + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName);
|
||||
pd.put("FILEPATH", temporaryFilePath[i]);
|
||||
pd.put("TYPE", TYPE);
|
||||
pd.put("FOREIGN_KEY", FOREIGN_KEY);
|
||||
imgfilesService.save(pd);
|
||||
}
|
||||
}
|
||||
|
||||
// if (files != null && files.length > 0) {
|
||||
// for (int i = 0; i < files.length; i++) {
|
||||
// String suffixName = files[i].getOriginalFilename().substring(files[i].getOriginalFilename().lastIndexOf(".")+1).toLowerCase();
|
||||
// if (!"json".equals(suffixName) && !"pdf".equals(suffixName) && !"jpg".equals(suffixName) && !"jpeg".equals(suffixName) && !"png".equals(suffixName) && !"mp4".equals(suffixName)) {
|
||||
// errInfo = "fail";
|
||||
// map.put("result", errInfo);
|
||||
// map.put("msg", "文件格式不正确!");
|
||||
// return map;
|
||||
// }
|
||||
// }
|
||||
// for (int i = 0; i < files.length; i++) {
|
||||
// MultipartFile file = files[i];
|
||||
//// 保存文件
|
||||
// File tempFile = new File(file.getOriginalFilename());
|
||||
// FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);
|
||||
// if(!FileUpload.isImage(tempFile) && !TYPE.equals("2")) {//四色图改为json文件,不用判断类型
|
||||
// tempFile.delete();
|
||||
// map.put("result", "failed");
|
||||
// map.put("exception", "上传图片格式不正确,请重新上传");
|
||||
// return map;
|
||||
// }
|
||||
// if (tempFile.exists()) {
|
||||
// tempFile.delete();
|
||||
// }
|
||||
//
|
||||
// if(TYPE.equals("2")) {//先删除原四色图
|
||||
// PageData pd2 = new PageData();
|
||||
// pd2.put("FOREIGN_KEY",FOREIGN_KEY);
|
||||
// pd2.put("TYPE",TYPE);
|
||||
// List<PageData> four = imgfilesService.listAll(pd2);
|
||||
// for (PageData pageData : four) {
|
||||
// File old = new File(PathUtil.getProjectpath()+pageData.getString("FILEPATH"));
|
||||
// old.delete();
|
||||
// imgfilesService.delete(pageData);
|
||||
// }
|
||||
// }
|
||||
// String ffile = DateUtil.getDays();
|
||||
//// String filePath = PathUtil.getProjectpath() + Const.FILEPATHFILE + ffile; //文件上传路径
|
||||
//// String fileName = FileUpload.fileUp(file, filePath, this.get32UUID()); //执行上传
|
||||
// String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
||||
// Smb.sshSftp(file, fileName,Const.FILEPATHYHTP + Jurisdiction.getCORPINFO_ID() + "/" + ffile);
|
||||
// PageData pd = new PageData();
|
||||
// pd.put("IMGFILES_ID", this.get32UUID());
|
||||
// pd.put("FILEPATH", Const.FILEPATHYHTP + Jurisdiction.getCORPINFO_ID() + "/" + ffile + "/" + fileName);
|
||||
// pd.put("TYPE", TYPE);
|
||||
// pd.put("FOREIGN_KEY", FOREIGN_KEY);
|
||||
// imgfilesService.save(pd);
|
||||
// }
|
||||
// }
|
||||
map.put("result", errInfo);
|
||||
return map;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
package com.zcloud.controller.filemanager;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.zcloud.service.bus.ImgFilesService;
|
||||
import com.zcloud.util.*;
|
||||
import net.sf.json.JSONArray;
|
||||
|
||||
import java.io.Console;
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -11,6 +14,7 @@ import java.util.Map;
|
|||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
@ -41,6 +45,9 @@ public class MfolderController extends BaseController {
|
|||
@Autowired
|
||||
private MfolderService mfolderService;
|
||||
|
||||
@Autowired
|
||||
private ImgFilesService imgfilesService;
|
||||
|
||||
/**创建目录
|
||||
* @param
|
||||
* @throws Exception
|
||||
|
@ -73,19 +80,32 @@ public class MfolderController extends BaseController {
|
|||
* @date 2024-9-10
|
||||
* @author hyx
|
||||
*/
|
||||
@RequestMapping("upLoadTemporary")
|
||||
@RequestMapping("/upLoadTemporary")
|
||||
@ResponseBody
|
||||
public Object upLoadTemporary(@RequestParam(value="FFILE",required=false) MultipartFile file) throws Exception{
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
public Object upLoadTemporary(@RequestParam(value="FFILE",required=false) MultipartFile file) throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
String temporaryFilePath = null;
|
||||
if (null != file && !file.isEmpty()) {
|
||||
|
||||
String suffixName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
|
||||
if (!"pdf".equals(suffixName) && !"jpg".equals(suffixName) && !"jpeg".equals(suffixName) && !"png".equals(suffixName) &&
|
||||
!"mp4".equals(suffixName) && !"json".equals(suffixName) && !"pdf".equals(suffixName) && !"jpg".equals(suffixName) &&
|
||||
!"jpeg".equals(suffixName) && !"png".equals(suffixName) && !"mp4".equals(suffixName)) {
|
||||
map.put("result", "fail");
|
||||
map.put("msg", "文件格式不正确!");
|
||||
return map;
|
||||
}
|
||||
|
||||
String CORPINFO_ID = Jurisdiction.getCORPINFO_ID();
|
||||
String temporaryFilePath = FileUploadUtil.uploadTemporaryFile(file, CORPINFO_ID);
|
||||
|
||||
temporaryFilePath = FileUploadUtil.uploadTemporaryFile(file, CORPINFO_ID);
|
||||
map.put("result", "success");
|
||||
map.put("temporaryFilePath", temporaryFilePath);
|
||||
return map;
|
||||
}else {
|
||||
map.put("result", "fail");
|
||||
map.put("msg","文件为空");
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -99,17 +119,57 @@ public class MfolderController extends BaseController {
|
|||
* @date 2024-9-10
|
||||
* @author hyx
|
||||
*/
|
||||
@RequestMapping("saveFile")
|
||||
@ResponseBody
|
||||
public Object saveFile(@RequestParam(value="temporaryFilePath",required=false) String temporaryFilePath,
|
||||
@RequestParam(value="NAME",required=false) String NAME,
|
||||
@RequestParam(value="PARENT_ID",required=false) String PARENT_ID,
|
||||
@RequestParam(value="REMARKS",required=false) String REMARKS,
|
||||
@RequestParam(value="SHARE",required=false) String SHARE
|
||||
@RequestParam(value="SHARE",required=false) String SHARE,
|
||||
@RequestParam(value="TYPE",required=false) String TYPE,
|
||||
@RequestParam(value="FOREIGN_KEY",required=false) String FOREIGN_KEY
|
||||
) throws Exception {
|
||||
Map<String,Object> map = new HashMap<String,Object>();
|
||||
if (ObjectUtil.hasEmpty(temporaryFilePath)){
|
||||
map.put("result", "fail");
|
||||
map.put("msg","缺少必要参数");
|
||||
return map;
|
||||
}
|
||||
PageData pd = new PageData();
|
||||
String productFilePath = FileUploadUtil.copyTemporaryFileToProduct(temporaryFilePath, Jurisdiction.getCORPINFO_ID());
|
||||
|
||||
return null;
|
||||
//如果上传文件为图片 -> 保存图片流程
|
||||
if (ObjectUtil.hasEmpty(TYPE,FOREIGN_KEY)){
|
||||
if ("2".equals(TYPE)) {
|
||||
pd.put("FOREIGN_KEY",FOREIGN_KEY);
|
||||
pd.put("TYPE",TYPE);
|
||||
List<PageData> four = imgfilesService.listAll(pd);
|
||||
for (PageData pageData : four) {
|
||||
File old = new File(PathUtil.getProjectpath()+pageData.getString("FILEPATH"));
|
||||
old.delete();
|
||||
imgfilesService.delete(pageData);
|
||||
}
|
||||
}
|
||||
pd.put("IMGFILES_ID", this.get32UUID());
|
||||
pd.put("FILEPATH", productFilePath);
|
||||
pd.put("TYPE", TYPE);
|
||||
pd.put("FOREIGN_KEY", FOREIGN_KEY);
|
||||
imgfilesService.save(pd);
|
||||
}else { //保存企业文件流程
|
||||
pd.put("FILEPATH", productFilePath); //文件路径
|
||||
pd.put("NAME", NAME); //文件名
|
||||
pd.put("PARENT_ID", PARENT_ID); //目录ID
|
||||
pd.put("CTIME", DateUtil.date2Str(new Date())); //创建时间
|
||||
pd.put("UNAME", Jurisdiction.getName()); //上传者,当前用户的姓名
|
||||
pd.put("MASTER", Jurisdiction.getUsername()); //用户名
|
||||
pd.put("SHARE", SHARE); //是否共享
|
||||
pd.put("MFOLDER_ID", this.get32UUID()); //主键
|
||||
pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID()); //企业
|
||||
mfolderService.save(pd);
|
||||
}
|
||||
|
||||
map.put("result", "success");//返回结果
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -66,13 +66,13 @@ public class SpecialEquipmentAqxjController extends BaseController {
|
|||
fileList.add(FILES[i]);
|
||||
if (i!=TYPE.length-1 && !TYPE[i].equals(TYPE[i+1])) {
|
||||
MultipartFile[] fileArr = new MultipartFile[fileList.size()];
|
||||
imgFilesController.finish(fileList.toArray(fileArr),type,pd.getString("SPECIALEQUIPMENTAQXJ_ID"));
|
||||
imgFilesController.finish(fileList.toArray(fileArr),type,pd.getString("SPECIALEQUIPMENTAQXJ_ID"),new String[]{});
|
||||
fileList.clear();
|
||||
}
|
||||
}
|
||||
if (fileList.size()>0) {
|
||||
MultipartFile[] fileArr = new MultipartFile[fileList.size()];
|
||||
imgFilesController.finish(fileList.toArray(fileArr),type,pd.getString("SPECIALEQUIPMENTAQXJ_ID"));
|
||||
imgFilesController.finish(fileList.toArray(fileArr),type,pd.getString("SPECIALEQUIPMENTAQXJ_ID"),new String[]{});
|
||||
}
|
||||
map.put("pd", pd);
|
||||
map.put("result", errInfo);
|
||||
|
|
|
@ -6,6 +6,7 @@ import org.springframework.context.annotation.Configuration;
|
|||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 定时清除临时文件任务
|
||||
|
@ -18,11 +19,16 @@ public class ClearFilesJob {
|
|||
|
||||
private final static Logger log = LoggerFactory.getLogger(ClearFilesJob.class);
|
||||
|
||||
@Scheduled(cron ="0 0 0 30 * ?")
|
||||
// @Scheduled(cron ="0 0 0 30 * ?")
|
||||
@Scheduled(cron = "0 0 0 * * ?")
|
||||
public void scheduled(){
|
||||
log.info("==========清除临时文件定时任务开启==========");
|
||||
String filePath = Const.PATH_PREFIX + Const.TEMPORARY_FILE_PATH;
|
||||
log.info(delAllFile(filePath) ? "临时文件清除成功!" : "临时文件清除失败!");
|
||||
System.out.println("==========清除临时文件定时任务开启==========");
|
||||
// String filePath = Const.PATH_PREFIX + Const.TEMPORARY_FILE_PATH;
|
||||
String filePath = "D:/file/test/linshi/";
|
||||
String beforeDayDate = DateUtil.getBeforeDayDate();
|
||||
System.out.println("要清除的文件夹:"+ filePath + beforeDayDate);
|
||||
delAllFile(filePath + beforeDayDate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -195,6 +195,20 @@ public class DateUtil {
|
|||
return dateStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取30天之前的日期字符串
|
||||
* @return yyyyMMdd格式时间字符串
|
||||
*/
|
||||
public static String getBeforeDayDate() {
|
||||
Calendar canlendar = Calendar.getInstance();
|
||||
canlendar.setTime(new Date());
|
||||
canlendar.add(Calendar.DATE, -30);
|
||||
Date date = canlendar.getTime();
|
||||
SimpleDateFormat sdfd = new SimpleDateFormat("yyyyMMdd");
|
||||
String dateStr = sdfd.format(date);
|
||||
return dateStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到n天之后是周几
|
||||
* @param days
|
||||
|
|
|
@ -4,6 +4,8 @@ import cn.hutool.core.io.FileUtil;
|
|||
import cn.hutool.core.io.IORuntimeException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* 文件上传工具类
|
||||
* 变更文件上传方式,原上传方式可能会导致附件丢失
|
||||
|
@ -16,7 +18,11 @@ public class FileUploadUtil {
|
|||
|
||||
private final static String PATH_PREFIX = "/mnt/vdc1/qask/file"; //服务器路径前缀
|
||||
private final static String TEMPORARY_FILE_PATH = "/uploadFiles/linshi/"; //临时文件储存路径
|
||||
private final static String PRODUCT_FILE_PATH = "/uploadFiles/yhtp/"; //正式文件储存路径
|
||||
private final static String PRODUCT_FILE_PATH = "/uploadFiles/zhengshi/"; //正式文件储存路径
|
||||
// private final static String PATH_PREFIX = "D:/file"; //服务器路径前缀
|
||||
// private final static String TEMPORARY_FILE_PATH = "/test/linshi/"; //临时文件储存路径
|
||||
// private final static String PRODUCT_FILE_PATH = "/test/zhengshi/"; //正式文件储存路径
|
||||
|
||||
/**
|
||||
* 上传文件到服务器临时地址
|
||||
*
|
||||
|
@ -31,11 +37,11 @@ public class FileUploadUtil {
|
|||
String ffile = DateUtil.getDays();
|
||||
String fileName = UuidUtil.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
||||
try {
|
||||
Smb.sshSftp(file,fileName,TEMPORARY_FILE_PATH + CORPINFO_ID + "/" + ffile);
|
||||
Smb.sshSftp(file,fileName,TEMPORARY_FILE_PATH + ffile + "/" + CORPINFO_ID);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return TEMPORARY_FILE_PATH + CORPINFO_ID+ "/" + ffile + "/" + fileName;
|
||||
return TEMPORARY_FILE_PATH + ffile + "/" + CORPINFO_ID + "/" + fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,16 +58,32 @@ public class FileUploadUtil {
|
|||
//临时文件在服务器的完整路径
|
||||
String completeTemporaryPath = PATH_PREFIX + temporaryFilePath;
|
||||
//截取文件名
|
||||
String fileName = completeTemporaryPath.substring(completeTemporaryPath.lastIndexOf("")).replace("/","");
|
||||
String fileName = completeTemporaryPath.substring(completeTemporaryPath.lastIndexOf("/")).replace("/","");
|
||||
//正式文件完整路径
|
||||
String completeProductPath = PATH_PREFIX + PRODUCT_FILE_PATH + CORPINFO_ID + "/" + DateUtil.getDays() + "/";
|
||||
String completeProductPath = PATH_PREFIX + PRODUCT_FILE_PATH + CORPINFO_ID + "/" ;
|
||||
//临时文件拷贝到正式路径
|
||||
try {
|
||||
FileUtil.copy(completeTemporaryPath,completeProductPath,true);
|
||||
FileUtil.copy(completeTemporaryPath,completeProductPath,false);
|
||||
} catch (IORuntimeException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return PRODUCT_FILE_PATH + fileName;
|
||||
return PRODUCT_FILE_PATH + CORPINFO_ID + "/" + fileName;
|
||||
}
|
||||
|
||||
public static String copyTemporaryFileToProduct(String temporaryFilePath) throws Exception {
|
||||
//临时文件在服务器的完整路径
|
||||
String completeTemporaryPath = PATH_PREFIX + temporaryFilePath;
|
||||
//截取文件名
|
||||
String fileName = completeTemporaryPath.substring(completeTemporaryPath.lastIndexOf("/")).replace("/","");
|
||||
//正式文件完整路径
|
||||
String completeProductPath = PATH_PREFIX + PRODUCT_FILE_PATH + "/" ;
|
||||
//临时文件拷贝到正式路径
|
||||
try {
|
||||
FileUtil.copy(completeTemporaryPath,completeProductPath,false);
|
||||
} catch (IORuntimeException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return PRODUCT_FILE_PATH + "/" + fileName;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue