Skip to content
Snippets Groups Projects
Commit cdeee853 authored by Matthias Severin's avatar Matthias Severin
Browse files

ajout du telechargement des fichiers et creation de la fonction pour le...

ajout du telechargement des fichiers et creation de la fonction pour le telechargement des fichiers (vide pour l'instant)
parent 65598405
No related branches found
No related tags found
No related merge requests found
......@@ -43,6 +43,10 @@
<artifactId>commons-net</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
</dependencies>
<build>
......
package fil.sr2.flopbox;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
......@@ -14,6 +19,11 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
......@@ -38,7 +48,7 @@ public class FtpResource {
@Path("/{name}")
public void updateFTP(@PathParam("name") String name, @FormParam("adress") String adress) {
System.out.println("@PUT");
ftpMap.put(name, adress);
ftpMap.replace(name, adress);
}
@DELETE
......@@ -59,11 +69,65 @@ public class FtpResource {
@GET
@Path("/{name}/list/{path: .*}")
@Produces(MediaType.TEXT_PLAIN)
public String listFTP(@PathParam("name") String name, @PathParam("path") String path, @HeaderParam("username") String username, @HeaderParam("password") String password) {
public Response listFTP(@PathParam("name") String name, @PathParam("path") String path, @HeaderParam("username") String username, @HeaderParam("password") String password) {
ResponseBuilder res;
try {
connect(ftpMap.get(name), username, password);
String res = list(path);
res = Response.status(Response.Status.OK).entity(list(path));
disconnect();
return res;
} catch (FTPException e) {
res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Failled to received the list of the directory\n");
}
return res.build();
}
@GET
@Path("/{name}/file/{path: .*}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFileFTP(@PathParam("name") String name, @PathParam("path") String path, @HeaderParam("username") String username, @HeaderParam("password") String password) {
ResponseBuilder res;
try {
connect(ftpMap.get(name), username, password);
res = Response.status(Response.Status.OK).entity(getFile(path));
disconnect();
} catch (FTPException e) {
res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Failled to received the list of the directory\n");
}
return res.build();
}
@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFileFTP(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) {
System.out.println("here");
System.out.println(fileDetail.getFileName());
}
private File getFile(String path) {
File file = new File("/home/severin/Documents/tmp/" + path);
System.out.println(file);
if (!file.exists()) {
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
ftpClient.enterLocalPassiveMode();
try {
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//ftpClient.changeWorkingDirectory(path);
OutputStream os = new FileOutputStream(file);
ftpClient.retrieveFile(path, os);
return file;
} catch (IOException e) {file.getParentFile().mkdir();
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private String list(String path) {
......
......@@ -2,6 +2,7 @@ package fil.sr2.flopbox;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
......@@ -22,7 +23,8 @@ public class Main {
public static HttpServer startServer() {
// create a resource config that scans for JAX-RS resources and providers
// in fil.sr2.flopbox package
final ResourceConfig rc = new ResourceConfig().packages("fil.sr2.flopbox");
final ResourceConfig rc = new ResourceConfig().packages("fil.sr2.flopbox").register(MultiPartFeature.class);
// create and start a new instance of grizzly http server
// exposing the Jersey application at BASE_URI
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment