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()). 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.
public class SendFile extends BasicMessage implements Message{
private File _file;
public SendFile(CommandEnum caption){
super(caption);
}
public SendFile(String file){
super(CommandEnu开发者_StackOverflow社区m.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.
精彩评论