Jersey文件上传:无法将文件保存到Web项目中

时间:2014-03-18 18:07:02

标签: java-ee upload jersey relative-path

我正面临着泽西岛文件上传的问题。我想上传特定的文件并使用@FormDataParam和@FormDataContentDisposition临时保存在 我的web项目文件夹中,该文件使用以下代码:

我的休息班:

@Path("file")

公共类FileRest扩展了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();
}

}

服务类:

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

}

上传工作正常,但我无法将文件保存在项目目录中。我试过几种方法(参见休息类中的代码片段)但没有成功,它总是希望将文件保存在我的web项目文件夹之外。泽西是否故意避免它?这可以解释为什么

System.getProperty("user.dir")
其余类中的

有" C:/ Program Files / Eclipse /"而不是" C:/ Users / LocalAdmin / git / PROJECTNAME /"。

保存目录是:" src / main / resources / media / files"

环境:Eclipse Keppler,Apache Tomcat v7,Jersey(Jax-RS)v1.18和Maven内置的Hibernate v4.3(Eclipse插件)

我的目标是使用相对路径将上传的文件保存到我的项目文件夹中,因为Web项目必须在Windows和Linux上运行。

我很感激有一些建议。非常感谢你。

0 个答案:

没有答案