我正在尝试按照服务器代码通过网络发送图像。
import java.net.*;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Server {
static private BufferedImage bi;
static Socket socket;
static ServerSocket serverSocket;
DataInputStream dis=null;
public static void connect() throws IOException
{
serverSocket = new ServerSocket(9632);
System.out.println("i am server & listening...");
socket = serverSocket.accept();
System.out.println("Connected");
}
public static void main (String [] args ) throws IOException {
connect();
receiveimage();
showimage();
}
public static void receiveimage()
{
byte[] data;
while (true){
try{
System.out.println("Reading Image");
InputStream in=socket.getInputStream();
data=new byte[socket.getReceiveBufferSize()];
in.read(data, 0, socket.getReceiveBufferSize());
Image image = getPhoto(data);
bi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
File outputfile = new File("saved.png");
ImageIO.write(bi, "png", outputfile);
}
catch(Exception ex)
{
System.out.println("error: " + ex.toString());
}
}
}
public static void showimage() throws IOException
{
File file = new File("saved.png");
Image image = ImageIO.read(file);
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static Image getPhoto(byte[] bytePhoto) throws IOException {
return ImageIO.read(new ByteArrayInputStream(bytePhoto)); //bytePhoto is the byte array
}
}
并关注客户端代码
import java.net.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class client{
public static byte[] bytePhoto;
public static Socket sock;
public static void connect() throws IOException
{
Socket sock = new Socket("172.16.0.143",9632);
System.out.println("Connected");
}
public static void main (String [] args ) throws IOException, ClassNotFoundException {
connect();
sendphoto();
}
public static void sendphoto() throws IOException
{
InputStream is = new BufferedInputStream(new FileInputStream("c:\\ziki.png"));
Image image = ImageIO.read(is);
byte[] data=setPhoto(image);
// OutputStream output = sock.getOutputStream();
//output.write(data);
}
public static byte[] setPhoto(Image img) throws IOException {
ImageIcon icon = new ImageIcon(img);
BufferedImage bImg = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = bImg.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
ByteArrayOutputStream writer = new ByteArrayOutputStream();
ImageIO.write(bImg, "jpg", writer);
return bytePhoto = writer.toByteArray(); //bytePhoto is a byte array
}
}
它在客户端给我Java Null指针异常。
由于
请尽快帮忙。我一直在努力搜索这个网站,但由于我是Java的新手,因此无法找到真正有用的东西。
答案 0 :(得分:0)
当我运行它时,我在服务器端得到一个NullPointerException。 ImageIO.read
中的getPhoto
正在返回null
,因为我没有注册能够阅读该流的ImageReader
。
类路径中的任何ImageInputStreamSpi
都会自动注册,所以我想我没有。{我不知道你的机器上安装了什么,但它可能是一个类似的问题。