Created
June 14, 2019 11:56
-
-
Save Feroz-Istar/0e6dbfc1cc45368acc00b10f8c354208 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment