将图像保存到Web应用程序中的项目文件夹

时间:2013-03-29 14:14:12

标签: java

我需要将图像保存到项目文件夹中。这是一个Web应用程序。 我无法弄清楚如何找到我要保存图像的文件夹的绝对路径。当我尝试创建空文件夹并获得绝对路径时,我将获得eclipse路径。

有人可以帮我解决这个问题吗?

以下是我试图保存图片的一些代码:

public void saveCustomsLabel(byte[] array, Obj1, String str) throws userException {
    byte[] array2 = null;

    try {

       imageInByte = array;

        OutputStream bos = new ByteArrayOutputStream();
        InputStream bis = new ByteArrayInputStream(imageInByte);
        BufferedImage bImageFromConvert = ImageIO.read(bis);
        File path = new File("Templates/Images/Image.bmp")
        ImageIO.write(bImageFromConvert, "bmp", path);

2 个答案:

答案 0 :(得分:1)

您可以尝试部署项目。在本地机器上,文件进入eclipse路径。

答案 1 :(得分:0)

将文件位置用作“/Templates/Images/Image.bmp”,此位置应位于运行服务器的同一驱动器中(如果是Windows)。在linux上它将来自root目录。

File path = new File("/Templates/Images/Image.bmp")

link可能对您有所帮助

试试这个

File file = new File("/opt/image.png");

    if(!file.exists()) {
        try {
            file.mkdirs();
            file.createNewFile();

            BufferedImage image = new BufferedImage(100,100,1);

             ImageIO.write(image, "JPG", file); 

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
相关问题