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

问题描述 投票:0回答:1
@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();
    }
}   

一切都很好。但是当我试图保存文件时,它给我FileNotFoundException。 我看到了许多与此相关的问题,但我没有在任何网站上看到任何答案。请帮助我。

java rest multipart
1个回答
0
投票

改变FormDataContentDisposition为Multipart。

© www.soinside.com 2019 - 2024. All rights reserved.