完成后端上传文件的代码。

This commit is contained in:
2025-10-16 15:20:11 +08:00
parent 2eaedf52fe
commit 74c4389c9f
4 changed files with 59 additions and 50 deletions

View File

@@ -27,7 +27,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
// @SuppressWarnings( "unused" ) // @SuppressWarnings( "unused" )
@Controller @Controller
@RequestMapping( path = "/file" ) @RequestMapping( path = "/file" )
public abstract class FileUpload public class FileUpload
{ {
/** /**
* 接收上传文件,并保存到临时目录: * 接收上传文件,并保存到临时目录:
@@ -101,7 +101,7 @@ public abstract class FileUpload
@RequestMapping( path = "/file-upload.do" ) @RequestMapping( path = "/file-upload.do" )
@ResponseBody @ResponseBody
public static FileUploadResult saveUploadFile( public FileUploadResult saveUploadFile(
@RequestParam( "file-name" ) String fileName, @RequestParam( "file-name" ) String fileName,
@RequestParam( "files" ) MultipartFile file, @RequestParam( "files" ) MultipartFile file,
HttpServletRequest request HttpServletRequest request
@@ -119,7 +119,7 @@ public abstract class FileUpload
File dir = new File( filePath ); File dir = new File( filePath );
// 创建临时目录 // 创建临时目录
if ( !dir.mkdirs() ) if ( (!dir.exists()) && (!dir.mkdirs()) )
{ {
result.setSuccess( false ); result.setSuccess( false );
result.setMessage( "创建临时目录失败:" + filePath ); result.setMessage( "创建临时目录失败:" + filePath );
@@ -129,6 +129,8 @@ public abstract class FileUpload
try try
{ {
files[0] = file;
SaveUploadFile.saveUploadFile( files, filePath ); SaveUploadFile.saveUploadFile( files, filePath );
} }
catch ( ProcessUploadedFileException error ) catch ( ProcessUploadedFileException error )

View File

@@ -2,7 +2,7 @@
* @Author: Kane Wang <wangkane@qq.com> * @Author: Kane Wang <wangkane@qq.com>
* @Date: 2025-10-16 09:46:42 * @Date: 2025-10-16 09:46:42
* @LastEditors: Kane Wang * @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 * @FilePath: src/main/java/com/cpic/xim/web/controllers/fileupload/SaveUploadFile.java
* @Description: * @Description:
* *
@@ -12,6 +12,8 @@ package com.cpic.xim.web.controllers.fileupload;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap; import java.util.HashMap;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@@ -29,12 +31,6 @@ public class SaveUploadFile
String fileName = ""; String fileName = "";
String fullPath; String fullPath;
// if ( !dir.mkdir() )
// {
// // 创建目录失败
// throw new ProcessUploadedFileException( "创建临时目录失败:" + tempFilePath );
// }
if ( !(tempFilePath.endsWith( "/" ) || tempFilePath.endsWith( "\\" )) ) if ( !(tempFilePath.endsWith( "/" ) || tempFilePath.endsWith( "\\" )) )
{ {
fullPath = tempFilePath + "/"; fullPath = tempFilePath + "/";
@@ -54,12 +50,14 @@ public class SaveUploadFile
continue; 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 destFile = new File( tempFilePath, fileName );
file.transferTo( destFile ); file.transferTo( destFile );
savedFiles.put( fileName, fullPath + fileName ); savedFiles.put( fileName, fullPath + fileName );
} }
} }

View File

@@ -29,9 +29,10 @@
</bean> --> </bean> -->
<bean id="multipartResolver" <bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
<property name="defaultEncoding" value="UTF-8" /> <!-- <property name="defaultEncoding" value="UTF-8" /> -->
<property name="maxUploadSize" value="-1" /> <!-- <property name="maxUploadSize" value="-1" /> -->
<!-- <property name="maxFileSize" value="10485760" /> -->
</bean> </bean>
</beans> </beans>

View File

@@ -17,6 +17,14 @@
<param-value>classpath:spring.xml</param-value> <param-value>classpath:spring.xml</param-value>
</init-param> </init-param>
<load-on-startup>1</load-on-startup> <load-on-startup>1</load-on-startup>
<multipart-config>
<!-- 上传文件最大为多少 -->
<max-file-size>10485760</max-file-size>
<!-- 最大的请求大小 -->
<max-request-size>10485760</max-request-size>
<!-- 多大以上的文件可以上传 -->
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet> </servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>springmvc</servlet-name> <servlet-name>springmvc</servlet-name>