PostmanでファイルとJSONデータをアップロードするにはどうすればいいですか? 質問する

PostmanでファイルとJSONデータをアップロードするにはどうすればいいですか? 質問する

私は Spring MVC を使用しており、方法は次のとおりです:

/**
* Upload single file using Spring Controller.
*/
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public @ResponseBody ResponseEntity<GenericResponseVO<? extends IServiceVO>> uploadFileHandler(
            @RequestParam("name") String name,
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request,
            HttpServletResponse response) {

    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();

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

            return null;
        } catch (Exception e) {
            return null;
        }
    }
}


Postman とファイルでセッション ID を渡す必要があります。どうすればいいでしょうか?

ベストアンサー1

Postmanの場合:

  • HTTP メソッド タイプをPOSTに設定します。
  • 次に、Body -> form-data -> パラメータ名(コードに応じたファイル)を入力します。
  • キー フィールドの右側にマウスを移動すると、テキスト/ファイルを選択するためのドロップダウン メニューが表示されます。ファイルを選択すると、値フィールドに「ファイルを選択」ボタンが表示されます。

残りの「テキスト」ベースのパラメータについては、Postman で通常行うように投稿できます。パラメータ名を入力し、右側のドロップダウン メニューから「テキスト」を選択して任意の値を入力し、送信ボタンを押します。コントローラ メソッドが呼び出されます。

おすすめ記事