gpt4 book ai didi

java - 请求被拒绝,因为在 spring boot 中没有找到 multipart 边界

转载 作者:IT老高 更新时间:2023-10-28 20:44:43 26 4
gpt4 key购买 nike

因为我正在尝试使用带有 postman Chrome 插件的 Spring Boot 和 Web 服务。

在 postman content-type="multipart/form-data" 中,我收到以下异常。

HTTP Status 500 - Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request;
nested exception is java.io.IOException:
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

在 Controller 中我指定了以下代码

@ResponseBody
@RequestMapping(value = "/file", headers = "Content-Type= multipart/form-data", method = RequestMethod.POST)

public String upload(@RequestParam("name") String name,
@RequestParam(value = "file", required = true) MultipartFile file)
//@RequestParam ()CommonsMultipartFile[] fileUpload
{
// @RequestMapping(value="/newDocument", , method = RequestMethod.POST)
if (!file.isEmpty()) {
try {
byte[] fileContent = file.getBytes();
fileSystemHandler.create(123, fileContent, name);
return "You successfully uploaded " + name + "!";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}

这里我指定文件处理程序代码

public String create(int jonId, byte[] fileContent, String name) {
String status = "Created file...";
try {
String path = env.getProperty("file.uploadPath") + name;
File newFile = new File(path);
newFile.createNewFile();
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(newFile));
stream.write(fileContent);
stream.close();
} catch (IOException ex) {
status = "Failed to create file...";
Logger.getLogger(FileSystemHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return status;
}

最佳答案

问题是你自己设置Content-Type,留空。谷歌浏览器会为你做这件事。多部分 Content-Type 需要知道文件边界,当您删除 Content-Type 时,Postman 会自动为您完成。

关于java - 请求被拒绝,因为在 spring boot 中没有找到 multipart 边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36005436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com