如何上传任何文件并将其保存到spring mvc中的特定文件夹中

时间:2014-12-05 14:42:14

标签: 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="Uploadfile"/>
<input type="submit" value="Upload" onclick="Uploadfiles();"/>

    </form>

Java脚本功能: 现在我如何使用表单数据将此请求发送到控制器,并再次成功获得字符串响应,如上传成功。我试过这样但不起作用:

<script type="text/javascript">
function Uploadfiles(){
$('#result').html('');
var formdata=document.getElementById("Uploadfile"); 
var fd = new FormData(formdata);
fd.append("CustomField","This is an additional data");

$.ajax({
    url: contextPath +"/fileUploadForm",
    dataType: 'text',
    data: fd,
    processData: false,
    contentType: false, 
    type: "POST",
    success: function(data){
            alert("inside success ***");
            $('#result').html(data);

    } ,
    error: function(){
        alert("error has occured");

        }
});
}
</script>

因为我不确定控制器代码是什么,因为我是从网站上获得的。如果我使用我在这里提到的我得到的错误,如错误的语法我的意思是由ajax调用发送到控制器的数据在语法上不正确接受表单数据,即ajax调用正在发送什么以及控制器代码期望的问题。请让我知道在ajax调用中实现如何发送文件请求的正确方法应该是什么,这样控制器应该获得正确的数据。由于保存文件代码工作正常,如果我不使用此ajax调用文件正在正确保存。 现在我的控制器代码:

   private static final String KDC_FILE_UPLOAD_LOCATION_FOLDER = "/root/Desktop/Upload_Files/";

  @RequestMapping(value= "/fileUploadForm",method = RequestMethod.POST)
   public String handleFileUpload(HttpServletRequest request,
        @RequestParam CommonsMultipartFile[] fileUpload) throws Exception {

    System.out.println("finally in controller ");

    if (fileUpload != null && fileUpload.length > 0) {
        for (CommonsMultipartFile aFile : fileUpload){

            System.out.println("Saving file: " + aFile.getOriginalFilename());

            if (!aFile.getOriginalFilename().equals("")) {
                aFile.transferTo(new File(FILE_UPLOAD_LOCATION_FOLDER + aFile.getOriginalFilename()));
            }
        }
    }

    // returns to the view "Result"
    return "File has been Uploaded Successfully";
}

我的Spring Dispatcher servlet xml文件中的更改是: 我已添加以下行仅使用multipart

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

请告诉我代码中的错误,以及是否可以使用其他方法获得相同的结果。

Spring调度程序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>

1 个答案:

答案 0 :(得分:0)

我认为没有调用控制器方法的原因是参数FormDataMultiPart,即泽西文件上传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...
}