如何在相同的项目文件夹中获取相对路径作为图像文件的字符串?

时间:2016-05-05 13:03:38

标签: java file

enter image description here

请不要贬低问题冠军!谢谢观看!! 我想获得"Tulips.jpg"(src/resource)的相对路径。我需要将这些文件保存在webcontent中吗?请帮我看看。

One way -// `private static final String FILE_PATH = ".\\WebContent\\uploadFiles\\Tulips.jpg";`
Another way -// `private static final String FILE_PATH = "D://uploadFiles/Tulips.jpg";`

    //  private static final String FILE_PATH = "/image.process/resource";

    URL url =     ClassLoader.getSystemResource("/image.process/resource/Tulips.jpg");
    String filename = "Tulips.jpg";

    @GET
    @Path("/get")
    @Produces("image/*")
    public Response getFile() {


         System.out.println("Working Directory = " +
                  System.getProperty("user.dir"));

        File file = new File(url + filename);

        ResponseBuilder response = Response.ok((Object) file);
        response.header("Content-Disposition",
            "attachment; filename=image_from_server.jpg");
        return response.build();

    }

2 个答案:

答案 0 :(得分:1)

您可以使用的一种方法是从当前线程获取类加载器并获取资源。

Thread.currentThread().getContextClassLoader().getResource("Tulips.jpg")

答案 1 :(得分:0)

在你的代码中输入它(最好是主方法)

Glib::Timeout->add( 20, \&update );

将tulips.png放入运行时打印的文件夹中,因为这会打印出程序正在查找的文件夹。如果您希望保留目录结构,请将资源文件夹移动到打印的文件夹