diff --git a/src/main/java/live/tomey/api/express/controller/NewsController.java b/src/main/java/live/tomey/api/express/controller/NewsController.java index 585f5b1..1288862 100644 --- a/src/main/java/live/tomey/api/express/controller/NewsController.java +++ b/src/main/java/live/tomey/api/express/controller/NewsController.java @@ -7,8 +7,11 @@ import live.tomey.api.express.entity.News; import live.tomey.api.express.service.NewsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; import java.util.List; /** @@ -22,6 +25,39 @@ public class NewsController { @Autowired private NewsService newsService; + + + @PassToken + @PostMapping("/addnews") + private Result> addNews(@RequestParam("file")MultipartFile file,@RequestParam("title")String title,@RequestParam("content") String content){ + try { + if (file.isEmpty()){ + return new Result<>(false,-1,"新增失败,请选择文件!"); + } + String fileName = file.getOriginalFilename(); +// String filePath = "F:\\Work\\file\\"; + String filePath = "/www/wwwroot/express/file/"; + String url = "http://47.96.37.195:8084/"; + //确保有这个文件目录才能使用 + File dect = new File(filePath,fileName); + News news = new News(); + try { + file.transferTo(dect); + news.setTitle(title); + news.setContent(content); + news.setImgUrl(url+fileName); + newsService.addNews(news); + }catch (IOException e){ + return new Result<>(false, -1, e.getMessage()+"文件错误"); + } + return new Result<>(true,1,url+fileName); + }catch (RuntimeException e){ + System.out.println(e.getMessage()); + throw new RuntimeException(e); +// return new Result<>(false, -1, e.getMessage()+"系统错误"); + } + } + @PassToken @PostMapping private Result> getList(@RequestBody News news) { diff --git a/src/main/java/live/tomey/api/express/interceptor/AuthenticationInterceptor.java b/src/main/java/live/tomey/api/express/interceptor/AuthenticationInterceptor.java index d1af4d4..dc21e80 100644 --- a/src/main/java/live/tomey/api/express/interceptor/AuthenticationInterceptor.java +++ b/src/main/java/live/tomey/api/express/interceptor/AuthenticationInterceptor.java @@ -38,6 +38,7 @@ public class AuthenticationInterceptor extends HandlerInterceptorAdapter { if (!(handler instanceof HandlerMethod)) { return true; } + System.out.println(((HandlerMethod) handler).getMethod().getName()); HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); //检查是否有passtoken注释,有则跳过认证 @@ -51,7 +52,7 @@ public class AuthenticationInterceptor extends HandlerInterceptorAdapter { JSONObject res = new JSONObject(); if (token == null) { res.put("success", false); - res.put("msg", "缺少必要参数"); + res.put("msg", "缺少token参数"); res.put("errorCode", -1); response.getWriter().write(res.toString()); return false; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 20b2d85..5c96611 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -24,9 +24,9 @@ spring.servlet.multipart.maxRequestSize=200MB # 数据库dataSource jdbc.driver=8zQIdS8RzOBmd4qRctgYdYM/fdvrjnIJHyQH1bD/jNs= #服务器本地127.0.0.1 -jdbc.url=jdbc:mysql://127.0.0.1:3306/mall?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true +jdbc.urls=jdbc:mysql://127.0.0.1:3306/mall?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true #azure -jdbc.urls=jdbc:mysql://40.83.126.15:3306/mall?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true +jdbc.url=jdbc:mysql://13.75.92.40:3306/mall?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true #服务器ip jdbc.username=mall jdbc.password=dr4ap4dbnyCMZTcr @@ -36,6 +36,15 @@ mybatis_config_file=mybatis-config.xml mapper_path=/mapper/**.xml typealias_package=live.tomey.api.express.entity +# 上传文件总的最大值 +spring.servlet.multipart.max-request-size=10MB +# 单个文件的最大值 +spring.servlet.multipart.max-file-size=10MB + +## jsp +spring.mvc.view.prefix=/WEB-INF/jsp/ +spring.mvc.view.suffix=.jsp + #Redis配置 redis.hostname=34.92.103.14 redis.port=6379