Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. Drop zone Rest
  2. @OPTIONS
  3. @Path("/upload")
  4. @javax.ws.rs.Produces(MediaType.APPLICATION_JSON)
  5. public Response uploadFile() {
  6. return Response.status(200).entity("success").header("Access-Control-Allow-Origin", "*")
  7. .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS")
  8. .header("Access-Control-Allow-Headers", "accept, Cache-Control, content-type, x-requested-with")
  9. .allow("OPTIONS").build();
  10. }
  11.  
  12. @POST
  13. @Path("/upload")
  14. @Consumes(MediaType.MULTIPART_FORM_DATA)
  15. public Response uploadFile(
  16. @FormDataParam("file") InputStream uploadedInputStream,
  17. @FormDataParam("file") FormDataContentDisposition fileDetail) {
  18.  
  19. String uploadedFileLocation = "/tmp/feroz/" + fileDetail.getFileName();
  20.  
  21. // save it
  22. writeToFile(uploadedInputStream, uploadedFileLocation);
  23.  
  24. String output = "File uploaded to : " + uploadedFileLocation;
  25. System.out.println(output);
  26. JsonObject jsonObject = new JsonObject();
  27.  
  28. jsonObject.addProperty("success", true);
  29. jsonObject.addProperty("status", "success");
  30. jsonObject.addProperty("path", uploadedFileLocation);
  31. return Response.status(200).entity(jsonObject.toString()).header("Access-Control-Allow-Origin", "*")
  32. .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
  33. .allow("OPTIONS").build();
  34.  
  35. }
  36.  
  37. // save uploaded file to new location
  38. private void writeToFile(InputStream uploadedInputStream,
  39. String uploadedFileLocation) {
  40.  
  41. try {
  42. OutputStream out = new FileOutputStream(new File(
  43. uploadedFileLocation));
  44. int read = 0;
  45. byte[] bytes = new byte[1024];
  46.  
  47. out = new FileOutputStream(new File(uploadedFileLocation));
  48. while ((read = uploadedInputStream.read(bytes)) != -1) {
  49. out.write(bytes, 0, read);
  50. }
  51. out.flush();
  52. out.close();
  53. } catch (IOException e) {
  54.  
  55. e.printStackTrace();
  56. }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement