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

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" )
@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 )

View File

@@ -2,7 +2,7 @@
* @Author: Kane Wang <wangkane@qq.com>
* @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<String, String> 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 );
}
}

View File

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

View File

@@ -1,39 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app
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">
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">
<display-name>Archetype Created Web Application</display-name>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/account/p13_account_check</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<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-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/account/p13_account_check</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CrosFilter</filter-name>
<filter-class>com.cpic.xim.web.filters.cros.CrosFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CrosFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CrosFilter</filter-name>
<filter-class>com.cpic.xim.web.filters.cros.CrosFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CrosFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
</web-app>