2019-7-16更新 支持本地存储

This commit is contained in:
Hellohao
2019-07-16 21:13:12 +08:00
parent 21e0bf2de4
commit aa6fe513a9
4 changed files with 117 additions and 3 deletions
@@ -0,0 +1,22 @@
package cn.hellohao.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import java.io.File;
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String filePath =File.separator + "HellohaoData" + File.separator;
//和页面有关的静态目录都放在项目的static目录下
//registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//上传的图片在D盘下的OTA目录下,访问路径如:http://localhost:8081/OTA/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
//其中OTA表示访问的前缀。"file:D:/OTA/"是文件真实的存储路径
//registry.addResourceHandler("/test/**").addResourceLocations("file:C:/test/");
registry.addResourceHandler("/links/**").addResourceLocations("file:"+filePath);
}
}
@@ -0,0 +1,72 @@
package cn.hellohao.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class LocUpdateImg {
public static void deleteLOCImg(String imagename){
String filePath =File.separator + "HellohaoData" + File.separator+imagename;
File file = new File(filePath);
file.delete();
}
public static Map<String, Integer> ImageuploadLOC(Map<String, MultipartFile> fileMap, String username, Map<String, String> fileMap2) throws Exception {
String filePath =File.separator + "HellohaoData" + File.separator;
if(fileMap2==null){
File file = null;
Map<String, Integer> ImgUrl = new HashMap<>();
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
String times = format1.format(new Date());
//file = SetFiles.changeFile(entry.getValue());
// 上传文件流。
System.out.println("待上传的图片:"+username + "/" + uuid+times + "." + entry.getKey());
File dest = new File(filePath + username + File.separator+ uuid+times + "." + entry.getKey());
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
entry.getValue().transferTo(dest);
ImgUrl.put(username + "/" + uuid+times + "." + entry.getKey(), (int) (entry.getValue().getSize()));
} catch (IOException e) {
e.printStackTrace();
System.err.println("上传失败");
}
}
return ImgUrl;
}else{
Map<String, Integer> ImgUrl = new HashMap<>();
for (Map.Entry<String, String> entry : fileMap2.entrySet()) {
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
String times = format1.format(new Date());
String oldfilePath = entry.getValue();
String newfilePath = File.separator + "HellohaoData" + File.separator + username + File.separator+ uuid+times + "." + entry.getKey();//
File file = new File(oldfilePath);
File targetFile =new File(newfilePath);
if(!targetFile.getParentFile().exists()) {
targetFile.mkdirs();
}
file.renameTo(new File(newfilePath));//只移动,源目录不存在文件
// 例2:采用数据流模式上传文件(节省内存),自动创建父级目录
//upyun.setContentMD5(UpYun.md5(new File(imgurl)));
// boolean result = upyun.writeFile(username + "/" + uuid+times + "." + entry.getKey(), new File(imgurl), true);
ImgUrl.put( username + "/" + uuid+times + "." + entry.getKey(), ImgUrlUtil.getFileSize2(new File(newfilePath)));
}
//new File(imgurl).delete();
return ImgUrl;
}
}
}
@@ -0,0 +1,20 @@
package cn.hellohao.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
public class SetFiles {
// 转换文件方法
public static File changeFile(MultipartFile multipartFile) throws Exception {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String prefix = fileName.substring(fileName.lastIndexOf("."));
// todo 修改临时文件文件名
File file = File.createTempFile(fileName, prefix);
// MultipartFile to File
multipartFile.transferTo(file);
return file;
}
}