From a336d3d02c3325ba6058cb7d1de40341e834e6dc Mon Sep 17 00:00:00 2001 From: Hellohao <923453645@qq.com> Date: Tue, 16 Jul 2019 21:11:44 +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 --- pom.xml | 5 + .../hellohao/controller/AdminController.java | 12 +- .../controller/AdminRootController.java | 32 +++--- .../hellohao/controller/ErrorController.java | 35 ++++++ .../controller/UpdateImgController.java | 66 ++++++++--- src/main/java/cn/hellohao/test/test1.java | 28 +++++ .../java/cn/hellohao/utils/IPPortUtil.java | 54 +++++++++ src/main/resources/application.properties | 9 +- .../resources/static/webuploade/js/upload.js | 2 +- src/main/resources/templates/404.html | 62 +++++++++++ .../templates/admin/storageconfig.html | 103 ++++++++++++++---- .../resources/templates/admin/survey.html | 43 +++++++- src/main/resources/templates/admin/table.html | 5 +- src/main/resources/templates/index.html | 28 ++--- 14 files changed, 403 insertions(+), 81 deletions(-) create mode 100644 src/main/java/cn/hellohao/controller/ErrorController.java create mode 100644 src/main/java/cn/hellohao/test/test1.java create mode 100644 src/main/java/cn/hellohao/utils/IPPortUtil.java create mode 100644 src/main/resources/templates/404.html diff --git a/pom.xml b/pom.xml index 4cfb87a..042c239 100644 --- a/pom.xml +++ b/pom.xml @@ -114,6 +114,11 @@ 1.2 + + nekohtml + nekohtml + 1.9.6.2 + org.springframework.boot diff --git a/src/main/java/cn/hellohao/controller/AdminController.java b/src/main/java/cn/hellohao/controller/AdminController.java index a77950f..da3a11e 100644 --- a/src/main/java/cn/hellohao/controller/AdminController.java +++ b/src/main/java/cn/hellohao/controller/AdminController.java @@ -4,6 +4,7 @@ import cn.hellohao.pojo.*; import cn.hellohao.pojo.vo.PageResultBean; import cn.hellohao.service.*; import cn.hellohao.service.impl.ImgServiceImpl; +import cn.hellohao.utils.LocUpdateImg; import cn.hellohao.utils.StringUtils; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @@ -104,13 +105,13 @@ public class AdminController { public String getwebconfig(HttpSession session) { JSONObject jsonObject = new JSONObject(); Config config = configService.getSourceype();//查询当前系统使用的存储源类型。 - Keys key = keysService.selectKeys(config.getSourcekey()); User u = (User) session.getAttribute("user"); Imgreview imgreview = imgreviewService.selectByPrimaryKey(1); jsonObject.put("usercount", imgService.countimg(u.getId()));//这个是根据用户id查询他的图片数 jsonObject.put("counts", imgService.counts(null) );//总数 jsonObject.put("getusertotal", userService.getUserTotal() ); jsonObject.put("imgreviewcount", imgreview.getCount()); + Keys key= keysService.selectKeys(config.getSourcekey());//然后根据类型再查询key Boolean b = StringUtils.doNull(key);//判断对象是否有空值 if(b){ jsonObject.put("source", key.getStorageType()); @@ -220,9 +221,10 @@ public class AdminController { } else if (key.getStorageType() == 3) { de.delectUSS(key, images.getImgname()); } else if (key.getStorageType() == 4) { - //初始化七牛云 de.delectKODO(key, images.getImgname()); - } else { + } else if (key.getStorageType() == 5) { + LocUpdateImg.deleteLOCImg(images.getImgname()); + }else { System.err.println("未获取到对象存储参数,删除失败。"); } Integer ret = imgService.deleimg(id); @@ -263,7 +265,9 @@ public class AdminController { } else if (key.getStorageType() == 4) { //初始化七牛云 de.delectKODO(key, images.getImgname()); - } else { + } else if (key.getStorageType() == 5) { + LocUpdateImg.deleteLOCImg(images.getImgname()); + }else { System.err.println("未获取到对象存储参数,删除失败。"); } Integer ret = imgService.deleimg(ids[i]); diff --git a/src/main/java/cn/hellohao/controller/AdminRootController.java b/src/main/java/cn/hellohao/controller/AdminRootController.java index d9cca52..b49543f 100644 --- a/src/main/java/cn/hellohao/controller/AdminRootController.java +++ b/src/main/java/cn/hellohao/controller/AdminRootController.java @@ -42,26 +42,32 @@ public class AdminRootController { @RequestMapping(value = "tostorage") public String tostorage(HttpSession session, Model model, HttpServletRequest request) { Config config = configService.getSourceype();//查询当前系统使用的存储源类型。 - Keys key = keysService.selectKeys(config.getSourcekey()); + Keys key= keysService.selectKeys(config.getSourcekey());//然后根据类型再查询key Boolean b = StringUtils.doNull(key);//判断对象是否有空值 Integer StorageType = 0; - if(b){ - //key信息 - model.addAttribute("AccessKey", key.getAccessKey()); - model.addAttribute("AccessSecret", key.getAccessSecret()); + if(config.getSourcekey()!=5){ + if(b){ + //key信息 + model.addAttribute("AccessKey", key.getAccessKey()); + model.addAttribute("AccessSecret", key.getAccessSecret()); - model.addAttribute("Endpoint", key.getEndpoint()); - model.addAttribute("Bucketname", key.getBucketname()); - model.addAttribute("RequestAddress", key.getRequestAddress()); - model.addAttribute("StorageType", config.getSourcekey()); - } - //如果是4就是七牛 - if(config.getSourcekey()==4){ - model.addAttribute("Endpoint2", key.getEndpoint()); + model.addAttribute("Endpoint", key.getEndpoint()); + model.addAttribute("Bucketname", key.getBucketname()); + model.addAttribute("RequestAddress", key.getRequestAddress()); + model.addAttribute("StorageType", config.getSourcekey()); + } + //如果是4就是七牛 + if(config.getSourcekey()==4){ + model.addAttribute("Endpoint2", key.getEndpoint()); + }else{ + model.addAttribute("Endpoint2", 0); + } }else{ + model.addAttribute("StorageType", 5);//切换到本地 model.addAttribute("Endpoint2", 0); } + return "admin/storageconfig"; } diff --git a/src/main/java/cn/hellohao/controller/ErrorController.java b/src/main/java/cn/hellohao/controller/ErrorController.java new file mode 100644 index 0000000..1cff8df --- /dev/null +++ b/src/main/java/cn/hellohao/controller/ErrorController.java @@ -0,0 +1,35 @@ +package cn.hellohao.controller; + +import org.springframework.boot.web.servlet.error.ErrorController; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.servlet.http.HttpServletRequest; + +/** + * 错误页面拦截 + * */ + +@Controller +class MainsiteErrorController implements ErrorController { + + @RequestMapping("/error") + public String handleError(HttpServletRequest request){ + //获取statusCode:401,404,500 + Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); + if(statusCode == 401){ + return "/401"; + }else if(statusCode == 404){ + return "/404"; + }else if(statusCode == 403){ + return "/403"; + }else{ + return "/500"; + } + + } + @Override + public String getErrorPath() { + return "/error"; + } +} \ No newline at end of file diff --git a/src/main/java/cn/hellohao/controller/UpdateImgController.java b/src/main/java/cn/hellohao/controller/UpdateImgController.java index 2ee5bbb..677466a 100644 --- a/src/main/java/cn/hellohao/controller/UpdateImgController.java +++ b/src/main/java/cn/hellohao/controller/UpdateImgController.java @@ -7,12 +7,12 @@ import java.util.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import cn.hellohao.TbedApplication; import cn.hellohao.pojo.*; import cn.hellohao.service.*; import cn.hellohao.service.impl.KODOImageupload; import cn.hellohao.service.impl.OSSImageupload; import cn.hellohao.service.impl.USSImageupload; +import cn.hellohao.utils.IPPortUtil; import cn.hellohao.utils.*; import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Autowired; @@ -47,8 +47,9 @@ public class UpdateImgController { @RequestMapping({"/", "/index"}) public String indexImg(Model model, HttpSession httpSession) { + Print.Normal("欢迎使用Hellohao图床源码。者也许是最好用的java图床项目。"); + Print.warning("当前项目路径:"+System.getProperty("user.dir")); Config config = configService.getSourceype();//查询当前系统使用的存储源类型。 - System.out.println("域名:"+config.getDomain()); UploadConfig uploadConfig = uploadConfigService.getUpdateConfig(); Integer filesizetourists = 0; Integer filesizeuser = 0; @@ -58,8 +59,12 @@ public class UpdateImgController { if(uploadConfig.getFilesizeuser()!=null){filesizeuser = uploadConfig.getFilesizeuser();} if(uploadConfig.getImgcounttourists()!=null){imgcounttourists = uploadConfig.getImgcounttourists();} if(uploadConfig.getImgcountuser()!=null){imgcountuser = uploadConfig.getImgcountuser();} - Keys key = keysService.selectKeys(config.getSourcekey());//然后根据类型再查询key - Boolean b = StringUtils.doNull(key);//判断对象是否有空值 + Boolean b =false; + Keys key = null; + if(config.getSourcekey()!=0 && config.getSourcekey()!=5){ + key= keysService.selectKeys(config.getSourcekey());//然后根据类型再查询key + b = StringUtils.doNull(key);//判断对象是否有空值 + } if(b) { if(key.getStorageType()!=0 || key.getStorageType()!=null){ @@ -151,6 +156,8 @@ public class UpdateImgController { }else if(key.getStorageType()==4){ //初始化七牛云 m = kodoImageupload.ImageuploadKODO(map, username,null); + }else if(key.getStorageType()==5){ + m = LocUpdateImg.ImageuploadLOC(map, username,null); }else{ System.err.println("未获取到对象存储参数,上传失败。"); } @@ -159,8 +166,19 @@ public class UpdateImgController { String times = df.format(new Date()); System.out.println("上传图片的时间是:"+times); for (Map.Entry entry : m.entrySet()) { - jsonArray.add(entry.getKey()); - img.setImgurl(entry.getKey());//图片链接 + if(key.getStorageType()==5){ + if(config.getDomain()!=null){ + jsonArray.add(config.getDomain()+"/links/"+entry.getKey()); + img.setImgurl(config.getDomain()+"/links/"+entry.getKey());//图片链接 + }else{ + jsonArray.add(config.getDomain()+"/links/"+entry.getKey()); + img.setImgurl("http://"+IPPortUtil.getLocalIP()+":"+IPPortUtil.getLocalPort()+"/links/"+entry.getKey());//图片链接 + } + }else{ + jsonArray.add(entry.getKey()); + img.setImgurl(entry.getKey());//图片链接 + } + img.setUpdatetime(times); img.setSource(key.getStorageType()); if (u == null) { @@ -247,10 +265,10 @@ public class UpdateImgController { m = ossImageupload.ImageuploadOSS(null, username,map); }else if(key.getStorageType()==3){ m = ussImageupload.ImageuploadUSS(null, username,map); - //初始化又拍 }else if(key.getStorageType()==4){ - //初始化七牛云 m = kodoImageupload.ImageuploadKODO(null, username,map); + }else if(key.getStorageType()==5){ + m = LocUpdateImg.ImageuploadLOC(null,username,map); }else{ System.err.println("未获取到对象存储参数,上传失败。"); } @@ -259,8 +277,18 @@ public class UpdateImgController { String times = df.format(new Date()); System.out.println("上传图片的时间是:"+times); for (Map.Entry entry : m.entrySet()) { - jsonArray.add(entry.getKey()); - img.setImgurl(entry.getKey());//图片链接 + if(key.getStorageType()==5){ + if(config.getDomain()!=null){ + jsonArray.add(config.getDomain()+"/links/"+entry.getKey()); + img.setImgurl(config.getDomain()+"/links/"+entry.getKey());//图片链接 + }else{ + jsonArray.add(config.getDomain()+"/links/"+entry.getKey()); + img.setImgurl("http://"+IPPortUtil.getLocalIP()+":"+IPPortUtil.getLocalPort()+"/links/"+entry.getKey());//图片链接 + } + }else{ + jsonArray.add(entry.getKey()); + } + //img.setImgurl(entry.getKey());//图片链接 img.setUpdatetime(times); img.setSource(key.getStorageType()); if (u == null) { @@ -316,10 +344,10 @@ public class UpdateImgController { m = ossImageupload.ImageuploadOSS(null, username,map); }else if(key.getStorageType()==3){ m = ussImageupload.ImageuploadUSS(null, username,map); - //初始化又拍 }else if(key.getStorageType()==4){ - //初始化七牛云 m = kodoImageupload.ImageuploadKODO(null, username,map); + }else if(key.getStorageType()==5){ + m =LocUpdateImg.ImageuploadLOC(null,username, map); }else{ System.err.println("未获取到对象存储参数,上传失败。"); } @@ -328,8 +356,18 @@ public class UpdateImgController { String times = df.format(new Date()); System.out.println("上传图片的时间是:"+times); for (Map.Entry entry : m.entrySet()) { - jsonArray.add(entry.getKey()); - img.setImgurl(entry.getKey());//图片链接 + if(key.getStorageType()==5){ + if(config.getDomain()!=null){ + jsonArray.add(config.getDomain()+"/links/"+entry.getKey()); + img.setImgurl(config.getDomain()+"/links/"+entry.getKey());//图片链接 + }else{ + jsonArray.add(config.getDomain()+"/links/"+entry.getKey()); + img.setImgurl("http://"+IPPortUtil.getLocalIP()+":"+IPPortUtil.getLocalPort()+"/links/"+entry.getKey());//图片链接 + } + }else{ + jsonArray.add(entry.getKey()); + } + //img.setImgurl(entry.getKey());//图片链接 img.setUpdatetime(times); img.setSource(key.getStorageType()); if (u == null) { diff --git a/src/main/java/cn/hellohao/test/test1.java b/src/main/java/cn/hellohao/test/test1.java new file mode 100644 index 0000000..4030982 --- /dev/null +++ b/src/main/java/cn/hellohao/test/test1.java @@ -0,0 +1,28 @@ +package cn.hellohao.test; + +import cn.hellohao.utils.Print; + +import java.io.File; + +public class test1 { + private static void deleteDirectory(File file) { + if (file.isFile()) {// 表示该文件不是文件夹 + file.delete(); + } else { + // 首先得到当前的路径 + String[] childFilePaths = file.list(); + for (String childFilePath : childFilePaths) { + String filePath =File.separator + "HellohaoData" + File.separator; + File childFile = new File(filePath + "admin"+ File.separator); + deleteDirectory(childFile); + } + file.delete(); + } + } + + public static void main(String[] args) { + + Print.warning(System.getProperty("user.dir")); + + } +} diff --git a/src/main/java/cn/hellohao/utils/IPPortUtil.java b/src/main/java/cn/hellohao/utils/IPPortUtil.java new file mode 100644 index 0000000..f17dabc --- /dev/null +++ b/src/main/java/cn/hellohao/utils/IPPortUtil.java @@ -0,0 +1,54 @@ +package cn.hellohao.utils; + +import javax.management.MBeanServer; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; +import javax.management.Query; +import java.lang.management.ManagementFactory; +import java.net.InetAddress; +import java.util.Set; + + +public class IPPortUtil { + + public static void main(String[] args)throws Exception { + Print.warning(IPPortUtil.getLocalIP()); + Print.warning(IPPortUtil.getLocalPort()); + } + /** + * @return + * @throws MalformedObjectNameException + * 获取当前机器的端口号 + */ + public static String getLocalPort() throws MalformedObjectNameException { + MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer(); + Set objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), + Query.match(Query.attr("protocol"), Query.value("HTTP/1.1"))); + String port = objectNames.iterator().next().getKeyProperty("port"); + return port; + } + + /** + * @return + * 获取当前机器的IP + */ + public static String getLocalIP() { + InetAddress addr = null; + try { + addr = InetAddress.getLocalHost(); + } catch (Exception e) { + e.printStackTrace(); + } + + + byte[] ipAddr = addr.getAddress(); + String ipAddrStr = ""; + for (int i = 0; i < ipAddr.length; i++) { + if (i > 0) { + ipAddrStr += "."; + } + ipAddrStr += ipAddr[i] & 0xFF; + } + return ipAddrStr; + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 03c3d2f..3e87008 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,9 +1,9 @@ #ݿ˺picturebed -spring.datasource.username=root +spring.datasource.username=picturebed #ݿ -spring.datasource.password=root +spring.datasource.password=Aa923453645 #ݿӵַ -spring.datasource.url=jdbc:mysql://localhost:3306/picturebed?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 +spring.datasource.url=jdbc:mysql://129.28.173.126:3306/picturebed?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 #˿ server.port=8088 @@ -16,9 +16,10 @@ spring.jackson.time-zone=GMT+8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.thymeleaf.cache=false -spring.http.multipart.location=/data/upload_tmp multipart.maxFileSize=10240KB multipart.maxRequestSize=10240KB +spring.thymeleaf.mode = LEGACYHTML5 +spring.http.multipart.location=/data/upload_tmp #ʾֻ֤󣬲ûӦ #spring.resources.cache.cachecontrol.no-cache=true #ʾκ¶Ӧ diff --git a/src/main/resources/static/webuploade/js/upload.js b/src/main/resources/static/webuploade/js/upload.js index bf2f72a..47c924f 100644 --- a/src/main/resources/static/webuploade/js/upload.js +++ b/src/main/resources/static/webuploade/js/upload.js @@ -148,7 +148,7 @@ formData: { uid: 123 }, - dnd: '#dndArea', + dnd: '#wrapper', paste: '#wrapper', swf: 'https://hellohao-cloud.oss-cn-beijing.aliyuncs.com/Uploader.swf', chunked: false,//分片上传 diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html new file mode 100644 index 0000000..ccb95dd --- /dev/null +++ b/src/main/resources/templates/404.html @@ -0,0 +1,62 @@ + + + + + Page Not Found + + + + +

404

+
+

Page Not Found

+

Sorry, but the page you were trying to view does not exist. GO HOME

+ + + \ No newline at end of file diff --git a/src/main/resources/templates/admin/storageconfig.html b/src/main/resources/templates/admin/storageconfig.html index d18501f..f96dc83 100644 --- a/src/main/resources/templates/admin/storageconfig.html +++ b/src/main/resources/templates/admin/storageconfig.html @@ -18,6 +18,8 @@
+ @@ -492,7 +493,9 @@ source = '又拍USS'; } else if (rows.source == 4) { source = '七牛KODO'; - } else { + } else if (rows.source == 5) { + source = '本地存储'; + }else { source = '未知'; } return source; diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 46f60ac..99f5d4d 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -8,7 +8,7 @@ - + @@ -22,7 +22,7 @@ - + @@ -32,17 +32,13 @@ - +