endpoint upload file

This commit is contained in:
Arimihanta 2021-11-25 17:03:48 +03:00
parent 80a50e94aa
commit 69e07a47c8
2 changed files with 56 additions and 3 deletions

View file

@ -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;
}
}

View file

@ -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<Document> {
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();
}
}