Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Drop zone Rest
- @OPTIONS
- @Path("/upload")
- @javax.ws.rs.Produces(MediaType.APPLICATION_JSON)
- public Response uploadFile() {
- return Response.status(200).entity("success").header("Access-Control-Allow-Origin", "*")
- .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS")
- .header("Access-Control-Allow-Headers", "accept, Cache-Control, content-type, x-requested-with")
- .allow("OPTIONS").build();
- }
- @POST
- @Path("/upload")
- @Consumes(MediaType.MULTIPART_FORM_DATA)
- public Response uploadFile(
- @FormDataParam("file") InputStream uploadedInputStream,
- @FormDataParam("file") FormDataContentDisposition fileDetail) {
- String uploadedFileLocation = "/tmp/feroz/" + fileDetail.getFileName();
- // save it
- writeToFile(uploadedInputStream, uploadedFileLocation);
- String output = "File uploaded to : " + uploadedFileLocation;
- System.out.println(output);
- JsonObject jsonObject = new JsonObject();
- jsonObject.addProperty("success", true);
- jsonObject.addProperty("status", "success");
- jsonObject.addProperty("path", uploadedFileLocation);
- return Response.status(200).entity(jsonObject.toString()).header("Access-Control-Allow-Origin", "*")
- .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
- .allow("OPTIONS").build();
- }
- // save uploaded file to new location
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement