2016-05-10 3 views
0

PostManを使用してマルチパートファイルをアップロードしようとしましたが、エラーが発生しました。ここでは、コードとスクリーンショットは以下のとおりです。郵便番号を使用してMultipartFileをアップロードしようとしています

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

答えて

2

あなたはこのようなものが必要です:

@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."); 
     } 
    } 

consumes = "multipart/form-data"に注意してください。マルチパートコールが必要なので、アップロードしたファイルに必要です。 @RequestParam("name") MultipartFile file)の代わりに@RequestParam("file") MultipartFile fileが必要です。あなたは、内蔵のapache-コモンズのファイルのアップロードおよびネイティブサーブレットのサポート3.

+0

おかげリゾルバmultipartviewを設定している必要があります。もちろん、

、私はその部分を追加しました。私は要求を適切に送信していないようです。それをどうするかわからない。 – mw02

+0

私はmultipart/form-dataを消費しているのを見て、@RequestParam( "file")MultipartFileファイルの@RequestParam( "name")MultipartFileファイルを変更します。実際にあなたがあなたのエラーを参照している場合は、Reqiured MultipartFileパラメタ名と言う。名前のついた名前をつけた名前の部分と名前の部分を渡すべきです。 –

+1

これを変更しました。ありがとう。また、郵便配達員からContent-Typeを削除してから、戻り値の型をコントローラに追加する必要がありました。今それは動作します。 – mw02

関連する問題