spring 3上传了很多文件

时间:2010-01-07 02:19:51

标签: spring-mvc

是的,我们的客户想要上传多个文件。 我们使用春天3 mvc。 像这样的官方例子:

标记:

<form method="post" action="/form" enctype="multipart/form-data">
    <input type="text" name="name"/>
    <input type="file" name="file"/>
    <input type="submit"/>
</form>

代码:

@RequestMapping(value = "/form", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,
                               @RequestParam("file") MultipartFile file) {

    if (!file.isEmpty()) {
        byte[] bytes = file.getBytes();

        // store the bytes somewhere
        return "redirect:uploadSuccess";
    } else {
        return "redirect:uploadFailure";
    }
}

只有一个文件,所以我可以在方法中写入文件输入名称。 但如果我想上传很多文件,我该怎么办? 我无法写入所有文件输入名称,因为如果是由js代码生成的。 我只知道它的名字就像'attach_' 那么,我该怎么写这个方法呢?如果我这样写

@RequestParam() MultipartFile file

@RequestParam("attach_") MultipartFile file

我会收到错误。

5 个答案:

答案 0 :(得分:9)

我使用Spring 3.0.4(在以前的Spring版本中存在问题,所以一定要使用&gt; = 3.0.4)。

要测试它,您可以使用以下步骤:

public class MultiPartFileUploadBean {

    private List<MultipartFile> files;

    public void setFiles(List<MultipartFile> files) {
        this.files = files;
    }

    public List<MultipartFile> getFiles() {
        return files;
    }
}

控制器:

@RequestMapping(value = "/uploadtest", method = RequestMethod.POST)
public String uploadtestProcess(MultiPartFileUploadBean file, BindingResult bindingResult,
        Model model) throws IOException {
    ... // binding check
    StringBuilder sb = new StringBuilder();
    List<MultipartFile> files = file.getFiles();
    for(MultipartFile f:files)
        sb.append(String.format("File: %s, contains: %s<br/>\n",f.getOriginalFilename(),new String(f.getBytes())));
    String content = sb.toString();
    model.addAttribute("content", content);
    return "uploadtest";
}

jsp:

<form method="post" action="/uploadtest" enctype="multipart/form-data">
<p>Type: <input type="text" name="type" value="multiPartFileSingle" size="60" /></p>
<p>File 1: <input type="file" name="files[0]" size="60" /></p>
<p>File 2: <input type="file" name="files[1]" size="60" /></p>
<p><input type="submit" value="Upload" /></p>
</form>
<c:if test="${not empty content}">
<p>The content uploaded: <br/>${content}</p>

答案 1 :(得分:9)

更简单的方法 - 适合我

/*** Upload Images ***/
@RequestMapping(value = "/images/upload", method = RequestMethod.POST)
public void upload(@RequestParam("file") List<MultipartFile> files, @RequestParam("user") String user) {

    files.forEach((file -> System.out.println(file.getOriginalFilename())));

}

答案 2 :(得分:1)

我发现使用MultipartHttpServletRequest对象作为控制器方法的参数更清楚:

@RequestMapping(value = "/save", method=RequestMethod.POST)
protected String save(Model model, MultipartHttpServletRequest multipartRequest) {
    MultipartFile file = multipartRequest.getFile("field-name");
    // Also multiple files with same name
    List<MultipartFile> files = multipartRequest.getFiles("multifield-name");
    // ...
}

链接到文档:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-multipart-resolver-commons

答案 3 :(得分:0)

您使用模型和表单。

(Html / Jsp)

<form id="uploadForm" method="POST"enctype="multipart/form-data/charset=UTF-8">
     //...multi file, add dynamic input
     <input type="file" name="file"/>
     <input type="file" name="file"/>
     <input type="file" name="file"/>
     <input type="file" name="file"/>
</form>
<input type="button" id="save_button" value="save" />

(JS)

var form = new FormData(document
        .getElementById('uploadForm'));

$.ajax({
    url : "/test/upload/file,
    type : 'POST',
    dataType : 'text',
    data : form,

    processData : false,
    contentType : false,

    success : function(response) {
        if (response == "success") {
            document.location.reload(true);

        } else {
            $("#editMsg").text("fail");
        }
    },
    error : function(request, status, error) {

    }
});

(模特)

public class fileModel {
   private List<MultipartFile> file; // this name = input name

  ... setter, getter
}

(财务主任)

@RequestMapping(value = "/upload/file", method = RequestMethod.POST)
public @ResponseBody String uploadFiles(fileModel model, HttpServletRequest req) {

    return "success" // <-- debug. break point !! Your watch model. 
}

答案 4 :(得分:-1)