Guest User

Untitled

a guest
May 23rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. @POST
  2. @Path("/upload")
  3. @Consumes(ExtendedMediaType.MULTIPART_FORM_DATA)
  4. @Produces(ExtendedMediaType.APPLICATION_JSON_UTF8)
  5. @ApiOperation(
  6. value = "Upload a selected image",
  7. notes = "Return with the ID of the uploaded image.",
  8. response = ByteArrayInputStream.class)
  9. @ApiResponses(value = {
  10. @ApiResponse(code = NotAcceptedMediaTypeError.HTTP_RESPONSE_CODE, message = NotAcceptedMediaTypeError.SWAGGER_API_RESPONSE_MESSAGE, response = ErrorInfo.class)})
  11. @Transactional
  12. @Override
  13. public Response uploadImage(@RequestParam("file") final InputStream file) throws IOException, NotAcceptedMediaTypeError, ParseException {
  14.  
  15. try (LimitedSizeInputStream limitedSizeInputStream = new LimitedSizeInputStream(file, maxFileSize)) {
  16. byte[] content = IOUtils.toByteArray(limitedSizeInputStream);
  17.  
  18. // check the type of the uploaded file
  19. org.apache.tika.mime.MediaType uploadedMediaType = ContentTypeDetector.getMediaType(content);
  20. boolean validMediaType = acceptedExtensions.contains(uploadedMediaType.toString());
  21.  
  22. if (validMediaType) {
  23. Image image = Image.builder().content(content).build();
  24. imageMapper.saveImage(image);
  25.  
  26. FormDataContentDisposition fileDetail = new FormDataContentDisposition(CustomHttpHeader.PROCESS_ID);
  27.  
  28. ImageMetadata imageMetadata = buildImageMetadata(fileDetail, image);
  29. imageMapper.saveMetadata(imageMetadata);
  30.  
  31. return Response.ok().entity(imageMetadata.getUuid()).build();
  32. } else {
  33. throw new NotAcceptedMediaTypeError(uploadedMediaType.toString(), acceptedExtensions);
  34. }
  35. }
  36. }
  37.  
  38. <!DOCTYPE html>
  39. <html lang="en">
  40. <head>
  41. <meta charset="UTF-8">
  42. <title>Image Service</title>
  43. <script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>
  44. </head>
  45. <body>
  46. <h1>RDBMS Image REST Service</h1>
  47. <h3>File upload demo</h3>
  48. <ul>
  49. <li>
  50. <form id="upload-file-form" method="post" enctype="multipart/form-data">
  51. Upload your file: <input type="file" name="file" size="45" />
  52. <br>
  53. <input id="submit-button" type="submit" value="Upload" />
  54. </form>
  55. </li>
  56. <li><p>Result: <br><span id="result"></span></p></li>
  57. </ul>
  58.  
  59. <h3>Show Image</h3>
  60. <ui>
  61. <li>original:<img id="image-o" src="#" alt="original image" /></li>
  62. <li>small: <img id="image-s" src="#" alt="small image" /></li>
  63. <li>medium: <img id="image-m" src="#" alt="medium image" /></li>
  64. <li>large: <img id="image-l" src="#" alt="large image" /></li>
  65. <li>extra large: <img id="image-xl" src="#" alt="extra large image" /></li>
  66. </ui>
  67.  
  68. <script type="text/javascript">
  69. $(document).ready(function () {
  70. $('#submit-button').click(function (event) {
  71. //stop submit the form, we will post it manually
  72. event.preventDefault();
  73.  
  74. // get form
  75. var form = $('#upload-form')[0];
  76.  
  77. // create an FormData object
  78. var data = new FormData(form);
  79.  
  80. // disabled the submit button
  81. $("#submit-button").prop("disabled", true);
  82.  
  83. // post data
  84. $.ajax({
  85. type: "POST",
  86. enctype: 'multipart/form-data',
  87. url: "api/upload",
  88. data: data,
  89. processData: false,
  90. contentType: false,
  91. cache: false,
  92. timeout: 600000,
  93.  
  94. success: function (data) {
  95. // shows server's response
  96. $("#result").text(data);
  97. console.log("SUCCESS: ", data);
  98.  
  99. enableSubmitButton();
  100. updateImages(data);
  101. },
  102.  
  103. error: function (e) {
  104. // shows server's response
  105. $("#result").text(e.responseText);
  106. console.log("ERROR: ", e);
  107.  
  108. enableSubmitButton();
  109. updateImages(e.responseText);
  110. }
  111. });
  112. });
  113. });
  114.  
  115. function enableSubmitButton() {
  116. $("#submit-button").prop("disabled", false);
  117. }
  118.  
  119. function updateImages(data) {
  120. var url = 'http://localhost:9001/image/api/' + data;
  121. $('#image-s').attr('src',url + '?size=s');
  122. $('#image-m').attr('src',url + '?size=m');
  123. $('#image-l').attr('src',url + '?size=l');
  124. $('#image-xl').attr('src',url + '?size=xl');
  125. $('#image-o').attr('src',url + '?size=o');
  126. }
  127. </script>
  128. </body>
  129. </html>
Add Comment
Please, Sign In to add comment