我在使用angular 8和spring boot作为一个简单的应用程序时遇到了一个问题,这个应用程序可以从ui上传文件和数据。当我尝试使用postman请求时,效果很好,但不起作用。错误为org.springframework.web.multipart.multipartexception:解析multipart servlet请求失败;嵌套异常是java.io.ioexception。当我试图使用postman发送put请求时,没有显示此错误
句柄输入函数
handleFileInput(files: FileList) {
let me = this;
let file = files[0];
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
console.log(reader.result);
me.ImageBaseData=reader.result;
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
input upload file
<div>
<input type="file" formControlName="fichier" (change)="handleFileInput($event.target.files)">
</div>
put请求函数
adddocuments(){if(this.form.valid){
let ImageBaseData= this.ImageBaseData.toString();
const document = {
type: this.form.get('type').value,
dateEdition: this.form.get('dateedition').value,
commentaire: this.form.get('commentaire').value,
libelle: this.form.get('libelle').value,
rang: this.form.get('rang').value,
file: ImageBaseData
};
this.uploadService.addDocument(this.data,document)
.subscribe(res => {
console.log(res);
this.notificationService.success(':: Document ajouté avec succés');
this.dialogRef.close();
}),
(error => {
console.log(error)
})
}
添加文档服务:
addDocument(id,data):Observable<any>{
return this.http.put(`${addDocs}/${id}`,data); }
Spring Boot
public ResponseEntity<Patient> AddDocsFile (@PathVariable("id") String id,@RequestParam("fichier") MultipartFile fichier, @ModelAttribute PDocument pDocument)throws IOException {
Optional<Patient> patientData = patientRepository.findById(id);
Patient P = patientData.get();
PDocument p = new PDocument(pDocument.getType(),pDocument.getDateEdition(),pDocument.getCommentaire(),pDocument.getLibelle(),pDocument.getRang(),pDocument.getFichier());
p.setFichier(new Binary(BsonBinarySubType.BINARY, fichier.getBytes()));
// filesRepository.save(photo);
pDocumentRepository.save(p);
P.getPDocuments().add(p);
return new ResponseEntity<>(patientRepository.save(P), HttpStatus.OK);
}
当我试图提交put请求时,数据如下所示
暂无答案!
目前还没有任何答案,快来回答吧!