[新增功能](hyx_changeFileUpload_2024-9-10)
ClearFilesJob.java - 定时清除临时文件任务 FileUploadUtil.java - 上传文件到服务器临时地址 - 临时文件拷贝到正式 MfolderController.java - 未完成hyx_changeFileUpload_2024-9-10
							parent
							
								
									1d66e96757
								
							
						
					
					
						commit
						a24771a505
					
				| 
						 | 
					@ -1,5 +1,6 @@
 | 
				
			||||||
package com.zcloud.controller.filemanager;
 | 
					package com.zcloud.controller.filemanager;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.zcloud.util.*;
 | 
				
			||||||
import net.sf.json.JSONArray;
 | 
					import net.sf.json.JSONArray;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.Console;
 | 
					import java.io.Console;
 | 
				
			||||||
| 
						 | 
					@ -22,16 +23,6 @@ import com.zcloud.controller.base.BaseController;
 | 
				
			||||||
import com.zcloud.entity.Page;
 | 
					import com.zcloud.entity.Page;
 | 
				
			||||||
import com.zcloud.entity.PageData;
 | 
					import com.zcloud.entity.PageData;
 | 
				
			||||||
import com.zcloud.service.filemanager.MfolderService;
 | 
					import com.zcloud.service.filemanager.MfolderService;
 | 
				
			||||||
import com.zcloud.util.Const;
 | 
					 | 
				
			||||||
import com.zcloud.util.DateUtil;
 | 
					 | 
				
			||||||
import com.zcloud.util.DelFileUtil;
 | 
					 | 
				
			||||||
import com.zcloud.util.FileDownload;
 | 
					 | 
				
			||||||
import com.zcloud.util.FileUpload;
 | 
					 | 
				
			||||||
import com.zcloud.util.FileUtil;
 | 
					 | 
				
			||||||
import com.zcloud.util.Jurisdiction;
 | 
					 | 
				
			||||||
import com.zcloud.util.PathUtil;
 | 
					 | 
				
			||||||
import com.zcloud.util.Smb;
 | 
					 | 
				
			||||||
