请问谁能帮我解决这个问题
org.apache.tomcat.util.http.fileupload.fileuploadexception:请求被拒绝,因为找不到多部分边界
文件控制器.java
package com.example.fileUploadApi.controller;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@RestController
@RequestMapping(path = "/fileUpload")
@CrossOrigin(origins = "http://localhost:4200")
@Produces(MediaType.APPLICATION_JSON)
public class FileController
{
@POST
@Path("/upload")
public void uploadData(MultipartBody file) throws Exception {
System.out.println(file);
}
}
upload-file.component.html文件
<div>
<input type="file" change="uploadFile($event)" />
</div>
上传文件组件.ts
uploadFile(event) {
file = event.target.files[0]
const formData = new FormData();
const dali = {
a: 'dali'
};
formData.append('file', file);
formData.append('model', JSON.stringify(dali));
this.fileUploadService.upload(formData).subscribe(
rsp => {console.log(rsp.type);}
}
2条答案
按热度按时间vktxenjb1#
在您的spring boot项目中使用此代码这将100%起作用
tjjdgumg2#
在服务中将formdata作为'params'传递给您,并使用@requestparams在api中获取它。我在post调用中没有看到@requestparams。