From a26d1b5203d202e3490b185b0d1fd40259f19a5b Mon Sep 17 00:00:00 2001 From: Kane Wang Date: Fri, 30 Sep 2022 11:30:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E5=92=8Ccontroller=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E3=80=82=20=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6=E7=9B=AE=E5=BD=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/java/car_dealer/.hintrc | 3 ++- .../fileupload/FileUploadController.java | 25 +++++++++++-------- .../main/webapp/WEB-INF/classes/spring.xml | 4 +-- .../src/main/webapp/WEB-INF/web.xml | 2 +- .../src/main/webapp/file_upload.html | 7 +++--- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/code/java/car_dealer/.hintrc b/code/java/car_dealer/.hintrc index 5b02c11..0eef3be 100644 --- a/code/java/car_dealer/.hintrc +++ b/code/java/car_dealer/.hintrc @@ -8,6 +8,7 @@ { "label": "off" } - ] + ], + "no-inline-styles": "off" } } \ No newline at end of file diff --git a/code/java/car_dealer/src/main/java/com/cpic/xim/car_dealer/controllers/fileupload/FileUploadController.java b/code/java/car_dealer/src/main/java/com/cpic/xim/car_dealer/controllers/fileupload/FileUploadController.java index 7f22c0e..ebaafe4 100644 --- a/code/java/car_dealer/src/main/java/com/cpic/xim/car_dealer/controllers/fileupload/FileUploadController.java +++ b/code/java/car_dealer/src/main/java/com/cpic/xim/car_dealer/controllers/fileupload/FileUploadController.java @@ -2,7 +2,7 @@ * @Author: Kane * @Date: 2022-09-28 15:47:02 * @LastEditors: Kane - * @LastEditTime: 2022-09-30 00:33:41 + * @LastEditTime: 2022-09-30 10:45:27 * @FilePath: \car_dealer\src\main\java\com\cpic\xim\car_dealer\controllers\fileupload\FileUploadController.java * @Description: * @@ -25,10 +25,11 @@ import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; @Controller -@Scope( "session" ) +@Scope( "session") +@RequestMapping( path = "/upload") public class FileUploadController { - @RequestMapping( value = "/test" ) + @RequestMapping( value = "/test.do") public void test() throws ServletException, IOException { // PrintWriter writer = response.getWriter(); @@ -42,20 +43,22 @@ public class FileUploadController * @param uploadFile * @return FileUploadResult *****************************************************/ - @RequestMapping( path = "/upload" ) + @RequestMapping( path = "/upload_file.do") @ResponseBody @CrossOrigin - public FileUploadResult uploadFile( HttpServletRequest request, MultipartFile uploadFile, - String test ) throws IllegalSelectorException, IOException + public FileUploadResult uploadFile( HttpServletRequest request, HttpServletResponse response, + MultipartFile uploadFile, String test ) throws IllegalSelectorException, IOException { // response.setContentType( "text/json;charset=UTF-8" ); // response.setHeader( "Access-Control-Allow-Origin", "*" ); // response.setHeader( "Access-Control-Allow-Methods", "GET,POST" ); + request.getCookies(); + FileUploadResult result = new FileUploadResult(); String sessionID = request.getSession().getId(); - if ( uploadFile == null ) + if ( uploadFile == null) { result.setSuccess( false ); result.setMessage( "请用于上传文件!" ); @@ -63,7 +66,7 @@ public class FileUploadController return result; } - if ( uploadFile.isEmpty() ) + if ( uploadFile.isEmpty()) { result.setSuccess( false ); result.setMessage( "上传空文件!" ); @@ -72,16 +75,16 @@ public class FileUploadController } // 使用sessionID来作为存放的目录 - String rootPath = request.getServletContext().getRealPath( "/" + sessionID ); + String rootPath = request.getServletContext().getRealPath( "/temp/upload/" + sessionID ); String fileName = uploadFile.getOriginalFilename(); File destFile = new File( rootPath, fileName ); // 判断路径是否存在,不存在就创建。 - if ( !destFile.getParentFile().exists() ) + if ( !destFile.getParentFile().exists()) { // 创建路径 - if ( !destFile.getParentFile().mkdirs() ) + if ( !destFile.getParentFile().mkdirs()) { // 如果创建路径失败 result.setSuccess( false ); diff --git a/code/java/car_dealer/src/main/webapp/WEB-INF/classes/spring.xml b/code/java/car_dealer/src/main/webapp/WEB-INF/classes/spring.xml index 3078e41..67ae7ed 100644 --- a/code/java/car_dealer/src/main/webapp/WEB-INF/classes/spring.xml +++ b/code/java/car_dealer/src/main/webapp/WEB-INF/classes/spring.xml @@ -19,8 +19,8 @@ --> - - + + \ No newline at end of file diff --git a/code/java/car_dealer/src/main/webapp/WEB-INF/web.xml b/code/java/car_dealer/src/main/webapp/WEB-INF/web.xml index cde9b1b..c9bf52f 100644 --- a/code/java/car_dealer/src/main/webapp/WEB-INF/web.xml +++ b/code/java/car_dealer/src/main/webapp/WEB-INF/web.xml @@ -15,7 +15,7 @@ springmvc - / + *.do \ No newline at end of file diff --git a/code/java/car_dealer/src/main/webapp/file_upload.html b/code/java/car_dealer/src/main/webapp/file_upload.html index 89bf4a7..2388181 100644 --- a/code/java/car_dealer/src/main/webapp/file_upload.html +++ b/code/java/car_dealer/src/main/webapp/file_upload.html @@ -2,7 +2,7 @@ * @Author: Kane * @Date: 2022-09-29 16:33:30 * @LastEditors: Kane - * @LastEditTime: 2022-09-30 00:33:12 + * @LastEditTime: 2022-09-30 10:26:34 * @FilePath: \car_dealer\src\main\webapp\file_upload.html * @Description: * @@ -22,7 +22,7 @@