我正在使用以下代码编写jpg文件:
String url="http://img01.taobaocdn.com/imgextra/i1/449400070/T2hbVwXj0XXXXXXXXX_!!449400070.jpg";
String to="D:/temp/result.jpg";
ImageIO.write(ImageIO.read(new URL(url)),"jpg", new File(to));
Run Code Online (Sandbox Code Playgroud)
但我得到的result.jpg是一个粉红色的背景图片:

我将图像绘制到jframe时出现null异常错误.我调试代码并检查图像和帧不是null但仍然在绘制图像到帧时抛出NULL异常.
请看一看 :
public void run(){
try{
ObjectInputStream objVideoIn = new ObjectInputStream(serVideoIn);
byte[] imgbytes=null;
ByteArrayInputStream barrin=null;
JFrame jf = new JFrame();
Graphics ga=jf.getGraphics(); //Getting null exception
//Thread.sleep(10000);
jf.setVisible(true);
jf.setSize(400, 400);
while(true){
int index=0;
//Thread.sleep(300);
int size= (int)objVideoIn.readObject();
imgbytes = new byte[size];
barrin = new ByteArrayInputStream(imgbytes);
System.out.println("image size" + size);
//Thread.sleep(200);
while(index<size)
{
System.out.println("reading image");
int bytesread = objVideoIn.read(imgbytes, index, size-index);
if(bytesread<0){
System.out.println("error in receiving bytes yar");
}
index+=bytesread;
}
//barrin.read(imgbytes, 0, imgbytes.length);
barrin = new ByteArrayInputStream(imgbytes);
buffImg = ImageIO.read(barrin);
if(buffImg==null) …Run Code Online (Sandbox Code Playgroud) 我设计了一个休息服务来响应用户存储在数据库中的图像.服务很好.该服务以jpg格式响应图像.如果用户以jpg格式上传图像,则响应也很好,如果图像是某些其他格式,则响应不能呈现为图像.我需要为所有输入图像类型构建一个jpg转换器或编码器.有没有可能的方法来实现这一目标.