开发者

how to read image from project folder in java?

开发者 https://www.devze.com 2022-12-22 04:29 出处:网络
I am developing web method for webservice in java. In this web method I have to read image from my images folder which resides in my webservice project folder. I am using the code a开发者_JS百科s foll

I am developing web method for webservice in java. In this web method I have to read image from my images folder which resides in my webservice project folder. I am using the code a开发者_JS百科s follows.

@WebMethod(operationName = "getAddvertisementImage")
public Vector getAddvertisementImage()
{
    Image image = null;
    Vector imageList = new Vector();

    try
    {
     File file = new File("E:/SBTS/SBTSWebservice/web/adv_btm.jpg");
     image = ImageIO.read(file);
     imageList.add(image);
    }
  catch (IOException e)
  {
        e.printStackTrace();
  }
    return imageList;
}

I am unable to read image from images folder.I am getting error image file "input file can't read" at image = ImageIO.read(file); how to resolve this issue ? Is there any mistake in my code or is there any other way to read image ? if there is any mistake in my code then can you proide me the code or link through which i can resolve the above issue.


Is the E:\ drive mapped on your web server? The Java compiler has no idea that you might access files outside of its scope and how it could tell your web server to map a network drive or a local hard disk which is attached to your development computer.

The solution is to put the image file into the same directory as the Java source file and then use

InputStream in = getClass().getResourceAsStream("adv_btm.jpg");

Check that your IDE (or whatever you use to build your application) does copy the image file in the same directory where it creates the .class file. Then it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消