0
这里有一个小代码。这个类运行在两台计算机上,一方发送一个文件(send()),另一个接收它(read())。我知道send()的作品,因为当我运行学校解决方案(它的任务),它可以从我这里下载文件,但由于某种原因,当我尝试下载文件创建(由构造函数),但阅读不写进入文件。无法通过套接字传输文件,java
public class SendFile extends BasicMessage implements Message{
private File _file;
public SendFile(CommandEnum caption){
super(caption);
}
public SendFile(String file){
super(CommandEnum.FILE);
_file = new File(FMDataManager.instance().getSharedDirectory(),file);
}
public void send (DataOutputStream out) throws IOException{
out.writeUTF(_caption.toString());
out.writeLong(_file.length());
FileInputStream fis = new FileInputStream(_file);
BufferedInputStream bis = new BufferedInputStream(fis);
for (int i=0; i<_file.length(); i++)
out.write(bis.read());
out.writeUTF(CommandEnum.END.toString());
}
public void read(DataInputStream in) throws IOException{
FileOutputStream fos = new FileOutputStream(_file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
in.readUTF();
long size = in.readLong();
for (int i=0; i<size; i++)
bos.write(in.read());
System.out.println(in.readUTF());
}
}
有什么想法吗?谢谢
你可以看到我在这些方法中获取数据流作为参数。这个流稍后会在它创建的同一个类中关闭,你是否仍然认为这可能是问题所在? – yotamoo 2011-05-20 09:32:53
你是对的,谢谢 – yotamoo 2011-05-20 09:49:33