当前位置: 首页 > 知识库问答 >
问题:

无法在spring boot中上载文件

乐正浩宕
2023-03-14

在过去的3天里,我一直在努力解决这个问题,当我尝试在spring boot项目中上载文件时,我不断遇到以下异常<代码>组织。springframework。网状物多部分。支持MissingServletRequestPartException:所需的请求部分“文件”不存在

我不确定它是否有所不同,但我正在将我的应用程序部署为对weblogic的战争,这是我的控制

@PostMapping
public AttachmentDto createAttachment(@RequestParam(value = "file") MultipartFile file) {
    logger.info("createAttachment - {}", file.getOriginalFilename());
    AttachmentDto attachmentDto = null;
    try {
        attachmentDto = attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return attachmentDto;
}

我可以在spring boot执行器中看到多部分bean

chrome中显示的有效载荷

共有3个答案

楚方伟
2023-03-14

您使用多部分发送文件,因此无需进行太多配置即可获得所需的结果。我有同样的要求,我的代码运行良好:

@RestController
@RequestMapping("/api/v2")
public class DocumentController {

    private static String bucketName = "pharmerz-chat";
    //   private static String keyName        = "Pharmerz"+ UUID.randomUUID();

    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA)
    public URL uploadFileHandler(@RequestParam("name") String name,
                                 @RequestParam("file") MultipartFile file) throws IOException {

/******* Printing all the possible parameter from @RequestParam *************/

        System.out.println("*****************************");

        System.out.println("file.getOriginalFilename() " + file.getOriginalFilename());
        System.out.println("file.getContentType()" + file.getContentType());
        System.out.println("file.getInputStream() " + file.getInputStream());
        System.out.println("file.toString() " + file.toString());
        System.out.println("file.getSize() " + file.getSize());
        System.out.println("name " + name);
        System.out.println("file.getBytes() " + file.getBytes());
        System.out.println("file.hashCode() " + file.hashCode());
        System.out.println("file.getClass() " + file.getClass());
        System.out.println("file.isEmpty() " + file.isEmpty());

/**
        BUSINESS LOGIC
Write code to upload file where you want
*****/
return "File uploaded";
}
濮阳振
2023-03-14

您可以尝试使用@Request estPart,因为它使用HttpMessageConverter,它考虑了请求部分的“Content-Type”标头。

请注意,@Request estParam注释也可用于将“multipart/form-data”请求的部分与支持相同方法参数类型的方法参数相关联。主要区别在于,当方法参数不是String时,@Request estParam依赖于通过注册的转换器或属性编辑器进行类型转换,而@Request estPart依赖于HttpMessageConverters,同时考虑到请求部分的“Content-Type”标头。@Request estParam可能与名称值表单字段一起使用,而@Request estPart可能与包含更复杂内容的部分一起使用(例如JSON、XML)。

Spring文档

代码

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public AttachmentDto createAttachment(@RequestPart("file") MultipartFile file) {
    logger.info("Attachment - {}", file.getOriginalFilename());

    try {
        return attachmentService.createAttachment(new AttachmentDto(file, 1088708753L));
    } catch (final IOException e) {
        logger.e("Error creating attachment", e);
    }

    return null;
}
湛光华
2023-03-14

@RequestParm“file”需要Name属性

<input type="file" class="file" name="file"/>
 类似资料:
  • 我正在使用CKEditor插件将图像上传到我的OpenShift应用程序,但该应用程序不会上传文件。它在本地运行良好,但我无法确定是什么导致了这个问题。它似乎找不到目录。 == {err:{[Error: ENOENT, open'/var/lib/openShift/57cdb2770c1e660d0b000003/app-root/runtime/repo/路由来/.../Public/上传/

  • 我在Chrome67上使用WebDriver 3.12.0和ChromeDriver 2.40。我初始化驱动程序变量如下: 当我在headless模式下执行它时,我得到这一行的TimeoutException。当我在正常模式下执行它时,它工作得很好。 错误消息: org.openqa.selenium.TimeoutException:预期条件失败:等待位于以下位置的元素的存在:By.xpath:

  • 我正在尝试上载文件,但无法工作: 有用信息:运行IIS Express(使用PHP5.3)-Windows 7 Professional 32位 代码: move_uploaded_file($_FILES[图像][名称],/图像/。$_FILES["Imagem"]["name"])或die("Error:".print_r($_FILES)); 它打印:数组([imagem]= 我确信路径是正

  • 我是新春尝试简单的webapp。 使用springboot 2.1.0application.properties如下文件。我在src/main/webapp/WEB-INF/jsp中有jsp文件 我的控制器是。 当我将jsp文件放在/main/resources/META-INF/resources/WEB-INF/jsp中时,它可以正常工作,否则会出现以下错误 pom还具有tomcat嵌入ja

  • 当我试图在heroku上部署springboot应用程序时,出现了以下错误: 无法执行目标org.apache.Maven.plugins:maven-compiler-plugin:3.8.1:在项目后端编译(default-compile):错误编译:无效的目标版本:11->[Help 1]Remote:[error]Remote:[error]要查看错误的全部堆栈跟踪,请使用-e开关重新运行

  • 我正在尝试上传一个图像。当我从本地主机上进行时,它工作得很好,但当我发布它时,它从服务器上抛出一个错误: 当我使用此代码时: 错误为: System.io.DirectoryNotFoundException:找不到路径“d:\inetpub\vhosts\xx.com\httpdocs\images\sections\developer\clientlogo\demo.png”的一部分。在sys