transferTo()
org.apache.commons.io.FileUtils.copyInputStreamToFile()
######代码如下:
public void upload(@RequestParam(value = “file”) MultipartFile file) {
if (file != null) {
try {
String fileRealName = file.getOriginalFilename();//获得原始文件名;
int pointIndex = fileRealName.lastIndexOf(".");//点号的位置
String fileSuffix = fileRealName.substring(pointIndex);//截取文件后缀
String fileNewName = DateUtils.getNowTimeForUpload();//文件new名称时间戳
String saveFileName = fileNewName.concat(fileSuffix);//文件存取名
String filePath = “D:\FileAll” ;
File path = new File(filePath); //判断文件路径下的文件夹是否存在,不存在则创建
if (!path.exists()) {
path.mkdirs();
}
File savedFile = new File(filePath);
boolean isCreateSuccess = savedFile.createNewFile(); // 是否创建文件成功
if(isCreateSuccess){ //将文件写入
//第一种
file.transferTo(savedFile);
//第二种
savedFile = new File(filePath,saveFileName);
FileUtils.copyInputStreamToFile(mufile.getInputStream(),savedFile);
}
} catch (Exception e) {
e.printStackTrace();
}
}else {
System.out.println(“文件是空的”);
}
}
附commons.io jar包maven地址:
commons-io
commons-io
2.4
作者:扯吧
来源:CSDN
原文:https://blog.csdn.net/qq_35564978/article/details/81701518
版权声明:本文为博主原创文章,转载请附上博文链接!