1

看来我的情况很简单,所以我猜这个问题很愚蠢。尽管如此......我没有看到它。

我正在尝试接受文件集合的上传,并一直在查看这篇文章作为指南:

http://viralpatel.net/blogs/spring-mvc-multiple-file-upload-example/

这是表单上传客户端:

<form:form method="post" action="save.html" modelAttribute="uploadArtifact" enctype="multipart/form-data">
    <div class="controls" id="artifacts">                               
        <input type="file" id="uploader" class="multi"/>        
    </div>
    <div>
        <input type="submit" value="Upload" />
    </div>
</form:form>

这是模型/ Bean 代码:

public class ArtifactModel {

private List<MultipartFile> artifacts;

public List<MultipartFile> getArtifacts() {
    return artifacts;
}

public void setArtifacts(List<MultipartFile> artifacts) {
    this.artifacts = artifacts;
}
}

...和控制器代码:

@Controller
public class ArtifactController {

    private static Log logger = LogFactory.getLog(ArtifactController.class);

    // @Autowired
    // ArtifactService artifactService;

    @RequestMapping(value = "/getUploadDialog", method = RequestMethod.GET)
    public String displayForm() {
        return "uploadArtifacts";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(
            @ModelAttribute("uploadArtifact") ArtifactModel uploadArtifact,
            Model map) {

        logger.info("Received file upload request with     "+uploadArtifact.getArtifacts().size()+" files");
            List<MultipartFile> files = uploadArtifact.getArtifacts();

        List<String> fileNames = new ArrayList<String>();

        if (null != files && files.size() > 0) {
            for (MultipartFile multipartFile : files) {
                String fileName = multipartFile.getOriginalFilename();
                fileNames.add(fileName);
                // Handle file content - multipartFile.getInputStream()
                logger.info("Received file with filename: " + fileName);
            }
        }

        map.addAttribute("files", fileNames);
    }

}

当我尝试调用应该来自 uploadArtifact @ModelAttribute 参数的 ArtifactModel 对象上的 getter 时,我得到了 NullPointerException ...

我查看了浏览器调试器,发现客户端正在发送数据......

建议?

4

1 回答 1

2

我认为您的文件输入元素中缺少一个属性。

尝试添加:

name='artifacts[0]'
于 2013-06-13T13:03:05.613 回答