From aa6fe513a9c6faa0a96486cbaae2f19021dc343d Mon Sep 17 00:00:00 2001 From: Hellohao <923453645@qq.com> Date: Tue, 16 Jul 2019 21:13:12 +0800 Subject: [PATCH] =?UTF-8?q?2019-7-16=E6=9B=B4=E6=96=B0=20=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=9C=AC=E5=9C=B0=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/picturebed.sql | 6 +- .../cn/hellohao/config/WebMvcConfigurer.java | 22 ++++++ .../java/cn/hellohao/utils/LocUpdateImg.java | 72 +++++++++++++++++++ src/main/java/cn/hellohao/utils/SetFiles.java | 20 ++++++ 4 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cn/hellohao/config/WebMvcConfigurer.java create mode 100644 src/main/java/cn/hellohao/utils/LocUpdateImg.java create mode 100644 src/main/java/cn/hellohao/utils/SetFiles.java diff --git a/sql/picturebed.sql b/sql/picturebed.sql index 441ee44..eeb2f4c 100644 --- a/sql/picturebed.sql +++ b/sql/picturebed.sql @@ -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 切勿上传违反中华人民共和国互联网法律条约资源', '
  • 作者博客
  • 高清壁纸
  • json格式化
  • ', '也许...|这将是最好用的图床', '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 切勿上传违反中华人民共和国互联网法律条约资源', '
  • 作者博客
  • 高清壁纸
  • json格式化
  • ', '也许...|这将是最好用的图床', '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 diff --git a/src/main/java/cn/hellohao/config/WebMvcConfigurer.java b/src/main/java/cn/hellohao/config/WebMvcConfigurer.java new file mode 100644 index 0000000..f6fa29e --- /dev/null +++ b/src/main/java/cn/hellohao/config/WebMvcConfigurer.java @@ -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); + } +} diff --git a/src/main/java/cn/hellohao/utils/LocUpdateImg.java b/src/main/java/cn/hellohao/utils/LocUpdateImg.java new file mode 100644 index 0000000..2491f07 --- /dev/null +++ b/src/main/java/cn/hellohao/utils/LocUpdateImg.java @@ -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 ImageuploadLOC(Map fileMap, String username, Map fileMap2) throws Exception { + String filePath =File.separator + "HellohaoData" + File.separator; + if(fileMap2==null){ + File file = null; + Map ImgUrl = new HashMap<>(); + for (Map.Entry 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 ImgUrl = new HashMap<>(); + + for (Map.Entry 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; + } + } + +} diff --git a/src/main/java/cn/hellohao/utils/SetFiles.java b/src/main/java/cn/hellohao/utils/SetFiles.java new file mode 100644 index 0000000..f54945f --- /dev/null +++ b/src/main/java/cn/hellohao/utils/SetFiles.java @@ -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; + } +}