我正在尝试在客户端创建一个Serialized ImageData对象(BufferedImage和long)。并在服务器端反序列化该对象。但问题是我没有在服务器端访问对象实例。我不知道问题是序列化还是反序列化。
客户端:主题1正在创建ImageData对象
线程2:将对象发送到服务器
简而言之,我想将ImageData对象从客户端传输到服务器。 代码正在成功执行,没有异常,没有错误
public class ImageData implements Serializable {
static BufferedImage screencapture;
static long ImageName;
ImageData() throws HeadlessException, AWTException
{
screencapture=new Robot().createScreenCapture(
new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
ImageName=System.currentTimeMillis();
}
}
客户端
abstract class ScreenCapture implements Runnable {
static BufferedImage screencapture;
static ImageData obj;
static FileOutputStream fileOut;
static ObjectOutputStream out;
public static void main(String args[]) throws
AWTException, IOException, InterruptedException {
// Open your connection to a server, at port 1234
final Socket ClientSocket = new Socket("localhost",1234);
try{
//First thread that is Taking screenshot
Thread TakeScreenShotthread = new Thread ()
{
public void run () {
try {
obj= new ImageData();
} catch (HeadlessException | IOException e) {
e.printStackTrace();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
//Thread 2 that is Sending Screenshot to server
Thread sendingScreenShotThread =new Thread () {
public void run () {
try {
fileOut =new FileOutputStream("ImageInfo.ser");
out = new ObjectOutputStream(fileOut);
out.writeObject(obj);
System.out.printf("Serialized data is saved in /tmp/ImageInfo.ser");
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
out.close();
fileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
TakeScreenShotthread.start();
TakeScreenShotthread.sleep(1000);
sendingScreenShotThread.start();
sendingScreenShotThread.sleep(1000);
}finally
{
//Closing Clients
ClientSocket.close();
}
}
服务器
public class ServerConnection
{
static BufferedImage image;
static long name;
static OutputStream out;
static FileInputStream fileIn;
public static void main(String args[]) throws IOException
{
ServerSocket serversock = new ServerSocket(1234);
Socket clientSocket = null;
clientSocket = serversock.accept();
ObjectInputStream in = null;
ImageData obj=null;
try{
boolean processing=true;
while(processing)
{
try{
fileIn = new FileInputStream("ImageInfo.ser");
in = new ObjectInputStream(fileIn);
obj = (ImageData) in.readObject();
in.close();
fileIn.close();
out = new BufferedOutputStream(new FileOutputStream(path+ obj.ImageName + ".jpg"));
ImageIO.write( obj.screencapture, "jpg", out);
System.out.println("Image file written successfully");
} catch (Exception e) {
}finally {
fileIn.close();
out.close();
processing=false;
}
}
}
finally{
clientSocket.close();
serversock.close();
}
}
}