上传多个文件,enctype =“multipart / form-data”

时间:2015-04-30 22:08:31

标签: file http spring-mvc multipartform-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 个答案:

答案 0 :(得分:1)

您没有在正确的位置进行循环。 尝试使用modelAndView(view)

后循环它
 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();

然后您将能够访问字节和文件名。试一试。 至少通过查看你的问题,我可以建议,如果你能给出更具体的错误,我可以提供帮助。