我正在尝试向服务器发送多部分请求,但出现以下异常
org.springframework.web.multipart.MultipartException: Current request is not a multipart request
at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:188)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:104)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:128)
基于教程和stackoverflow问题,我开发了以下代码
控制器
@RequestMapping(value = "/upload-file", method = RequestMethod.POST)
public ResponseEntity<Void> getUploadFile(@RequestParam("file") MultipartFile file) {
LOGGER.debug(String.valueOf(file));}
多部分配置
@Configuration
@EnableWebMvc
public class MultipartConfiguration {
private static final String DEFAULT_ENCODING = "UTF-8";
private static final long MAX_UPLOAD_FILE_SIZE = 100000000;
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver=new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
resolver.setMaxUploadSize(MAX_UPLOAD_FILE_SIZE);
resolver.setDefaultEncoding(DEFAULT_ENCODING);
return resolver;
}
}
html
<input type="file" style="display:none" #fileupload name="image" (change)="fileProgress($event)" />
零件
export class FileUploadComponent {
fileData: any = null;
uploadedFilePath: string = null;
formData = new FormData();
reader = new FileReader();
@Output() fiLoaded: EventEmitter<File> = new EventEmitter();
constructor(private pointeService: PointeService) { }
fileProgress(fileInput: any) {
this.fileData = fileInput.target.files[0];
this.reader.readAsDataURL(this.fileData);
this.reader.onloadend = (_event) => {
this.formData.append('file', this.fileData);
this.onFileLoaded(this.formData);
};
}
onFileLoaded(formData: any) {
this.pointeService.postModifiedFileExel(formData)
.subscribe(events => {
if (events.type === HttpEventType.UploadProgress) {
} else if (events.type === HttpEventType.Response) {
// this.fileUploadProgress = 0;
console.log(events.body);
alert('SUCCESS !!');
}
});
}
}
服务
postModifiedFileExel(formData: any) {
return this.http.post(`${AppUtils.REST_API_SERVER}/espacegroupe/upload-file`, formData , {
reportProgress: true,
observe: 'events'
});
}
我还注意到我的帖子只包含文件的名称,它不包含字节格式的数据
请问有什么建议吗?