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
+3 -3
View File
@@ -11,7 +11,7 @@
Target Server Version : 50720
File Encoding : 65001
Date: 14/07/2019 17:55:13
Date: 16/07/2019 18:22:50
*/
SET NAMES utf8mb4;
@@ -41,7 +41,7 @@ CREATE TABLE `config` (
-- ----------------------------
-- Records of config
-- ----------------------------
INSERT INTO `config` VALUES (1, 1, 1, 'Hellohao图床', '网站由JAVA语言编写应用SpringBoot框架开发,前端全部组件由BootStrap/Layui框架编写。由作者个人更新维护,后期会加入更全面的功能供大家使用,如有BUG请与我反馈。', 'https://hellohao.nos-eastchina1.126.net/%E7%BD%91%E7%AB%99%E7%B4%A0%E6%9D%90/logo2.png', 'Hellohao 切勿上传违反中华人民共和国互联网法律条约资源', '<li><a href=\"http://www.hellohao.cn/\" rel=\"nofollow\" target=\"_blank\">作者博客</a></li><li><a href=\"http://bz.hellohao.cn/\" rel=\"nofollow\" target=\"_blank\">高清壁纸</a></li><li><a href=\"http://json.hellohao.cn/\" rel=\"nofollow\" target=\"_blank\">json格式化</a></li>', '也许...|这将是最好用的图床', 'console.log(\'JS代码\');', 'http://tc.hellohao.cn', 'https://hellohao.oss-cn-beijing.aliyuncs.com/Hellohao/eb83a0714030248.jpg', 'https://hellohao.oss-cn-beijing.aliyuncs.com/Hellohao/086650714030248.jpg');
INSERT INTO `config` VALUES (1, 5, 1, 'Hellohao图床', '网站由JAVA语言编写应用SpringBoot框架开发,前端全部组件由BootStrap/Layui框架编写。由作者个人更新维护,后期会加入更全面的功能供大家使用,如有BUG请与我反馈。', 'https://hellohao.nos-eastchina1.126.net/%E7%BD%91%E7%AB%99%E7%B4%A0%E6%9D%90/logo2.png', 'Hellohao 切勿上传违反中华人民共和国互联网法律条约资源', '<li><a href=\"http://www.hellohao.cn/\" rel=\"nofollow\" target=\"_blank\">作者博客</a></li><li><a href=\"http://bz.hellohao.cn/\" rel=\"nofollow\" target=\"_blank\">高清壁纸</a></li><li><a href=\"http://json.hellohao.cn/\" rel=\"nofollow\" target=\"_blank\">json格式化</a></li>', '也许...|这将是最好用的图床', 'console.log(\'JS代码\');', 'http://tc.hellohao.cn', 'https://hellohao.oss-cn-beijing.aliyuncs.com/Hellohao/eb83a0714030248.jpg', 'https://hellohao.oss-cn-beijing.aliyuncs.com/Hellohao/086650714030248.jpg');
-- ----------------------------
-- Table structure for emailconfig
@@ -120,7 +120,7 @@ INSERT INTO `keys` VALUES (1, '', '', '', '', '', 1);
INSERT INTO `keys` VALUES (2, '11111111', '222222222222', '444444444444', '555555555', 'http://1.1.1', 2);
INSERT INTO `keys` VALUES (3, '', '', '0', '', '', 3);
INSERT INTO `keys` VALUES (4, '', '', '', '', '', 4);
INSERT INTO `keys` VALUES (5, NULL, NULL, NULL, NULL, NULL, 5);
INSERT INTO `keys` VALUES (5, '0', '0', '0', '0', '0', 5);
-- ----------------------------
-- Table structure for notice
@@ -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;
}
}