Java 如何在SpringMVC中上传任何文件并将其保存到特定文件夹

Java 如何在SpringMVC中上传任何文件并将其保存到特定文件夹,java,javascript,spring,jsp,spring-mvc,Java,Javascript,Spring,Jsp,Spring Mvc,我有一个对话框,在这里我可以选择上传一个文件,但它不起作用。我的意思是请求不要去管制员那里。我的代码如下所示: 我的JSP Div内容: 对话框的内容是: <form method="post" action="fileUploadForm" enctype="multipart/form-data"> Please select a file to upload :<input type="file" name="fileUpload" id="Uploadfil

我有一个对话框,在这里我可以选择上传一个文件,但它不起作用。我的意思是请求不要去管制员那里。我的代码如下所示: 我的JSP Div内容:

对话框的内容是:

<form method="post" action="fileUploadForm" enctype="multipart/form-data">

    Please select a file to upload :<input type="file" name="fileUpload"  id="Uploadfile"/>
<input type="submit" value="Upload" onclick="Uploadfiles();"/>

    </form>
我的spring Dispatcher servlet xml文件中的更改是: 我在下面添加了一行,只是为了使用multipart

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
请让我知道我的代码中有什么错误,以及我们是否可以使用其他方法获得相同的结果

Spring dispatcher servlet文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">

<bean id="userController" class="com.controller.UserLoginController">
 <property name="multipartResolver" ref="multipartResolver"></property>

</bean>
 <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- max upload size in bytes -->
    <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

    <!-- max size of file in memory (in bytes) -->
    <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

</bean>

我认为控制器方法没有被调用的原因是参数FormDataMultiPart,它是jersey文件上传api。您正在将此API与spring MVC混合使用

我建议您使用MultipartFile API进行上传,如下所示

@RequestMapping(value= "/fileUploadForm" , method = RequestMethod.POST)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public @ResponseBody
String uploadFile(@RequestParam("file") MultipartFile file){
   // your code...
}

如何获取contextpath?您的web.xml配置正确吗?是的,我得到的上下文路径如下,var contexPath=;您将变量用作contexPath,ajax调用中缺少一个t,这可能是问题吗?感谢Ozan纠正我,我已经更改了之前的输入错误,现在仍然请求不去我的控制器,我只想请求应该去控制器,控制器代码中的控制器工作正常。没有调用的控制器对我来说仍然是一个contextpath问题,您确定尝试上载的页面获得了正确的contextPath吗?你能在这里发布你的ajax调用中设置的实际值吗?那么你现在得到的是什么?对不起,我无法理解你的问题,你的意思是对话框中的表单可能会导致问题吗?你说你的Spring API现在是正确的,因为你已经用独立客户端对它进行了测试。如果对话框中的表单会导致任何问题,那么是的。我已经创建了示例SpringMVC项目,我可以上传文件。
 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">

<bean id="userController" class="com.controller.UserLoginController">
 <property name="multipartResolver" ref="multipartResolver"></property>

</bean>
 <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- max upload size in bytes -->
    <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

    <!-- max size of file in memory (in bytes) -->
    <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->

</bean>
@RequestMapping(value= "/fileUploadForm" , method = RequestMethod.POST)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public @ResponseBody
String uploadFile(@RequestParam("file") MultipartFile file){
   // your code...
}