如何在java中保存通过网络接收的图像

时间:2015-03-30 03:12:03

标签: java networking image-processing

图像在服务器端成功接收,我可以在标签上显示 但我的 问题是如何保存该图片

我用过

  

JFileChooser.showSaveDialog()

我试过了printstream。我可以保存文件,但每当我在图像查看器中打开文件时,它显示为这种类型的文件无法打开


BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(sock.getInputStream()));

System.out.println("Image received!!!!"); 

JFileChooser fc = new JFileChooser();
int i=fc.showSaveDialog(null);
if( i == JFileChooser.APPROVE_OPTION ) {                

    PrintStream ps = new PrintStream(fc.getSelectedFile());

    // ImageIO.write(bimg,"JPG",fc.getInputStream());
    ps.print( img);
    ps.close();
    lblNewLabel.setIcon(new ImageIcon(img)); //image is successfully displaying on the label
}

1 个答案:

答案 0 :(得分:0)

您正在撰写"对象"图像的表示,只有当您通过PrintStream加载它时,您才有可能再次看到它。

尝试使用类似......

的内容
ImageIO.write(img,"JPG",fc.getSelectedFile());

代替