diff --git a/communityservice/.gitignore b/communityservice/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..a2a3040aa86debfd8826d9c2b5c816314c17d9fe --- /dev/null +++ b/communityservice/.gitignore @@ -0,0 +1,31 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/** +!**/src/test/** + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ + +### VS Code ### +.vscode/ diff --git a/communityservice/pom.xml b/communityservice/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e7539512751c1ab5b895ccc7ec508a08c75f646 --- /dev/null +++ b/communityservice/pom.xml @@ -0,0 +1,114 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.5.RELEASE + + + com.woniu + communityservice + 0.0.1-SNAPSHOT + communityservice + Demo project for Spring Boot + + + 1.8 + Hoxton.SR3 + + + + + org.springframework.boot + spring-boot-starter-data-redis + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.1.2 + + + org.springframework.cloud + spring-cloud-starter-netflix-eureka-client + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + mysql + mysql-connector-java + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + + + com.qiniu + qiniu-java-sdk + [7.2.0, 7.2.99] + + + com.google.code.gson + gson + 2.8.6 + + + + commons-fileupload + commons-fileupload + 1.3.3 + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/communityservice/src/main/java/com/woniu/CommunityserviceApplication.java b/communityservice/src/main/java/com/woniu/CommunityserviceApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..fa8936337517df620e950ae66240eb6986ea9a2c --- /dev/null +++ b/communityservice/src/main/java/com/woniu/CommunityserviceApplication.java @@ -0,0 +1,15 @@ +package com.woniu; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; + +@SpringBootApplication +@EnableEurekaClient //开启eureka客户端 +public class CommunityserviceApplication { + + public static void main(String[] args) { + SpringApplication.run(CommunityserviceApplication.class, args); + } + +} diff --git a/communityservice/src/main/java/com/woniu/config/UploadConfig.java b/communityservice/src/main/java/com/woniu/config/UploadConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..1635a41c15d2727cc975e0f9c058d5ec12339380 --- /dev/null +++ b/communityservice/src/main/java/com/woniu/config/UploadConfig.java @@ -0,0 +1,23 @@ +package com.woniu.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.multipart.MultipartResolver; +import org.springframework.web.multipart.commons.CommonsMultipartResolver; + +@Configuration +public class UploadConfig { + + /** + * 设置上传文件限制 + * @return + */ + @Bean + public MultipartResolver multipartResolver(){ + CommonsMultipartResolver resolver = new CommonsMultipartResolver(); + resolver.setDefaultEncoding("UTF-8"); + resolver.setMaxUploadSize(3000*1024); + resolver.setResolveLazily(true); + return resolver; + } +} diff --git a/communityservice/src/main/java/com/woniu/config/WebMvcConfig.java b/communityservice/src/main/java/com/woniu/config/WebMvcConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..690eef6e1a2cd196d5f897d54ee183255e366114 --- /dev/null +++ b/communityservice/src/main/java/com/woniu/config/WebMvcConfig.java @@ -0,0 +1,14 @@ +package com.woniu.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("community").setViewName("/community"); + } +} diff --git a/communityservice/src/main/java/com/woniu/controller/CommunityController.java b/communityservice/src/main/java/com/woniu/controller/CommunityController.java new file mode 100644 index 0000000000000000000000000000000000000000..9b2e3bea33ac8eff3dce9809608f77c45ae0f9e4 --- /dev/null +++ b/communityservice/src/main/java/com/woniu/controller/CommunityController.java @@ -0,0 +1,11 @@ +package com.woniu.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/community") +public class CommunityController { + + +} diff --git a/communityservice/src/main/java/com/woniu/controller/DynamicController.java b/communityservice/src/main/java/com/woniu/controller/DynamicController.java new file mode 100644 index 0000000000000000000000000000000000000000..c8d0d53615f735e2468fdb30c7de3ca6d8353a41 --- /dev/null +++ b/communityservice/src/main/java/com/woniu/controller/DynamicController.java @@ -0,0 +1,54 @@ +package com.woniu.controller; + +import com.woniu.result.CommonResult; +import com.woniu.util.QiniuUploadUtil; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.util.UUID; + +@Controller +@RequestMapping("/dynamic") +public class DynamicController { + + + /** + * 文件上传 + * @param file 文件数组 + * @return + */ + @PostMapping("/upload") + @ResponseBody + public CommonResult upload(MultipartFile file){ + System.out.println("_________________"); + if(file==null){ + return CommonResult.failed("请选择上传图片"); + } + try { + //返回的路径,即可存入数据库,即可以在网上直接访问(new QiniuUploadUtil().upload)调取Util里面方法; + //QiniuUploadUtil工具类不需要做任何修改 + //UUID获取文件名(自己编写) + String fileName = UUID.randomUUID().toString(); + String imgurl = new QiniuUploadUtil().upload(fileName, file.getBytes()); +// System.out.println(imgurl); +// Map map=new HashMap(); +// map.put("src",imgurl); + return CommonResult.success(imgurl); + } catch (IOException e) { + e.printStackTrace(); + return CommonResult.failed("上传失败"); + } + } + + @RequestMapping("/test") + @ResponseBody + public String test(Integer aa){ + System.out.println(aa); + System.out.println("*******"); + return "tttt"; + } +} diff --git a/communityservice/src/main/java/com/woniu/result/CommonResult.java b/communityservice/src/main/java/com/woniu/result/CommonResult.java new file mode 100644 index 0000000000000000000000000000000000000000..5376338f0c81614f7ea9fd2543e447697ad79c93 --- /dev/null +++ b/communityservice/src/main/java/com/woniu/result/CommonResult.java @@ -0,0 +1,107 @@ +package com.woniu.result; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class CommonResult implements Serializable { + private Long code; + private String msg; + private Long count; + T data; + + public CommonResult(){} + public CommonResult(Long code,String message){ + this.code = code; + this.msg = message; + } + + public CommonResult(Long code ,String message,T data){ + this.code = code; + this.msg = message; + this.data = data; + } + + + /** + * 响应成功的结果 + * @return 状态码 200 + */ + public static CommonResult success(){ + return new CommonResult(Result.SUCCESS.getCode(),Result.SUCCESS.getMessage()); + } + + public static CommonResult success(String message){ + return new CommonResult(Result.SUCCESS.getCode(),message); + } + + public static CommonResult success(T data){ + return new CommonResult(Result.SUCCESS.getCode(),Result.SUCCESS.getMessage(),data); + } + public static CommonResult success(String message,T data){ + return new CommonResult(Result.SUCCESS.getCode(),message,data); + } + + /** + * 响应失败 + * @return 状态码500 + */ + public static CommonResult failed(){ + return new CommonResult(Result.FAILED.getCode(),Result.FAILED.getMessage()); + } + + public static CommonResult failed(String message){ + return new CommonResult(Result.FAILED.getCode(),message); + } + /** + * 参数验证失败 + * @return 状态码 400 + */ + public static CommonResult valetateFailed(){ + return new CommonResult(Result.VALIDATE_FAILED.getCode(),Result.VALIDATE_FAILED.getMessage()); + } + public static CommonResult valetateFailed(String msg){ + return new CommonResult(Result.VALIDATE_FAILED.getCode(),msg); + } + + /** + * 未认证 + * @return 状态码401 + */ + public static CommonResult unathorizedFailed(){ + return new CommonResult(Result.UNAUTORIED.getCode(), Result.UNAUTORIED.getMessage()); + } + + /** + * 未授权 + * @return 状态码403 + */ + public static CommonResult forbiddenFailed(){ + return new CommonResult(Result.FORBIDDEN.getCode(),Result.FORBIDDEN.getMessage()); + } + + public Long getCode() { + return code; + } + + public void setCode(Long code) { + this.code = code; + } + + public String getMessage() { + return msg; + } + + public void setMessage(String message) { + this.msg = message; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } +} diff --git a/communityservice/src/main/java/com/woniu/result/Result.java b/communityservice/src/main/java/com/woniu/result/Result.java new file mode 100644 index 0000000000000000000000000000000000000000..9470abee595a6936f2e1692f95a4e395ac9f6129 --- /dev/null +++ b/communityservice/src/main/java/com/woniu/result/Result.java @@ -0,0 +1,32 @@ +package com.woniu.result; + +public enum Result { + SUCCESS(200L,"操作成功"), + FAILED(500L,"操作失败"), + VALIDATE_FAILED(400L,"参数验证失败"), + UNAUTORIED(401L,"未认证或token过期"), + FORBIDDEN(403L,"未授权"); + private Long code; + private String message; + + private Result(Long code,String message){ + this.code = code; + this.message = message; + } + + public Long getCode() { + return code; + } + + public void setCode(Long code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/communityservice/src/main/java/com/woniu/util/QiniuUploadUtil.java b/communityservice/src/main/java/com/woniu/util/QiniuUploadUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..00ef71ee13c5b823d13d4b7b017d79f40e6e54c8 --- /dev/null +++ b/communityservice/src/main/java/com/woniu/util/QiniuUploadUtil.java @@ -0,0 +1,46 @@ +package com.woniu.util; + +import com.google.gson.Gson; +import com.qiniu.http.Response; +import com.qiniu.storage.Configuration; +import com.qiniu.storage.Region; +import com.qiniu.storage.UploadManager; +import com.qiniu.storage.model.DefaultPutRet; +import com.qiniu.util.Auth; + +import java.util.Date; + +/** + * 七牛云文件上传工具类 + */ +public class QiniuUploadUtil { + private static final String accessKey = "4g3yhrqRdZrSmDzobCwKlKLPCVMkt0X5l8ah52L1"; + private static final String secretKey = "TAsmCWOeGIO8Va9WjlcpMuexoEE-f-fXTiC4MaCi"; + private static final String bucket = "psrson-centor"; + private static final String prix = "http://q7fucxp9q.bkt.clouddn.com/"; + private UploadManager manager; + + public QiniuUploadUtil() { + //初始化基本配置 + Configuration cfg = new Configuration(Region.region2()); + //创建上传管理器 + manager = new UploadManager(cfg); + } + + //文件名 = key + //文件的byte数组 + public String upload(String imgName , byte [] bytes) { + Auth auth = Auth.create(accessKey, secretKey); + //构造覆盖上传token + String upToken = auth.uploadToken(bucket,imgName); + try { + Response response = manager.put(bytes, imgName, upToken); + DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); + //返回请求地址 + return prix+putRet.key+"?t="+new Date().getTime(); + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } +} diff --git a/communityservice/src/main/resources/application.yml b/communityservice/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..f5f570008c71c321dd93a86ba5ab4147367fc01e --- /dev/null +++ b/communityservice/src/main/resources/application.yml @@ -0,0 +1,24 @@ +spring: + datasource: + password: 20200322 + username: develop + url: jdbc:mysql://106.12.148.100:3307/happycommunity + driver-class-name: com.mysql.jdbc.Driver + jackson: + time-zone: GMT+8 + date-format: yyyy-MM-dd + redis: + host: localhost + port: 6379 + application: + name: community +mybatis: + configuration: + map-underscore-to-camel-case: true + mapper-locations: classpath:mapper/*.xml +server: + port: 8011 +eureka: + client: + service-url: + defaultZone: http://localhost:8761/eureka \ No newline at end of file diff --git a/communityservice/src/test/java/com/woniu/communityservice/CommunityserviceApplicationTests.java b/communityservice/src/test/java/com/woniu/communityservice/CommunityserviceApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..3397cbaeb4a456488bb9813276fbcb6db1450a21 --- /dev/null +++ b/communityservice/src/test/java/com/woniu/communityservice/CommunityserviceApplicationTests.java @@ -0,0 +1,13 @@ +package com.woniu.communityservice; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class CommunityserviceApplicationTests { + + @Test + void contextLoads() { + } + +}