java传文件的程序出错!不知道是何原因?

杨武兵 2007-04-26 09:42:22
java传文件的程序出错!不知道是何原因?
错误描述如下:
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:313)
at java.io.DataInputStream.read(DataInputStream.java:80)
at wjcsq.ConfirmFrame.receiveFiles(ConfirmFrame.java:151)
at wjcsq.ConfirmFrame.jButton3_actionPerformed(ConfirmFrame.java:101)yes

at wjcsq.confirmFrame_jButton3_actionAdapter.actionPerformed(ConfirmFrame.java:261)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at java.io.DataOutputStream.flush(DataOutputStream.java:106)
at wjcsq.transferClient.run(transferClient.java:66)
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:313)
at java.io.DataInputStream.read(DataInputStream.java:80)
at wjcsq.ConfirmFrame.receiveFiles(ConfirmFrame.java:151)
at wjcsq.ConfirmFrame.jButton3_actionPerformed(ConfirmFrame.java:101)yes

at wjcsq.confirmFrame_jButton3_actionAdapter.actionPerformed(ConfirmFrame.java:261)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)
at java.awt.Component.processMouseEvent(Component.java:5488)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at java.io.DataOutputStream.flush(DataOutputStream.java:106)
at wjcsq.transferClient.run(transferClient.java:66)


代码如下:
由于有文字长度限制,
所以代码在下面帖出.


这个问题我弄了好几天了,都没有解决.
郁闷死了。
请各路高手帮忙啊!
...全文
421 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
njp20033266njp 2007-04-29
  • 打赏
  • 举报
回复
我感觉这样写没错
同一时刻却只有一个方法会使用
方法receiveFiles()。
你是指什么!也就是jButton2_actionPerformed或jButton3_actionPerformed在程序只可能走一下吗可是你
socket 是全居的变量一但关闭你会从新在做ConfirmFrame的构造吗?
andrew_fan 2007-04-28
  • 打赏
  • 举报
回复
看到头大。
帖关键代码
njp20033266njp 2007-04-28
  • 打赏
  • 举报
回复
不是方到jbInit();里面
反正等你程序完成后你才能关闭socket.close();
因为receiveFiles()好向不是只一个在用
jButton2_actionPerformed
jButton3_actionPerformed
都调用了
要不你就要在receiveFiles()在打开一次
我能给的想法就这么多了
我水平不高希望有高手来帮忙
杨武兵 2007-04-28
  • 打赏
  • 举报
回复
虽然
jButton2_actionPerformed
jButton3_actionPerformed
都会调用,
但是在同一时刻却只有一个方法会使用
方法receiveFiles()。
杨武兵 2007-04-28
  • 打赏
  • 举报
回复
好的!出现异常的代码是:
while(true)
{
in=0;
in = doc.read(buf);
if(in!=0)
break;
}
和:
if(new String(buf,0,counter).equalsIgnoreCase("yes"))
{
FileInputStream fis = new FileInputStream(file);
int num = fis.read(buf);
while (num != ( -1)) {
os.write(buf, 0, num);
os.flush();
num = fis.read(buf);
}
os.close();
fis.close();
//socket.close();
}
}
杨武兵 2007-04-27
  • 打赏
  • 举报
回复
njp20033266njp():
我认真看了你给的修改意见。
好象不行啊。
socket.close();方到jbInit();
那不是一创建对象就关闭了socket。
那还怎么传输文件啊。
杨武兵 2007-04-27
  • 打赏
  • 举报
回复
njp20033266njp()
非常谢谢你能够抽空看完我的那么代码。
我原本觉得自己发这么代码,肯定没人会去看的。
没抱什么希望。
不过,你真是给了我很大的惊喜啊。
再次感谢。
你给的建议,我也会认真考虑的。
杨武兵 2007-04-27
  • 打赏
  • 举报
回复
njp20033266njp():
老兄:这样改不跟没改一样啊!
njp20033266njp 2007-04-27
  • 打赏
  • 举报
回复
int num = fis.read(buf);
while ((num = fis.read(buffer)) != -1) {
os.write(buf, 0, num);
os.flush();
num = fis.read(buf);
}

这样改一下看看行不行
shuai45 2007-04-27
  • 打赏
  • 举报
