/** {@inheritDoc} */ @POST @Path("/picture/{personId}") @Consumes("image/jpeg") public Response uploadImage(@PathParam("personId") String personId, InputStream inputStream) throws IOException { String destinationPath = PICTURE_BASE_PATH + personId + ".jpg"; try { int read; byte[] bytes = new byte[1024]; new File(destinationPath).delete(); FileOutputStream out = new FileOutputStream(destinationPath, false); while ((read = inputStream.read(bytes)) > 0) { out.write(bytes, 0, read); } inputStream.close(); out.flush(); out.close(); cardManager.updateImage(personId); } catch (IOException e) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e).build(); } return Response.ok().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(); } }