我的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"/>
我做错了吗?引导我,谢谢。
答案 0 :(得分:1)
尝试以下代码
URL url = new URL(GlobalConstants.CDN_SERVER+ "/images/generic_image.gif");
BufferedImage originalImage = ImageIO.read(url);