传送图片的问题(ObjectOutputStream)

newman0708 2003-05-23 09:03:16
我在运行时出现
out of memories的错误
还有
错误指在这里!!!!!
ObjectOutputStream out=new ObjectOutputStream(new GZIPOutputStream(socket.getOutputStream()));


应该怎么改?

请帮忙看一下,谢谢!


//服务器端
public void run(){
try {
java.awt.Robot robot=new java.awt.Robot();
BufferedImage bi=new BufferedImage(800,600,BufferedImage.TYPE_INT_ARGB);
System.out.println("start capture screen");

ObjectOutputStream out=new ObjectOutputStream(new GZIPOutputStream(socket.getOutputStream()));//########错误指在这里!!!!!

System.out.println("create ObjectOutputStream is ok!");

while (true) {
bi=robot.createScreenCapture(new Rectangle(800,600));//?????
System.out.println("create Screen Capture is ok!");

image=new ImageIcon((Image)bi);
out.writeObject(image);
out.flush();
image=null;
try {
Thread.sleep(1500);
}
catch (Exception ex) {
System.err.println("sleep error.");
}
}
}
catch (Exception ex) {
System.err.println("client Error");
}


//客户端
public void run(){
try {
ServerSocket ss=new ServerSocket(port);
System.out.println("server start.");
socket=ss.accept();
System.out.println("accept a client."+socket.getInetAddress() .getHostName());
ObjectInputStream is=new ObjectInputStream(new GZIPInputStream(socket.getInputStream()));
System.out.println("reading data now");
ImageIcon img;
while (true) {
//System.out.println("available: "+is.available());
img=(ImageIcon)is.readObject();
this.label .setIcon(null) ;
this.label .repaint();
this.label.setIcon(img);
this.label .repaint();
img=null;
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
...全文
75 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
newman0708 2003-05-30
  • 打赏
  • 举报
回复
我想要ObjectOutputStream,

看来我还是自己搞吧。
nil2000 2003-05-27
  • 打赏
  • 举报
回复
简单写了一下,SOCKET连接部分没有写。
public void send() {
try {
File f = new File("d:\\test.jpg");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
int ch = bis.read();
while(ch != -1) {
bos.write(ch);
ch = bis.read();
}
bos.flush();
bos.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void receive() {
try {
File f = new File("d:\\test1.jpg");
if(!f.exists()) {
f.createNewFile();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
int ch = bis.read();
while(ch != -1) {
bos.write(ch);
ch = bis.read();
}
bos.flush();
bos.close();
bis.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
nil2000 2003-05-26
  • 打赏
  • 举报
回复
如上所述,socket流应该在使用完后关闭。如你的out,is
bluesmile979 2003-05-25
  • 打赏
  • 举报
回复
注意看看你的流是否的都在用过以后关闭了,否则会产生很严重的问题,而且不容易发现。
bluesmile979 2003-05-25
  • 打赏
  • 举报
回复
while (true) {
bi=robot.createScreenCapture(new Rectangle(800,600));//?????
System.out.println("create Screen Capture is ok!");

image=new ImageIcon((Image)bi);
out.writeObject(image);
out.flush();
out.close();
image=null;
try {
Thread.sleep(1500);
}
catch (Exception ex) {
System.err.println("sleep error.");
}
}
newman0708 2003-05-25
  • 打赏
  • 举报
回复
我觉得好像不是内存不足的问题,而是我的程序占了内存没有释放而引起。
因为,刚开始时是可以运行,但是过了半分钟后,就出现了out of memories的错误,程序终止了。

请再来指点一下!

newman0708 2003-05-25
  • 打赏
  • 举报
回复
程序的工作是:
服务器:接收客户端发来的image对象(也就是图片)

客户端:先截取屏幕,将它转化为image类型,发发送到服务器端。

在运行了一会儿后就出现了out of memories的错误

不知道怎么解决,请指点一下。
bluesmile979 2003-05-25
  • 打赏
  • 举报
回复
内存不足

运行的时候java -Xmx256M (youclassname)

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