gpt4 book ai didi

java - java REST API 中的多部分表单请求给出 FileNotFound 异常

转载 作者:行者123 更新时间:2023-12-01 16:40:48 25 4
gpt4 key购买 nike

@POST
@Produces("application/json")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response signUp(
@FormDataParam("name") String name, @FormDataParam("mobile") String mobile,
@FormDataParam("password") String password, @FormDataParam("email") String email,
@FormDataParam("dob") String dob, @FormDataParam("address") String address,
@FormDataParam("otp") String otp, @FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail,
@FormDataParam("path") String path) {

Registration reg = new Registration();
reg.setName(name);
reg.setMobile(mobile);
reg.setPassword(password);
reg.setEmail(email);
reg.setDob(dob);
reg.setAddress(address);
reg.setEnteredDate(new Date());
reg.setOtp(otp);
//path=http://myip:8080/project_name
String uploadedFileLocation = path + fileDetail.getFileName();
System.out.println(uploadedFileLocation);
writeToFile(uploadedInputStream, uploadedFileLocation);
RegistrationServiceI regService = new RegistrationService();
String result = regService.saveRegistration(reg);
JSONObject jsonObj = new JSONObject();
jsonObj.put("result", result);

return Response.status(200).entity(jsonObj.toString()).build();
}

private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Everything working good. But when I am trying to save file, it gives me FileNotFoundException. I saw many questions related to this but I have not seen any answer in any website. Please help me.

最佳答案

将 FormDataContentDisposition 更改为 Multipart

关于java - java REST API 中的多部分表单请求给出 FileNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61866885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com