修改了资源访问和controller映射路径。

修改了临时文件目录。
This commit is contained in:
Kane Wang 2022-09-30 11:30:51 +08:00
parent c41bcc6a35
commit a26d1b5203
5 changed files with 23 additions and 18 deletions

View File

@ -8,6 +8,7 @@
{
"label": "off"
}
]
],
"no-inline-styles": "off"
}
}

View File

@ -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 );

View File

@ -19,8 +19,8 @@
</bean> -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- <property name="defaultEncoding" value="UTF-8" /> -->
<property name="maxUploadSize" value="5000000" />
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="-1" />
</bean>
</beans>

View File

@ -15,7 +15,7 @@
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

View File

@ -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 @@
<body>
<div id="app">
<form
action="http://localhost:8080/cardealer/upload"
action="http://localhost:8080/cardealer/upload.do"
id="uploadform"
enctype="multipart/form-data"
method="post"
@ -51,7 +51,8 @@
</div>
</body>
<script>
const reqeustURL = "http://localhost:8080/cardealer/upload";
const reqeustURL =
"http://localhost:8080/cardealer/upload/upload_file.do";
const btnUpload = document.getElementById("btnUpload");
function fileUploadChange() {