回复
这也太长了
看你的错误是java.net.SocketException: socket closed
看样子socket 已经关闭。
njp20033266njp 2007-04-27
  • 打赏
  • 举报
回复
不好意思!看的头大想错了!
socket.close();方到jbInit();后看看不要方在receiveFiles()试一下
杨武兵 2007-04-26
  • 打赏
  • 举报
回复
文件transferClient.java如下:
package wjcsq;
import java.io.*;
import java.net.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class transferClient extends Thread{
private String ip="";
private File file=null;
Socket socket=null;
public transferClient(String ip,File file) {
try {
this.ip = ip;
this.file = file;
socket = new Socket(ip, 4700);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run()
{
try{
int counter;//记录字节个数。
int len = (int) (file.length());
String str = String.valueOf(len);
byte[] buf = new byte[2048];
byte by = '?';
byte by1 = '*';
String fileName = file.getName();
OutputStream ops = socket.getOutputStream();
OutputStream os = new DataOutputStream(new BufferedOutputStream(ops));
InputStream netOut = socket.getInputStream();
InputStream doc=new DataInputStream(new BufferedInputStream(netOut));
os.write(fileName.getBytes());
os.write(by);
os.flush();
os.write(str.getBytes());
os.write(by1);
os.flush();
//循环监视接收端是否允许发送文件,若允许则接收到'yes,否则'no'。
while(true)
{
counter=0;//归零。
counter=doc.read(buf);
if(counter!=0)
break;
}
System.out.println(new String(buf,0,counter));
if(new String(buf,0,counter).equalsIgnoreCase("yes"))
{
FileInputStream fis = new FileInputStream(file);
int num = fis.read(buf);
while (num != ( -1)) {
os.write(buf, 0, num);
os.flush();
num = fis.read(buf);
}
os.close();
fis.close();
//socket.close();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

这个问题我弄了好几天了,都没有解决.
郁闷死了。
请各路高手帮忙啊!
杨武兵 2007-04-26
  • 打赏
  • 举报
回复


//循环等待发送端的数据到来。
while(true)
{

in=0;
in = doc.read(buf);
if(in!=0)
break;
}
//in=doc.read(buf);
while(in!=(-1)){
raf.write(buf,0,in);
raf.skipBytes(in);
plen=plen+(int)(raf.length());
//in=doc.read(buf);
//循环等待发送端的数据到来。
while(true)
{
in=0;
in = doc.read(buf);
if(in!=0)
break;
}

}

//关闭各种资源。
raf.close();
doc.close();
socket.close();
}
/*拒绝接收文件时的处理。*/
public void refuseFiles() throws IOException
{
OutputStream ops = null;
ops = socket.getOutputStream();
OutputStream os=new DataOutputStream(new BufferedOutputStream(ops));
os.write("no".getBytes());
os.flush();
os.close();
}
}



class confirmFrame_jButton1_actionAdapter implements ActionListener {
private ConfirmFrame adaptee;
confirmFrame_jButton1_actionAdapter(ConfirmFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}


class confirmFrame_jButton2_actionAdapter implements ActionListener {
private ConfirmFrame adaptee;
confirmFrame_jButton2_actionAdapter(ConfirmFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}


class confirmFrame_jButton3_actionAdapter implements ActionListener {
private ConfirmFrame adaptee;
confirmFrame_jButton3_actionAdapter(ConfirmFrame adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}
杨武兵 2007-04-26
  • 打赏
  • 举报
回复
文件ConfirmFrame.java
package wjcsq;

import java.awt.event.ActionEvent;
import wjcsq.confirmFrame_jButton2_actionAdapter;
import wjcsq.confirmFrame_jButton1_actionAdapter;
import com.borland.jbcl.layout.XYLayout;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import wjcsq.confirmFrame_jButton3_actionAdapter;
import java.io.RandomAccessFile;
import java.net.Socket;
import javax.swing.JTextArea;
import java.io.InputStream;
import java.io.IOException;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.File;
import ywb.confirmFrame;
import java.io.FileOutputStream;
import com.borland.jbcl.layout.XYConstraints;
import javax.swing.JFrame;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;

/**
* <p>Title: java文件传输器</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author 杨武兵
* @version 1.0
*/
public class ConfirmFrame extends JFrame{
XYLayout xYLayout1 = new XYLayout();
JTextArea jTextArea1 = new JTextArea();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JFileChooser jFileChooser1 = new JFileChooser();
File fileDirectory = new File("c:/"); //文件保存绝对路径。如果不修改的话,默认路径是c:/。
String ipAdd = "127.0.0.1"; //发送的ip地址。
String fileName = ""; //发送的文件的文件名称。
String fileLen="0";//发送来的文件大小。单位:字节。
Socket socket = null; //与客户端建立的连接。
public ConfirmFrame(String ipAdd, Socket socketObject) {
this.ipAdd = ipAdd;
this.socket = socketObject;
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}

private void jbInit() throws Exception {
this.getContentPane().setLayout(xYLayout1);
xYLayout1.setWidth(437);
xYLayout1.setHeight(326);
jButton3.setText("接受另存为...");
jButton3.addActionListener(new confirmFrame_jButton3_actionAdapter(this));
jButton2.setText("接受默认路径");
jButton2.addActionListener(new confirmFrame_jButton2_actionAdapter(this));
jButton1.setText("拒绝");
jButton1.addActionListener(new confirmFrame_jButton1_actionAdapter(this));
jTextArea1.setEditable(false);
jTextArea1.setText("地址:" + ipAdd + "向您发送文件!\n" );
//+ "文件名是:"+ fileName+"\n"+"文件大小是:"+fileLen+"字节");
this.getContentPane().add(jTextArea1,
new XYConstraints(29, 62, 378, 113));
this.getContentPane().add(jButton3, new XYConstraints(29, 219, 124, 24));
this.getContentPane().add(jButton1, new XYConstraints(336, 219, 71, 24));
this.getContentPane().add(jButton2, new XYConstraints(171, 219, 120, 24));
this.setSize(437, 326);
this.setResizable(false);
this.setVisible(true);
}

protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
this.setVisible(false);
}
}

public static void main(String[] args) {
//confirmFrame cf= new confirmFrame("192.168.1.1");
}

public void jButton3_actionPerformed(ActionEvent e) {
jFileChooser1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jFileChooser1.showSaveDialog(this);
fileDirectory = jFileChooser1.getSelectedFile();
try {
this.receiveFiles();
} catch (Exception ex) {
ex.printStackTrace();
}
this.setVisible(false);
}

public void jButton2_actionPerformed(ActionEvent e) {
try {
this.receiveFiles();
} catch (Exception ex) {
ex.printStackTrace();
}
this.setVisible(false);
}

public void jButton1_actionPerformed(ActionEvent e){
//拒绝接受来自客户端发送的文件。
try {
refuseFiles();
//socket.close();
} catch (IOException ex) {
ex.printStackTrace();
}
this.setVisible(false);
}
//接收来自于发送端的文件。
public void receiveFiles()throws Exception
{
byte[] buf=new byte[2048];
byte by='?';
byte by1='*';
int count=0;
int count1=0;
int flag=1;
int in;//字节个数计数器。
InputStream netOut=socket.getInputStream();
InputStream doc=new DataInputStream(new BufferedInputStream(netOut));
OutputStream ops=socket.getOutputStream();
OutputStream os=new DataOutputStream(new BufferedOutputStream(ops));
//向发送端发送要接收文件的信号。
os.write("yes".getBytes());
os.flush();
os.close();
//循环等待发送端的数据到来。
while(true)
{
in=0;
in = doc.read(buf);
if(in!=0)
break;
}
for(int i=0;i<in;i++)
{
if( buf[i]==by)
{ count=i;
break;
}
}
for(int j=0;j<in;j++)
{
if( buf[j]==by1)
{
count1=j;
break;
}
}
//文件名称。
fileName=new String(buf,0,count);
//文件长度。
String fileLen=new String(buf,count+1,count1-count-1);
int intLen=Integer.parseInt(fileLen);
System.out.println("文件名称为:"+fileName);
System.out.println("文件长度为:"+fileLen);
File file=new File(fileDirectory+"\\"+fileName);
FileOutputStream fis=new FileOutputStream(file);
file.createNewFile();
RandomAccessFile raf=new RandomAccessFile(file,"rw");
raf.write(buf,count1+1,in-count1-1);
String str1=new String(buf,count1+1,in-count1-1);
raf.skipBytes(in-count1-1);
int plen=(int)(raf.length());

81,094

社区成员

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

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