Jersey File Upload: Cannot save file into web project

607 views Asked by At

I'm facing on a problem with a file Upload in Jersey. I want to upload specific Files and save them temporarily on the server in my web project folder using @FormDataParam and @FormDataContentDisposition which works with following code:

My Rest Class:

@Path("file")

public class FileRest extends Application {

/**
 * Method handling HTTP POST requests. The received object will be a
 * uploaded file as "multipart/form-data" media type.
 *
 * @param http header, input stream, content disposition,
 * @return response code.
 * @throws FileNotFoundException 
 */
@POST
@Path("/upload")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @Context HttpHeaders headers,
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition contentDisposition
        )  {

    String fileLocation = "src/main/resources/media/files/" + contentDisposition.getFileName();  // FileNotFoundException: The system cannot find the path specified
    String fileLocation = System.getProperty("user.dir") + "/src/main/resources/media/files/" + contentDisposition.getFileName(); // does not work --> shows to Eclipse directory but outside of the rest class it shows to the current project folder  
    String fileLocation = System.getProperty("user.home") + "/" + contentDisposition.getFileName(); // works --> saves file in C:\\users\username\\file
    String fileLocation = "C:/Users/LocalAdmin/git/PROJECTNAME/src/main/resources/media/files/" + contentDisposition.getFileName(); // absolute path works
    String fileLocation = contentDisposition.getFileName(); // works --> saves file in Eclipse directory

    FileService service = new FileService();
    int code = service.saveUploadedFile(uploadedInputStream, fileLocation);

    String message = code == 200 ? "Datei wurde erfolgreich hochgeladen" : "Datei konnte nicht hochgeladen werden";

    return Response.status(code).entity(message).build();
}

}

Service Class:

public class FileService {

public int saveUploadedFile(InputStream stream, String fileLocation) {
    return writeToFile(stream, fileLocation);
}

// write and save uploaded file to new location
private int writeToFile(InputStream uploadedInputStream,
        String uploadedFileLocation) {

    try {
        FileOutputStream out = new FileOutputStream(uploadedFileLocation);
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
        return 503;
    }

    return 200;
}

}

The upload works fine but I cannot save the file in my project directory. I tried several ways (see code snippet in rest class) but no success, it always wants to save the file outside of my web project folder. Does Jersey avoid it on purpose? This would explain why

System.getProperty("user.dir")

in the rest class has "C:/Program Files/Eclipse/" instead of "C:/Users/LocalAdmin/git/PROJECTNAME/".

The saving directory is: "src/main/resources/media/files"

Environment: Eclipse Keppler, Apache Tomcat v7, Jersey (Jax-RS) v1.18 and Hibernate v4.3 built in Maven (Eclipse plugin)

My goal is to save an uploaded file into my project folder using a relative path because the web project must work on Windows and Linux.

I'd appreciate it to have some advice. Thank you very much.

0

There are 0 answers