diff --git a/code/backend/RegulatoryManagementBackend/src/main/java/com/cpic/xim/web/controllers/fileupload/FileUpload.java b/code/backend/RegulatoryManagementBackend/src/main/java/com/cpic/xim/web/controllers/fileupload/FileUpload.java index 8e19bbc..ff0b413 100644 --- a/code/backend/RegulatoryManagementBackend/src/main/java/com/cpic/xim/web/controllers/fileupload/FileUpload.java +++ b/code/backend/RegulatoryManagementBackend/src/main/java/com/cpic/xim/web/controllers/fileupload/FileUpload.java @@ -27,7 +27,7 @@ import org.springframework.web.bind.annotation.RequestMethod; // @SuppressWarnings( "unused" ) @Controller @RequestMapping( path = "/file" ) -public abstract class FileUpload +public class FileUpload { /** * 接收上传文件,并保存到临时目录: @@ -101,7 +101,7 @@ public abstract class FileUpload @RequestMapping( path = "/file-upload.do" ) @ResponseBody - public static FileUploadResult saveUploadFile( + public FileUploadResult saveUploadFile( @RequestParam( "file-name" ) String fileName, @RequestParam( "files" ) MultipartFile file, HttpServletRequest request @@ -119,7 +119,7 @@ public abstract class FileUpload File dir = new File( filePath ); // 创建临时目录 - if ( !dir.mkdirs() ) + if ( (!dir.exists()) && (!dir.mkdirs()) ) { result.setSuccess( false ); result.setMessage( "创建临时目录失败:" + filePath ); @@ -129,6 +129,8 @@ public abstract class FileUpload try { + files[0] = file; + SaveUploadFile.saveUploadFile( files, filePath ); } catch ( ProcessUploadedFileException error ) diff --git a/code/backend/RegulatoryManagementBackend/src/main/java/com/cpic/xim/web/controllers/fileupload/SaveUploadFile.java b/code/backend/RegulatoryManagementBackend/src/main/java/com/cpic/xim/web/controllers/fileupload/SaveUploadFile.java index c0b8e9c..20f51fc 100644 --- a/code/backend/RegulatoryManagementBackend/src/main/java/com/cpic/xim/web/controllers/fileupload/SaveUploadFile.java +++ b/code/backend/RegulatoryManagementBackend/src/main/java/com/cpic/xim/web/controllers/fileupload/SaveUploadFile.java @@ -2,7 +2,7 @@ * @Author: Kane Wang * @Date: 2025-10-16 09:46:42 * @LastEditors: Kane Wang - * @LastModified: 2025-10-16 10:50:45 + * @LastModified: 2025-10-16 15:16:57 * @FilePath: src/main/java/com/cpic/xim/web/controllers/fileupload/SaveUploadFile.java * @Description: * @@ -12,6 +12,8 @@ package com.cpic.xim.web.controllers.fileupload; import java.io.File; import java.io.IOException; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.HashMap; import org.springframework.web.multipart.MultipartFile; @@ -26,14 +28,8 @@ public class SaveUploadFile { HashMap savedFiles = new HashMap<>(); // File dir = new File( tempFilePath ); - String fileName = ""; - String fullPath; - - // if ( !dir.mkdir() ) - // { - // // 创建目录失败 - // throw new ProcessUploadedFileException( "创建临时目录失败:" + tempFilePath ); - // } + String fileName = ""; + String fullPath; if ( !(tempFilePath.endsWith( "/" ) || tempFilePath.endsWith( "\\" )) ) { @@ -54,12 +50,14 @@ public class SaveUploadFile continue; } - fileName = file.getOriginalFilename(); + // 文件名前加上时间戳,避免覆盖 + Long milliSecond = LocalDateTime.now().toInstant( ZoneOffset.of( "+8" ) ).toEpochMilli(); + + fileName = String.valueOf( milliSecond ) + " - " + file.getOriginalFilename(); File destFile = new File( tempFilePath, fileName ); file.transferTo( destFile ); - savedFiles.put( fileName, fullPath + fileName ); } } diff --git a/code/backend/RegulatoryManagementBackend/src/main/webapp/WEB-INF/classes/spring.xml b/code/backend/RegulatoryManagementBackend/src/main/webapp/WEB-INF/classes/spring.xml index a349f68..b669a49 100644 --- a/code/backend/RegulatoryManagementBackend/src/main/webapp/WEB-INF/classes/spring.xml +++ b/code/backend/RegulatoryManagementBackend/src/main/webapp/WEB-INF/classes/spring.xml @@ -29,9 +29,10 @@ --> - - + class="org.springframework.web.multipart.support.StandardServletMultipartResolver"> + + + \ No newline at end of file diff --git a/code/backend/RegulatoryManagementBackend/src/main/webapp/WEB-INF/web.xml b/code/backend/RegulatoryManagementBackend/src/main/webapp/WEB-INF/web.xml index f5c2aef..dd70fff 100644 --- a/code/backend/RegulatoryManagementBackend/src/main/webapp/WEB-INF/web.xml +++ b/code/backend/RegulatoryManagementBackend/src/main/webapp/WEB-INF/web.xml @@ -1,39 +1,47 @@ - - Archetype Created Web Application + version="4.0" + xmlns="http://xmlns.jcp.org/xml/ns/javaee" + xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee" + xmlns:xml="http://www.w3.org/XML/1998/namespace" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"> - - springmvc - org.springframework.web.servlet.DispatcherServlet - - contextConfigLocation - classpath:spring.xml - - 1 - - - springmvc - *.do - - - springmvc - /account/p13_account_check - + Archetype Created Web Application - - CrosFilter - com.cpic.xim.web.filters.cros.CrosFilter - - - CrosFilter - * - + + springmvc + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath:spring.xml + + 1 + + + 10485760 + + 10485760 + + 0 + + + + springmvc + *.do + + + springmvc + /account/p13_account_check + + + + CrosFilter + com.cpic.xim.web.filters.cros.CrosFilter + + + CrosFilter + * + \ No newline at end of file