使用springBoot和ReactJs通过文件上传添加额外数据
希望你们都很好,我在spring boot中上传一个包含一些额外数据的文件时遇到了一个严重问题,这是我的上传文件java代码使用springBoot和ReactJs通过文件上传添加额外数据,reactjs,spring-boot,visual-studio-code,axios,Reactjs,Spring Boot,Visual Studio Code,Axios,希望你们都很好,我在spring boot中上传一个包含一些额外数据的文件时遇到了一个严重问题,这是我的上传文件java代码 // Add new AssignmentList List Record @PostMapping("/uploadfile") public ResponseEntity<String> uploadData(@RequestParam("document") MultipartFile file)
// Add new AssignmentList List Record
@PostMapping("/uploadfile")
public ResponseEntity<String> uploadData(@RequestParam("document") MultipartFile file) throws Exception {
try {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
AssignmentList assignmentList = new AssignmentList();
assignmentList.setFileName(fileName);
assignmentList.setData(file.getBytes());
assignmentList.setFileSize(file.getSize());
assignmentList.setFileType(file.getContentType());
assignmentList.setUploadTime(new Date());
// assignmentList.setSemester(assignmentList.getSemester());
assignmentListDao.save(assignmentList);
return new ResponseEntity("File Uploaded Successfully", HttpStatus.OK);
}catch (Exception err){
return new ResponseEntity<>("File not uploaded", HttpStatus.BAD_REQUEST);
}
}
分配列表实体如下所示:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name="assignment_list")
public class AssignmentList implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int fileId;
@Column(name="file_name", length=512, nullable = false, unique = true)
private String fileName;
@Column(name="file_size")
private Long fileSize;
@Column(name="file_type")
private String fileType;
@Column(name="upload_time")
private Date uploadTime;
@Lob
private byte[] data;
@OneToOne(cascade = {CascadeType.DETACH}, fetch = FetchType.EAGER )
private Semester semester;
// constructor
public AssignmentList(int fileId, String fileName, Long fileSize, Date uploadTime) {
this.fileId = fileId;
this.fileName = fileName;
this.fileSize = fileSize;
this.uploadTime = uploadTime;
}
}
当我尝试使用Reactjs上载文件时,spring boot中有以下实体,它将上载该文件,但没有学期id
,react axios post请求为
const uploadFile = async () => {
const formData = new FormData();
formData.append("document", file);
formData.append("semester", sem);
await axios
.post("http://localhost:8080/assignmentlist/uploadfile", formData, {
headers: {
"Content-Type": "multipart/form-data",
Accept: "application/json",
type: "formData",
},
})
.then(
function (response) {
//handle success
console.log(response);
},
function (error) {
// handle error
alert(error.message);
}
);
};
其中sem
是从其中一个选择框获得的值,即
onChange(e=>setSem(e.target.value))
我希望我澄清我的疑虑
提前感谢:)将控制器的方法签名更改为:
public ResponseEntity<String> uploadData(@RequestParam("document") MultipartFile file, @RequestParam("semester") Semester semester) throws Exception
如果这不起作用,只需将学期
作为int
阅读,然后自己从中构建学期
public ResponseEntity<String> uploadData(@RequestParam("document") MultipartFile file, @RequestParam("semester") Semester semester) throws Exception
assignmentList.setSemester(semester);