2013-06-12 81 views
1

看來我的情況很簡單,所以我猜這個問題很愚蠢。儘管如此......我沒有看到它。與Spring MVC的Multipartfile上的NullPointer

我試圖接受文件的收集上傳,一直在尋找在這篇文章作爲指導:

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

} 

我得到一個NullPointerException當我嘗試撥打本應來自uploadArtifact @ModelAttribute參數ArtifactModel對象上,吸氣......

我在瀏覽器調試器中看過,看到客戶端正在發送數據...

建議?

+2

嘗試添加屬性'名稱= '工件[0]''到文件輸入元件。 – kevin847

+0

這樣做。你爲什麼不把它放在一個答案中,所以我可以給你信貸的地方:)謝謝先生。 – Raevik

+0

太好了,會做的。乾杯。 – kevin847

回答

2

我想你錯過了文件輸入元素中的屬性。

嘗試增加:

name='artifacts[0]'