File upload Can';t在spring boot中上载文件

File upload Can';t在spring boot中上载文件,file-upload,spring-boot,weblogic,File Upload,Spring Boot,Weblogic,在过去的3天里,我一直在努力解决这个问题,当我尝试在我的spring boot项目中上传一个文件时,我一直遇到以下异常。 org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“文件”不存在 我不确定这是否会有所不同,但我将我的应用程序部署为weblogic上的一场战争, 这是我的控制器 @PostMapping createAttachment(@RequestParam(valu

在过去的3天里,我一直在努力解决这个问题,当我尝试在我的spring boot项目中上传一个文件时,我一直遇到以下异常。
org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部分“文件”不存在

我不确定这是否会有所不同,但我将我的应用程序部署为weblogic上的一场战争, 这是我的控制器

@PostMapping
createAttachment(@RequestParam(value=“file”)多部分文件的公共附件{
info(“createAttachment-{}”,file.getOriginalFilename());
AttachmentDto AttachmentDto=null;
试一试{
attachmentDto=attachmentService.createAttachment(新的attachmentDto(文件,1088708753L));
}捕获(IOE异常){
e、 printStackTrace();
}
返回附件至;
}
我可以在spring boot启动器中看到多部分bean

铬合金的有效载荷


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

请注意,@RequestParam注释还可用于将“多部分/表单数据”请求的一部分与支持相同方法参数类型的方法参数相关联。主要区别在于,当方法参数不是字符串时,@RequestParam依赖于通过注册转换器或PropertyEditor进行的类型转换,而@RequestPart依赖于HttpMessageConverters,同时考虑到请求部分的“Content type”头@RequestParam可能用于名称值表单字段,而@RequestPart可能用于包含更复杂内容的部分(例如JSON、XML)

代码:

@PostMapping(consumes=MediaType.MULTIPART\u FORM\u DATA\u VALUE)
创建附件的公共附件(@RequestPart(“文件”)多部分文件){
logger.info(“附件-{}”,file.getOriginalFilename());
试一试{
返回attachmentService.createAttachment(新的AttachmentDto(文件1088708753L));
}捕获(最终IOE例外){
logger.e(“创建附件时出错”,e);
}
返回null;
}

Name属性是@RequestParm“file”所必需的


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

@RestController
@请求映射(“/api/v2”)
公共类文档控制器{
私有静态字符串bucketName=“pharmerz chat”;
//私有静态字符串keyName=“Pharmerz”+UUID.randomUUID();
@RequestMapping(value=“/upload”,method=RequestMethod.POST,consumes=MediaType.MULTIPART\u FORM\u DATA)
公共URL uploadFileHandler(@RequestParam(“name”)字符串名称,
@RequestParam(“文件”)多部分文件)引发IOException{
/*******从@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(“名称”+名称);
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());
/**
业务逻辑
编写代码将文件上载到您想要的位置
*****/
返回“上传文件”;
}

上述解决方案对我都不起作用,但当我深入挖掘时,我发现spring security是罪魁祸首。即使我发送了CSRF令牌,我也一再面临不受支持的问题。当我在google chrome中使用开发者工具进行检查并在中看到状态代码时,我知道我收到了禁止403网络选项卡。我在Spring Security configuration中将映射添加到ignoredCsrfMapping,然后它完全正常工作,没有任何其他缺陷。不知道为什么不允许我按安全性发布多部分数据。需要在application.properties文件中声明的一些强制设置如下:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
spring.http.multipart.enabled=true

恐怕运气不好,我以前试过,谢谢你的文档,好的阅读会把我正在使用的库翻出来('ng2-file-upload'))是混乱的一些它是如何发送其他东西,作为名称,尝试了这个代码从基本的html和它的工作,对此表示抱歉,谢谢,但这已经得到解决,这是一个问题与前端库我用来上传文件