import com.zcloud.util.Tools;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
 | 
					import org.apache.shiro.authz.annotation.RequiresPermissions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -75,6 +66,53 @@ public class MfolderController extends BaseController {
 | 
				
			||||||
		return map;
 | 
							return map;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * 上传临时文件
 | 
				
			||||||
 | 
						 * @return 不含前缀的临时文件路径
 | 
				
			||||||
 | 
						 * @throws Exception
 | 
				
			||||||
 | 
						 * @date 2024-9-10
 | 
				
			||||||
 | 
						 * @author hyx
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						@RequestMapping("upLoadTemporary")
 | 
				
			||||||
 | 
						@ResponseBody
 | 
				
			||||||
 | 
						public Object upLoadTemporary(@RequestParam(value="FFILE",required=false) MultipartFile file) throws Exception{
 | 
				
			||||||
 | 
							Map<String,Object> map = new HashMap<String,Object>();
 | 
				
			||||||
 | 
							if (null != file && !file.isEmpty()) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							String CORPINFO_ID = Jurisdiction.getCORPINFO_ID();
 | 
				
			||||||
 | 
							String temporaryFilePath = FileUploadUtil.uploadTemporaryFile(file, CORPINFO_ID);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							map.put("temporaryFilePath", temporaryFilePath);
 | 
				
			||||||
 | 
							return map;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * 保存文件到正式地址
 | 
				
			||||||
 | 
						 * @param temporaryFilePath 不含前缀的临时文件路径
 | 
				
			||||||
 | 
						 * @param PARENT_ID 文件父级id
 | 
				
			||||||
 | 
						 * @param REMARKS 备注
 | 
				
			||||||
 | 
						 * @param SHARE 是否共享
 | 
				
			||||||
 | 
						 * @return
 | 
				
			||||||
 | 
						 * @throws Exception
 | 
				
			||||||
 | 
						 * @date 2024-9-10
 | 
				
			||||||
 | 
						 * @author hyx
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						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
 | 
				
			||||||
 | 
											   ) throws Exception {
 | 
				
			||||||
 | 
							Map<String,Object> map = new HashMap<String,Object>();
 | 
				
			||||||
 | 
							PageData pd = new PageData();
 | 
				
			||||||
 | 
							String productFilePath = FileUploadUtil.copyTemporaryFileToProduct(temporaryFilePath, Jurisdiction.getCORPINFO_ID());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return null;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**上传文件
 | 
						/**上传文件
 | 
				
			||||||
	 * @param
 | 
						 * @param
 | 
				
			||||||
	 * @throws Exception
 | 
						 * @throws Exception
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,64 @@
 | 
				
			||||||
 | 
					package com.zcloud.util;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
 | 
					import org.slf4j.LoggerFactory;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					import org.springframework.scheduling.annotation.Scheduled;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.File;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 定时清除临时文件任务
 | 
				
			||||||
 | 
					 * 每月30日定时清除 /uploadFiles/linshi/ 路径下所有文件
 | 
				
			||||||
 | 
					 * @date 2024-9-10
 | 
				
			||||||
 | 
					 * @author hyx
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					public class ClearFilesJob {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final static Logger log = LoggerFactory.getLogger(ClearFilesJob.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Scheduled(cron ="0 0 0 30 * ?")
 | 
				
			||||||
 | 
					    public void scheduled(){
 | 
				
			||||||
 | 
					        log.info("==========清除临时文件定时任务开启==========");
 | 
				
			||||||
 | 
					        String filePath = Const.PATH_PREFIX + Const.TEMPORARY_FILE_PATH;
 | 
				
			||||||
 | 
					        log.info(delAllFile(filePath) ? "临时文件清除成功!" : "临时文件清除失败!");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 删除文件夹下所有文件
 | 
				
			||||||
 | 
					     * @param path 目标路径
 | 
				
			||||||
 | 
					     * @return
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static boolean delAllFile(String path) {
 | 
				
			||||||
 | 
					        boolean flag = false;
 | 
				
			||||||
 | 
					        File file = new File(path);
 | 
				
			||||||
 | 
					        if (!file.exists()) {
 | 
				
			||||||
 | 
					            return flag;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (!file.isDirectory()) {
 | 
				
			||||||
 | 
					            return flag;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        String[] tempList = file.list();
 | 
				
			||||||
 | 
					        File temp = null;
 | 
				
			||||||
 | 
					        for (int i = 0; i < tempList.length; i++) {
 | 
				
			||||||
 | 
					            if (path.endsWith(File.separator)) {
 | 
				
			||||||
 | 
					                temp = new File(path + tempList[i]);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                temp = new File(path + File.separator + tempList[i]);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (temp.isFile()) {
 | 
				
			||||||
 | 
					                temp.delete();
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (temp.isDirectory()) {
 | 
				
			||||||
 | 
					                //删除文件夹下文件
 | 
				
			||||||
 | 
					                delAllFile(path + "/" + tempList[i]);
 | 
				
			||||||
 | 
					                flag = true;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return flag;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -63,4 +63,8 @@ public class Const {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	public static final String USER_CITY_CODE = "SESSION_USER_CITY_CODE";
 | 
						public static final String USER_CITY_CODE = "SESSION_USER_CITY_CODE";
 | 
				
			||||||
	public static final String USER_CITY_NAME = "SESSION_USER_CITY_NAME";
 | 
						public static final String USER_CITY_NAME = "SESSION_USER_CITY_NAME";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public final static String TEMPORARY_FILE_PATH = "/uploadFiles/linshi/";  // 临时文件路径
 | 
				
			||||||
 | 
						public final static String PRODUCT_FILE_PATH = "/uploadFiles/yhtp/";  //正式文件路径
 | 
				
			||||||
 | 
						public final static String PATH_PREFIX = "/mnt/vdc1/qask/file";  //服务器路径前缀
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,68 @@
 | 
				
			||||||
 | 
					package com.zcloud.util;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import cn.hutool.core.io.FileUtil;
 | 
				
			||||||
 | 
					import cn.hutool.core.io.IORuntimeException;
 | 
				
			||||||
 | 
					import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * 文件上传工具类
 | 
				
			||||||
 | 
					 * 变更文件上传方式,原上传方式可能会导致附件丢失
 | 
				
			||||||
 | 
					 * 修改后逻辑:点击上传 -> 调后端接口返回临时文件路径 -> 提交-> 临时文件拷贝到正式,保存正式文件地址 -> 定期清空临时文件
 | 
				
			||||||
 | 
					 * @date 2024-9-10
 | 
				
			||||||
 | 
					 * @author hyx
 | 
				
			||||||
 | 
					 * @version V1.0
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					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/";  //正式文件储存路径
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 上传文件到服务器临时地址
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param file 文件
 | 
				
			||||||
 | 
					     * @param CORPINFO_ID 企业id
 | 
				
			||||||
 | 
					     * @return 文件临时路径
 | 
				
			||||||
 | 
					     * @throws Exception
 | 
				
			||||||
 | 
					     * @date 2024-9-10
 | 
				
			||||||
 | 
					     * @author hyx
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static String uploadTemporaryFile(MultipartFile file,String CORPINFO_ID) throws Exception {
 | 
				
			||||||
 | 
					        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);
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					            throw new RuntimeException(e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return TEMPORARY_FILE_PATH + CORPINFO_ID+ "/" + ffile + "/" + fileName;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 临时文件拷贝到正式
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param temporaryFilePath 临时文件路径
 | 
				
			||||||
 | 
					     * @param CORPINFO_ID 企业id
 | 
				
			||||||
 | 
					     * @return 不含路径前缀的正式文件路径
 | 
				
			||||||
 | 
					     * @throws Exception
 | 
				
			||||||
 | 
					     * @date 2024-9-10
 | 
				
			||||||
 | 
					     * @author hyx
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static String copyTemporaryFileToProduct(String temporaryFilePath,String CORPINFO_ID) throws Exception {
 | 
				
			||||||
 | 
					        //临时文件在服务器的完整路径
 | 
				
			||||||
 | 
					        String completeTemporaryPath = PATH_PREFIX + temporaryFilePath;
 | 
				
			||||||
 | 
					        //截取文件名
 | 
				
			||||||
 | 
					        String fileName = completeTemporaryPath.substring(completeTemporaryPath.lastIndexOf("")).replace("/","");
 | 
				
			||||||
 | 
					        //正式文件完整路径
 | 
				
			||||||
 | 
					        String completeProductPath = PATH_PREFIX + PRODUCT_FILE_PATH + CORPINFO_ID + "/" + DateUtil.getDays() + "/";
 | 
				
			||||||
 | 
					        //临时文件拷贝到正式路径
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            FileUtil.copy(completeTemporaryPath,completeProductPath,true);
 | 
				
			||||||
 | 
					        } catch (IORuntimeException e) {
 | 
				
			||||||
 | 
					            throw new RuntimeException(e);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return PRODUCT_FILE_PATH + fileName;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue