无法使用Java代码访问Apache服务器中托管的映像

时间:2014-01-02 02:47:04

标签: java jsp

我的java代码是这样的。

String imgSrc= GlobalConstants.CDN_SERVER+ "/images/generic_image.gif";                     
BufferedImage originalImage=ImageIO.read(new File(imgSrc));
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos );
byte[] imageInByte=baos.toByteArray();

我已将文件托管在 Apache HTTP服务器

GlobalConstants.CDN_SERVER "http://localhost/abc"

我正在尝试从Apache服务器访问 generic_image.gif ,但它为第二行代码抛出此错误.i.e。 BufferedImage originalImage=ImageIO.read(new File(imgSrc))

javax.imageio.IIOException: Can't read input file!
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)

但是,如果我试图从jsp访问相同的图像,它的工作正常。即

<src="<%=GlobalConstants.CDN_SERVER%>/images/generic_image.gif"/>

我做错了吗?引导我,谢谢。

1 个答案:

答案 0 :(得分:1)

尝试以下代码

URL url = new URL(GlobalConstants.CDN_SERVER+ "/images/generic_image.gif");
BufferedImage originalImage = ImageIO.read(url);