From 69e07a47c89179304c82209fd7b767360343b7db Mon Sep 17 00:00:00 2001 From: Arimihanta Date: Thu, 25 Nov 2021 17:03:48 +0300 Subject: [PATCH] endpoint upload file --- .../fr/lirmm/aren/model/aaf/UploadedFile.java | 13 ++++++ .../aren/ws/rest/DocumentRESTFacade.java | 46 +++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/main/java/fr/lirmm/aren/model/aaf/UploadedFile.java diff --git a/src/main/java/fr/lirmm/aren/model/aaf/UploadedFile.java b/src/main/java/fr/lirmm/aren/model/aaf/UploadedFile.java new file mode 100644 index 0000000..41a979d --- /dev/null +++ b/src/main/java/fr/lirmm/aren/model/aaf/UploadedFile.java @@ -0,0 +1,13 @@ +package fr.lirmm.aren.model.aaf; + +public class UploadedFile { + private String name ; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java b/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java index 0a04f3b..01e059d 100644 --- a/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java +++ b/src/main/java/fr/lirmm/aren/ws/rest/DocumentRESTFacade.java @@ -1,14 +1,20 @@ package fr.lirmm.aren.ws.rest; +import javax.annotation.security.PermitAll; import javax.annotation.security.RolesAllowed; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import fr.lirmm.aren.model.aaf.UploadedFile; import fr.lirmm.aren.service.DocumentService; import fr.lirmm.aren.model.Document; +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; +import org.glassfish.jersey.media.multipart.FormDataParam; + +import java.io.*; import java.util.Set; /** @@ -88,4 +94,38 @@ public class DocumentRESTFacade extends AbstractRESTFacade { return this.create(document); } + + @POST + @Path("/map") + @PermitAll + @Consumes({MediaType.MULTIPART_FORM_DATA}) + public Response uploadPdfFile(@FormDataParam("file") InputStream fileInputStream, + @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception + { + File UPLOAD_PATH = new File("/tmp/img/"); + if (! UPLOAD_PATH.exists()){ + UPLOAD_PATH.mkdir(); + } + UploadedFile uploadedFile=new UploadedFile() ; + try + { + int read = 0; + byte[] bytes = new byte[1024]; + String filename=UPLOAD_PATH +"carto"+System.currentTimeMillis()+ fileMetaData.getFileName().substring(fileMetaData.getFileName().lastIndexOf(".")) ; + File file=new File(filename) ; + OutputStream out = new FileOutputStream(file); + while ((read = fileInputStream.read(bytes)) != -1) + { + out.write(bytes, 0, read); + } + out.flush(); + out.close(); + + uploadedFile.setName(file.getAbsolutePath()); + } catch (IOException e) + { + throw new WebApplicationException("Error while uploading file. Please try again !!"); + } + return Response.status(200).entity(uploadedFile).build(); + } }