保存进度!
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
* @Author: Kane Wang <wangkane@qq.com>
|
||||
* @Date: 2025-10-16 09:46:42
|
||||
* @LastEditors: Kane Wang
|
||||
* @LastModified: 2025-11-18 17:03:33
|
||||
* @LastModified: 2025-11-27 17:29:30
|
||||
* @FilePath: src/main/java/com/cpic/xim/utils/files/SaveUploadFile.java
|
||||
* @Description:
|
||||
*
|
||||
@@ -82,30 +82,32 @@ public class SaveUploadFile
|
||||
|
||||
/**
|
||||
* 将files参数中上传的文件,保存到tempFilePath指定的路径,文件名加上毫秒数避免重复。
|
||||
* @param files
|
||||
* @param tempFilePath
|
||||
*
|
||||
* @param files
|
||||
* @param absoluteFilePath
|
||||
* @return 返回UploadedFile对象数组
|
||||
* @throws ProcessUploadedFileException
|
||||
*/
|
||||
public static Vector<UploadedFile> saveUploadFiles(
|
||||
MultipartFile[] files,
|
||||
String tempFilePath
|
||||
String absoluteFilePath,
|
||||
String relativeFilePath
|
||||
)
|
||||
throws ProcessUploadedFileException
|
||||
{
|
||||
Vector<UploadedFile> savedFiles = new Vector<>();
|
||||
UploadedFile uploadedFile = null;
|
||||
Vector<UploadedFile> savedFiles = new Vector<>();
|
||||
UploadedFile uploadedFile = null;
|
||||
// File dir = new File( tempFilePath );
|
||||
String fileName = "";
|
||||
String fullPath;
|
||||
String fullPath = null;
|
||||
|
||||
if ( !(tempFilePath.endsWith( "/" ) || tempFilePath.endsWith( "\\" )) )
|
||||
if ( !(absoluteFilePath.endsWith( "/" ) || absoluteFilePath.endsWith( "\\" )) )
|
||||
{
|
||||
fullPath = tempFilePath + "/";
|
||||
fullPath = absoluteFilePath + "/";
|
||||
}
|
||||
else
|
||||
{
|
||||
fullPath = tempFilePath;
|
||||
fullPath = absoluteFilePath;
|
||||
}
|
||||
|
||||
try
|
||||
@@ -122,18 +124,18 @@ public class SaveUploadFile
|
||||
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( fullPath, fileName );
|
||||
|
||||
file.transferTo( destFile );
|
||||
|
||||
uploadedFile = new UploadedFile(fileName, destFile.getPath() );
|
||||
uploadedFile = new UploadedFile( fileName, fullPath, relativeFilePath );
|
||||
|
||||
savedFiles.add( uploadedFile );
|
||||
}
|
||||
}
|
||||
catch ( IOException error )
|
||||
{
|
||||
throw new ProcessUploadedFileException( "临时目录" + tempFilePath + "保存文件" + fileName + "失败!" );
|
||||
throw new ProcessUploadedFileException( "临时目录" + absoluteFilePath + "保存文件" + fileName + "失败!" );
|
||||
}
|
||||
|
||||
return savedFiles;
|
||||
@@ -142,32 +144,33 @@ public class SaveUploadFile
|
||||
/**
|
||||
*
|
||||
* @param originFilePath 文件路径
|
||||
* @param newFilePath 新文件存放路径
|
||||
* @param newFilePath 新文件存放路径
|
||||
* @throws MoveUploadedFileException
|
||||
*/
|
||||
public static void MoveUploadedFile(
|
||||
String originFilePath,
|
||||
String newFilePath
|
||||
) throws MoveUploadedFileException
|
||||
)
|
||||
throws MoveUploadedFileException
|
||||
{
|
||||
// 防御性验证
|
||||
File originFile = new File( originFilePath );
|
||||
File dest = new File( newFilePath );
|
||||
File dest = new File( newFilePath );
|
||||
|
||||
// 文件如果不存在就抛出异常
|
||||
if ( originFile.exists() == false )
|
||||
{
|
||||
throw new MoveUploadedFileException("文件不存在!");
|
||||
throw new MoveUploadedFileException( "文件不存在!" );
|
||||
}
|
||||
|
||||
if ( dest.exists() == true )
|
||||
{
|
||||
throw new MoveUploadedFileException("目的路径已存在同名文件!" );
|
||||
throw new MoveUploadedFileException( "目的路径已存在同名文件!" );
|
||||
}
|
||||
|
||||
if ( originFile.renameTo(dest) == false )
|
||||
if ( originFile.renameTo( dest ) == false )
|
||||
{
|
||||
throw new MoveUploadedFileException("移动文件失败!" );
|
||||
throw new MoveUploadedFileException( "移动文件失败!" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* @Author: Kane Wang <wangkane@qq.com>
|
||||
* @Date: 2025-10-31 17:33:13
|
||||
* @LastEditors: Kane Wang
|
||||
* @LastModified: 2025-11-19 21:16:07
|
||||
* @LastModified: 2025-11-27 11:43:18
|
||||
* @FilePath: src/main/java/com/cpic/xim/utils/files/UploadedFile.java
|
||||
* @Description:
|
||||
*
|
||||
@@ -12,37 +12,62 @@ package com.cpic.xim.utils.files;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
/**
|
||||
* 描述上传文件的类。
|
||||
*
|
||||
*/
|
||||
public class UploadedFile
|
||||
{
|
||||
@JsonProperty( "fileName" )
|
||||
private String fileName;
|
||||
|
||||
@JsonProperty( "localFilePath" )
|
||||
private String localFilePath;
|
||||
|
||||
|
||||
@JsonProperty( "absoluteFilePath" )
|
||||
private String absoluteFilePath;
|
||||
|
||||
@JsonProperty( "relativeFilePath" )
|
||||
private String relativeFilePath;
|
||||
|
||||
public String getFileName()
|
||||
{
|
||||
return fileName;
|
||||
}
|
||||
|
||||
|
||||
public void setFileName( String fileName )
|
||||
{
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public String getLocalFilePath()
|
||||
|
||||
public String getAbsoluteFilePath()
|
||||
{
|
||||
return localFilePath;
|
||||
return absoluteFilePath;
|
||||
}
|
||||
|
||||
public void setLocalFilePath( String localFilePath )
|
||||
|
||||
public void setAbsoluteFilePath( String absoluteFilePath )
|
||||
{
|
||||
this.localFilePath = localFilePath;
|
||||
this.absoluteFilePath = absoluteFilePath;
|
||||
}
|
||||
|
||||
public UploadedFile( String fileName, String localFilePath )
|
||||
|
||||
public String getRelativeFilePath()
|
||||
{
|
||||
this.fileName = fileName;
|
||||
this.localFilePath = localFilePath;
|
||||
return relativeFilePath;
|
||||
}
|
||||
|
||||
public void setRelativeFilePath( String relativeFilePath )
|
||||
{
|
||||
this.relativeFilePath = relativeFilePath;
|
||||
}
|
||||
|
||||
public UploadedFile( String fileName, String absoluteFilePath, String relativeFilePath )
|
||||
{
|
||||
this.fileName = fileName;
|
||||
this.absoluteFilePath = absoluteFilePath;
|
||||
this.relativeFilePath = relativeFilePath;
|
||||
}
|
||||
|
||||
public UploadedFile( String fileName, String absoluteFilePath )
|
||||
{
|
||||
this.fileName = fileName;
|
||||
this.absoluteFilePath = absoluteFilePath;
|
||||
this.relativeFilePath = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
// @SuppressWarnings( "unused" )
|
||||
@Controller
|
||||
@RequestMapping( path = "/file" )
|
||||
public class FileUpload
|
||||
public class UploadFileController
|
||||
{
|
||||
/**
|
||||
* 接收上传文件,并保存到临时目录:
|
||||
@@ -60,14 +60,15 @@ public class FileUpload
|
||||
result.setSuccess( true );
|
||||
result.setMessage( "上传成功!" );
|
||||
|
||||
String filePath = request.getServletContext().getRealPath( "/temp/upload/" + sessionID );
|
||||
File dir = new File( filePath );
|
||||
String relativeFilePath = "/temp/upload/" + sessionID;
|
||||
String absolutefilePath = request.getServletContext().getRealPath( relativeFilePath );
|
||||
File dir = new File( absolutefilePath );
|
||||
|
||||
// 创建临时目录
|
||||
if ( (!dir.exists()) && (!dir.mkdirs()) )
|
||||
{
|
||||
result.setSuccess( false );
|
||||
result.setMessage( "创建临时目录失败:" + filePath );
|
||||
result.setMessage( "创建临时目录失败:" + absolutefilePath );
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -76,7 +77,9 @@ public class FileUpload
|
||||
{
|
||||
files[0] = file;
|
||||
|
||||
Vector<UploadedFile> uploadFiles = SaveUploadFile.saveUploadFiles( files, filePath );
|
||||
Vector<UploadedFile> uploadFiles = SaveUploadFile.saveUploadFiles( files,
|
||||
absolutefilePath,
|
||||
relativeFilePath );
|
||||
|
||||
result.setFileList( uploadFiles );
|
||||
}
|
||||
Reference in New Issue
Block a user