该请求被拒绝,因为在springboot中找不到多部分边界。

15 浏览
0 Comments

该请求被拒绝,因为在springboot中找不到多部分边界。

我正在尝试使用Spring Boot和Postman Chrome插件进行Webservices。在Postman中,我设置了content-type=\"multipart/form-data\",但是出现了以下异常:\nHTTP状态500 - 请求处理失败;嵌套异常是org.springframework.web.multipart.MultipartException:无法解析多部分servlet请求;嵌套异常是java.io.IOException:org.apache.tomcat.util.http.fileupload.FileUploadException:由于没有找到多部分边界,请求被拒绝\n在Controller中,我指定了以下代码:\n

@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 "上传成功:" + name + "!";
        } catch (Exception e) {
            return "上传失败:" + name + " => " + e.getMessage();
        }
    } else {
        return "上传失败:" + name + ",因为文件为空。";
    }
}

\n这里是文件处理器的代码:\n

public String create(int jonId, byte[] fileContent, String name) {
    String status = "创建文件...";
    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 = "文件创建失败...";
        Logger.getLogger(FileSystemHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return status;
}

0
0 Comments

问题的原因是在Spring Boot中没有找到multipart boundary。解决方法是在Postman中设置正确的请求头和请求体。

在Postman中,首先需要设置正确的请求头。打开Postman并选择POST请求。在请求头中,设置Content-Type为multipart/form-data,并确保在请求头中设置了"boundary"参数,该参数用于分隔multipart请求中的各个部分。

接下来,设置正确的请求体。在请求体中,选择"form-data"选项,并提供正确的键值对。对于文件上传,需要提供一个键来标识文件,并选择文件作为值。

通过以上步骤,可以解决在Spring Boot中出现"no multipart boundary was found"的问题。

具体的代码如下所示:

@PostMapping("/upload")
public ResponseEntity handleFileUpload(@RequestParam("file") MultipartFile file) {
    // 处理文件上传逻辑
    return ResponseEntity.ok("File uploaded successfully");
}

在Postman中,设置请求头如下:

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

并在请求体中选择"form-data"选项,以及提供正确的键值对。

通过以上设置,可以成功上传文件到Spring Boot后端应用程序。

0
0 Comments

这是由于在Spring Boot中找不到multipart边界而导致请求被拒绝的问题。解决方法是在请求中设置正确的multipart边界。

在Postman中,取消选中"Content-Type"选项,并让Postman根据运行时输入自动检测内容类型。

以下是解决该问题的步骤:

1. 打开Postman应用程序。

2. 在Postman中创建一个新的请求。

3. 在请求的"Body"选项卡中选择"form-data"。

4. 在"Key"字段中输入键的名称。

5. 在"Value"字段中输入相应键的值。

6. 确保"Content-Type"字段未选中。

7. 点击"Send"按钮发送请求。

这样,Postman将根据您在运行时输入的内容自动检测内容类型,并在请求中设置正确的multipart边界,从而解决了请求被拒绝的问题。

希望这篇文章能帮助您解决问题,如果您还有任何疑问,请随时提问。

0
0 Comments

问题出现的原因是由于没有在请求中找到multipart边界导致的。解决方法是将Content-Type设置为空,让Google Chrome自动处理,或者在Postman中移除Content-Type,让Postman自动处理。此外,还有一个解决方法是在请求中添加边界参数。另外,某些情况下在移除Content-Type后出现了"org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain' not supported"的错误,这可能是由于服务器不支持text/plain类型导致的。还有人表示感谢,解决了他们的问题。总之,这个问题的解决方法是通过设置正确的Content-Type或让工具自动处理来解决。

0