【问题标题】:java web appliaction save image on project folderjava web应用程序将图像保存在项目文件夹中
【发布时间】:2015-09-09 08:09:32
【问题描述】:

我正在尝试将图像文件保存到我的项目文件夹中。 图像文件来自数据库。 它是一个 Maven 项目和 REST Web 服务。 我没有任何servlet。 这是我的代码,但它保存在 eclipse 文件夹中。

        byte[] imgData = null;
        Blob img = null;
        img = resultset.getBlob("LOGO");
        imgData = img.getBytes(1, (int) img.length());

        BufferedImage img2 = null;
        img2 = ImageIO.read(new ByteArrayInputStream(imgData));

        File outputfile = new File("birimler/"+resultset.getString("BASLIK")
                + "Logo.png");
        outputfile.mkdirs();
        ImageIO.write(img2, "png", outputfile);
        System.out.println(outputfile.getAbsolutePath());

输出为:/Users/xxx/Documents/eclipse/Eclipse.app/Contents/MacOS/birimler/imageLogo.png

感谢您的帮助!

【问题讨论】:

    标签: java eclipse rest tomcat web-applications


    【解决方案1】:

    那是因为 eclipse 工作目录是他的安装文件夹。 提供完整的绝对路径,或更改运行配置的工作目录。

    File outputfile = new File("/birimler/"+resultset.getString("BASLIK")
                    + "Logo.png");
    

    最终会出现在

    "/birimler/imageLogo.png"

    再添加一个斜线:

    File outputfile = new File("/birimler/"+resultset.getString("BASLIK")
                    + "/Logo.png");
    

    会产生:

    "/birimler/image/Logo.png"

    【讨论】:

    • 它现在在我的本地机器上,稍后我将在服务器上的 tomcat8 中使用。如果我从 Eclipse 更改目录,它在服务器上不起作用。
    • 稍后在服务器上,tomcat 的工作目录通常是他的安装文件夹。无论如何,提供绝对路径以确保目的地。
    • 感谢您的帮助,但我如何提供绝对路径。本机操作系统为 Mac OSX,服务器为 Windows。
    • 通过在路径前添加斜线!或者如果你想让它写使用 File.separator ,其中是 eitehr / 或 \ 取决于操作系统
    • 别忘了点赞并检查我的答案是否正确
    猜你喜欢
    • 2018-10-26
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    相关资源
    最近更新 更多