完成后端上传文件的代码。
This commit is contained in:
@@ -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 )
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
@@ -26,14 +28,8 @@ public class SaveUploadFile
|
|||||||
{
|
{
|
||||||
HashMap<String, String> savedFiles = new HashMap<>();
|
HashMap<String, String> savedFiles = new HashMap<>();
|
||||||
// File dir = new File( tempFilePath );
|
// File dir = new File( tempFilePath );
|
||||||
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( "\\" )) )
|
||||||
{
|
{
|
||||||
@@ -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 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -1,39 +1,47 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<web-app
|
<web-app
|
||||||
version="4.0"
|
version="4.0"
|
||||||
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||||
xmlns:javaee="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:xml="http://www.w3.org/XML/1998/namespace"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
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>
|
|
||||||
|
|
||||||
<servlet>
|
<display-name>Archetype Created Web Application</display-name>
|
||||||
<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>
|
|
||||||
|
|
||||||
<filter>
|
<servlet>
|
||||||
<filter-name>CrosFilter</filter-name>
|
<servlet-name>springmvc</servlet-name>
|
||||||
<filter-class>com.cpic.xim.web.filters.cros.CrosFilter</filter-class>
|
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||||
</filter>
|
<init-param>
|
||||||
<filter-mapping>
|
<param-name>contextConfigLocation</param-name>
|
||||||
<filter-name>CrosFilter</filter-name>
|
<param-value>classpath:spring.xml</param-value>
|
||||||
<url-pattern>*</url-pattern>
|
</init-param>
|
||||||
</filter-mapping>
|
<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>
|
||||||
|
|
||||||
</web-app>
|
</web-app>
|
||||||
Reference in New Issue
Block a user