跪求如何解决java中由getInputStream()方法带来的Socket is closed问题?

以下是报错的部分

jbt1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                num=1;
                String ss=String.valueOf(num);
                OutputStream out = null;
                try {
                    out = socket.getOutputStream();
                } catch (IOException e1) {
                    // TODO 自动生成的 catch 块
                    e1.printStackTrace();
                }
                try {
                    out.write(ss.getBytes());
                } catch (IOException e1) {
                    // TODO 自动生成的 catch 块
                    e1.printStackTrace();
                }
                try {
                    out.close();
                } catch (IOException e1) {
                    // TODO 自动生成的 catch 块
                    e1.printStackTrace();
                }
                InputStream in=null;
               try {
                    in = socket.getInputStream();//**【【【【这里被报错**
                    byte[] bs=new byte[100];//每次接受100字节
                    int len=-1;//随意赋值
                    OutputStream fileOut=new FileOutputStream("src/Client/1.jpg");

                    while((len=in.read(bs))!=-1) {
                        fileOut.write(bs,0,len);
                    }

                    //fileOut.close();
               }catch(Exception e1) {e1.printStackTrace();}

                }


        });

报错信息:

java.net.SocketException: Socket is closed
    at java.base/java.net.Socket.getInputStream(Socket.java:905)
    at chris.Client$2.actionPerformed(Client.java:91)//【【应该是这里的问题
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6632)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6397)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5008)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2762)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4840)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

2个回答

socket已经被out读取一次了。

DeclanC
SeerSky. 关了也不行?
5 个月之前 回复

socket超时了吧

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

3
遇到个 java 加解密的难题
3
java.net.UnknownHostException: src="https运行报错
2
java如何通过图片链接获取图片的格式
6
调用别人的rest接口,本地测试返回不乱码,运行在tomcat上会出现部分中文乱码,跪求大神
8
使用java的Http请求里的Url对象调用第三方接口返回的数据中文乱码
2
如何在java里调用使用get方法,参数以application/x-www-form-urlencoded方式传递的web接口?
1
springboot项目启动报nested exception is java.io.FileNotFoundException
2
ffmpeg配了环境变量,在cmd中可以正常使用,Java使用processbuilder调用cmd命令就报错无法执行?
0
java c/s通信问题....
0
求救!!!!!HessianProxy cannot connect to xxxxx
1
java使用webservice服务,创建客户端的时候报错,折磨了一天,各位有缘人帮忙看一下吧
0
ssm+cas+shiro+maven重定向问题,报错Caused by: java.io.FileNotFoundException
3
IDEA测试Maven SSM项目时报错:找不到配置文件[classpath*:mybatis-config.xml]
2
在java项目中用json传值的get请求,接收返回值时直接跳到错误?
2
url在建立连接的时候,包连接超时,偶先现象。
2
ssm整合报500的错无法读取jdbc属性文件
2
spring入门程序报错500?
1
Java 如何在建立 Socket 通信后主动向 C#客户端发送信息
3
我用spring的webclient做文件下载的功能,下载的是一个zip文件,然后报出了?请大家帮帮忙,谢谢!
1
java调用scrapy无反应