java REST API 中的多部分表单请求给出 FileNotFoundexception
Multipart form request in java REST API gives FileNotFound exception
@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 更改为多部分
@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 更改为多部分