2015-04-30 53 views
1

我想上傳多個文件到控制器,但它只取一個。上傳多個文件,enctype =「multipart/form-data」

我正在使用multipart進行文件傳輸。 如何獲取多個文件到控制器端。 我無法訪問字節和文件名。它的投擲錯誤,

@RequestMapping(value = "/home/step38/uploadReport", method = RequestMethod.POST) 
public ModelAndView uploadReport(     
     @RequestParam(value = "fileName") List<MultipartFile> files, 
     @RequestParam(value = "pLogId") String pLogId, 
      HttpServletRequest request, HttpSession session) 
{ 
    int applicationNameId = 0; 
    String fileName; 
    String typeOption = "Raw Particle Data"; 
    for(MultipartFile file:files)  
    fileName = file.getOriginalFilename(); 

    logger.debug("step3.1 upload particle count -- Start"); 

    ModelAndView modelAndView = createModelAndView(ToogleStep.step38); 
    setCurrentStep(session, modelAndView, ToogleStep.step38); 
    String view = "redirect:/home/step38"; 
    modelAndView.setViewName(view); 

    try 
    { 
     User user = (User) session.getAttribute(Constants.USER_DB); 
     Project current_project = (Project) session.getAttribute(Constants.PROJECT); 
     Credential credential = (Credential) session.getAttribute(Constants.AUTH_USER);    
     boolean checkOK = true;   

     if (current_project != null && SystemUtils.projectEditable(current_project, credential)) 
     { 
      long projectId = current_project.getId(); 

      if(checkOK) 
      { 
       byte[] bytes = file.getBytes();  
       HashMap<String,String> options= new HashMap<String,String>(); 
       //added pLogId in the options(this will contain demoToogleFileInfoId)   
       options.put(ToogleReportDataConstants.TTL_PROCESS_LOG_ID_OPTION,pLogId); 
       String toogleFileId = reportService.uploadReport(user, projectId, fileName, typeOption, bytes, applicationNameId, options); 

      } 
     }   
    } 

回答

1

你沒有在正確的位置循環。 嘗試環路,你有你的ModelAndView(視圖)後

int applicationNameId = 0; 
     String typeOption = "Raw Particle Data"; 
     ModelAndView modelAndView = createModelAndView(ToogleStep.step38); 
     setCurrentStep(session, modelAndView, ToogleStep.step38); 
     String view = "redirect:/home/step38"; 
     modelAndView.setViewName(view); 

     // upload multiple files.  
      for(MultipartFile file:files){ 
      String fileName= file.getOriginalFilename(); 

,然後你就可以訪問字節和文件名。試試這個。 至少看看你的問題,我可以建議,如果你能給出更具體的錯誤,我可以幫助。

+1

你是一個救世主...對不起,我沒有想到它。謝謝 :) – PSDebugger