2016-06-11 2 views
-1

Я всегда получаю эту ошибку, когда я пытаюсь получить доступ к «/ uploadFile»Spring MVC В настоящее время запроса не многочастный запрос

Etat HTTP 500 - Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request 

Вот метод в контроллере

@RequestMapping(value = "/uploadFile", method = RequestMethod.GET) 
    public @ResponseBody 
    String uploadFileHandler(String name,MultipartFile file) { 

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

       // Creating the directory to store file 
       String rootPath = System.getProperty("catalina.home"); 
       File dir = new File(rootPath + File.separator + "tmpFiles"); 
       if (!dir.exists()) 
        dir.mkdirs(); 

       // Create the file on server 
       File serverFile = new File(dir.getAbsolutePath() 
         + File.separator + name); 
       BufferedOutputStream stream = new BufferedOutputStream(
         new FileOutputStream(serverFile)); 
       stream.write(bytes); 
       stream.close(); 

       logger.info("Server File Location=" 
         + serverFile.getAbsolutePath()); 

       return "You successfully uploaded file=" + name; 
      } catch (Exception e) { 
       return "You failed to upload " + name + " => " + e.getMessage(); 
      } 
     } else { 
      return "You failed to upload " + name 
        + " because the file was empty."; 
     } 
    } 

СПЯ форма

<form method="GET" action="uploadFile" enctype="multipart/form-data"> 
    File to upload: <input type="file" name="file"><br /> 
    Name: <input type="text" name="name"><br /> <br /> 
    <input type="submit" value="Upload"> Press here to upload the file! 
</form> 

и я добавили эту web.xml

<listener> 
     <listener-class>org.springframework.web.multipart.commons.CommonsMultipartResolver</listener-class> 
    </listener> 

Что я здесь делаю неправильно? Любая помощь будет очень признательна.

ответ

0
файл

загрузки HTTP шахты должен быть способ применения/форм-данных должен быть пост

<form method="POST" 
method = RequestMethod.POST 
+0

, когда я изменить получить на сообщение я получаю эту ошибку Etat HTTP 405 - метод запроса «GET» не поддерживается –

0
@RequestMapping(...., consumes = {"multipart/*"}) 

Либо указать * или сохранить *