0
PostManを使用してマルチパートファイルをアップロードしようとしましたが、エラーが発生しました。ここでは、コードとスクリーンショットは以下のとおりです。郵便番号を使用してMultipartFileをアップロードしようとしています
@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.");
}
}
おかげリゾルバmultipartviewを設定している必要があります。もちろん、
、私はその部分を追加しました。私は要求を適切に送信していないようです。それをどうするかわからない。 – mw02
私はmultipart/form-dataを消費しているのを見て、@RequestParam( "file")MultipartFileファイルの@RequestParam( "name")MultipartFileファイルを変更します。実際にあなたがあなたのエラーを参照している場合は、Reqiured MultipartFileパラメタ名と言う。名前のついた名前をつけた名前の部分と名前の部分を渡すべきです。 –
これを変更しました。ありがとう。また、郵便配達員からContent-Typeを削除してから、戻り値の型をコントローラに追加する必要がありました。今それは動作します。 – mw02