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

Java 将图像保存到web应用程序的项目文件夹中,java,Java,我需要将图像保存到项目文件夹中。这是一个web应用程序。 我无法确定如何找到要保存图像的文件夹的绝对路径。当我试图创建空文件夹并获取其绝对路径时,我得到的是eclipse路径 有人能帮我解决这个问题吗 以下是我试图保存图像的一些代码: public void saveCustomsLabel(byte[] array, Obj1, String str) throws userException { byte[] array2 = null; try { ima

我需要将图像保存到项目文件夹中。这是一个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);
使用文件位置“/Templates/Images/Image.bmp”,此位置应位于运行服务器的同一驱动器中(对于windows)。在linux上,它将来自根目录

File path = new File("/Templates/Images/Image.bmp")
这可能对你有帮助

像这样试试

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

可能您应该尝试部署您的项目。在本地计算机上,文件将转到eclipse路径。

您可以发布一些代码,以便我们可以看到您正在尝试执行的操作吗?确定它将为您提供eclipse路径,这是因为它是当前目录,因此,如果您可以发布代码并告诉我们何时要保存图像,您可以获得帮助:)基本上,我在这里以字节的形式从Web服务调用获取图像..:下面是一些代码public void saveCustomsLabel(byte[]label,shipping shipping,String pkgId)抛出用户异常{byte[]imageInByte=null;try{imageInByte=label;OutputStream bos=new ByteArrayOutputStream();InputStream bis=new ByteArrayInputStream(imageInByte);BuffereImage bImageFromConvert=ImageIO.read(bis);File path=new File(“Templates/Images/Image.bmp”)ImageIO.write(bImageFromConvert,“bmp”,path);Templates文件夹在哪里???您需要输入完整路径,例如:如果部分
C
中的“Templates”文件夹,路径将为“C:\\Templates\Images\Image.bmp“,请理解我??我正在尝试使用您在上面给出的路径我正在获取资源未找到异常…我已通过您提供的链接…我尝试了这种方法…但它正在eclipse路径中保存图像…我获取以下异常java.io.FileNotFoundException:。\Templates\Images\image.bmp(系统找不到指定的路径)使用我测试过的代码段编辑了我的答案,并且我能够创建映像。只需将我创建BuffereImage对象的方式替换为您的方式。目前我正在Windows计算机上工作..它是一个web应用程序..我尝试使用上述代码段,但仍然看到错误“java.io.IOException:系统找不到指定的路径”exception.:(您能发布完整的stacktrace和实际的代码片段吗。