实现对重复文件自动重命名
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -66,6 +66,12 @@
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import com.fjy.spring.service.FileService;
|
||||
import com.fjy.spring.service.LogService;
|
||||
import com.fjy.spring.untils.FormatFileSizeUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -175,13 +176,23 @@ public class UpLoadController {
|
||||
pathname = uploadUrl + filename;
|
||||
tbFile.setColfilename(filename);
|
||||
}
|
||||
|
||||
File targetFile = new File(pathname);
|
||||
//若文件已存在则自动重命名
|
||||
if (targetFile.exists()){
|
||||
File mvfile = new File(pathname+".bak");
|
||||
try {
|
||||
FileUtils.moveFile(targetFile, mvfile);
|
||||
log.info("源文件:"+targetFile.getName()+"已重命名为:"+ mvfile.getName());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
log.info("文件上传到: " + uploadUrl + filename);
|
||||
log.info("文件大小: " + new FormatFileSizeUtil().GetFileSize(file.getSize()));
|
||||
log.info("文件名: " + filename);
|
||||
|
||||
|
||||
tbFile.setColfilesize(new FormatFileSizeUtil().GetFileSize(file.getSize()));
|
||||
|
||||
tbFile.setColtime(dateNowStr);
|
||||
|
||||
Reference in New Issue
Block a user