[英]failing to transfer a file through a socket, java
here's a little code. 这是一些代码。 This class runs on two computers, one side sends a file (send()) and the other one recieves it (read()).
该类在两台计算机上运行,一侧发送文件(send()),另一侧接收文件(read())。 I know send() works because when i run school solution (its an assignment) it can download a file from me, but for some reason when i try to download the file is created (by the constructor) but read doesn't write anything into the file.
我知道send()可以工作,因为当我运行学校解决方案(它的作业)时,它可以从我这里下载文件,但是由于某些原因,当我尝试下载文件时(由构造函数创建),但read没有写任何东西到文件中。
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());
}
}
any ideas? 有任何想法吗? thanks
谢谢
You must close your streams to ensure it is correct. 您必须关闭流以确保其正确。 In your particular case, file content is probably still inside the BufferedOutputStream.
在您的特定情况下,文件内容可能仍在BufferedOutputStream中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.