2016-05-10 6 views
0

Ich versuche, eine Multipart-Datei mit PostMan hochladen und Fehler bekommen. Hier ist der Code und Screenshots:Versuchen, MultipartFile mit Postman

http://imgur.com/pZ5cXrh

http://imgur.com/NaWQceO

@RequestMapping(value = "/upload", method = RequestMethod.POST) 
public void uploadFileHandler(@RequestParam("name") String name, 
     @RequestParam("name") MultipartFile file) { 

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

      // Creating the directory to store file 
      //String rootPath = System.getProperty("catalina.home"); 
      String rootPath = "C:\\Desktop\\uploads"; 
      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(); 

      System.out.println("Server File Location=" 
        + serverFile.getAbsolutePath()); 

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

Antwort

2

Sie sollten etwas wie dieses:

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data") 
    public void uploadFileHandler(@RequestParam("name") String name, 
            @RequestParam("file") MultipartFile file) { 

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

       // Creating the directory to store file 
       //String rootPath = System.getProperty("catalina.home"); 
       String rootPath = "C:\\Users\\mworkman02\\Desktop\\uploads"; 
       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(); 

       System.out.println("Server File Location=" 
         + serverFile.getAbsolutePath()); 

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

Beachten Sie consumes = "multipart/form-data". Es ist notwendig für Ihre hochgeladene Datei, weil Sie einen mehrteiligen Anruf haben sollten. Sie sollten @RequestParam("file") MultipartFile file anstelle von @RequestParam("name") MultipartFile file) haben.

Natürlich sollten Sie einen multipartview konfiguriert haben Resolver die eingebaute Unterstützung für Apache-Commons-Datei-Upload und nativen Servlet 3.

+0

Danke, ich, dass ein Teil hinzugefügt. Es sieht so aus, als ob ich die Anfrage nicht richtig sende. Ich bin mir nicht sicher, wie ich das machen soll. – mw02

+0

Wenn ich sehe, füge ich multipart/form-data in consume hinzu und ändere die @RequestParam ("name") MultipartFile Datei in @RequestParam ("Datei") MultipartFile Datei. In der Tat, wenn Sie den Fehler sehen, dass Reqiured MultipartFile Paramter Name. Sie sollten den mehrteiligen Teil mit Mähne Datei und Name mit Namen als Paramiter Name übergeben. –

+1

Ok, ich habe diese Änderungen vorgenommen. Vielen Dank. Ich musste auch den Content-Type vom Postboten entfernen und dann einen Return-Typ zum Controller hinzufügen. Jetzt funktioniert es. – mw02