File upload along with other object in Jersey restful web service

后端 未结 6 1778
忘掉有多难
忘掉有多难 2020-11-22 13:34

I want to create an employee information in the system by uploading an image along with employee data. I am able to do it with different rest calls using jersey. But I want

6条回答
  •  没有蜡笔的小新
    2020-11-22 13:59

    You can access the Image File and data from a form using MULTIPART FORM DATA By using the below code.

    @POST
    @Path("/UpdateProfile")
    @Consumes(value={MediaType.APPLICATION_JSON,MediaType.MULTIPART_FORM_DATA})
    @Produces(value={MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public Response updateProfile(
        @FormDataParam("file") InputStream fileInputStream,
        @FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
        @FormDataParam("ProfileInfo") String ProfileInfo,
        @FormDataParam("registrationId") String registrationId) {
    
        String filePath= "/filepath/"+contentDispositionHeader.getFileName();
    
        OutputStream outputStream = null;
        try {
            int read = 0;
            byte[] bytes = new byte[1024];
            outputStream = new FileOutputStream(new File(filePath));
    
            while ((read = fileInputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
    
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) { 
                try {
                    outputStream.close();
                } catch(Exception ex) {}
            }
        }
    }
    

提交回复
热议问题