【发布时间】:2011-09-06 15:55:37
【问题描述】:
我已经编写了一个服务器和一个客户端来实现 FTP,如果我创建文本文件并发送它们,它们就可以正常工作。但是,一旦我发送其他格式的文件,客户端上收到的文件就会损坏。这里是我发送文件的代码
try
{
fis=new FileInputStream(filenm);
}
catch(FileNotFoundException exc)
{
filexists=false;
System.out.println("FileNotFoundException:"+exc.getMessage());
}
if(filexists)
{
System.out.println("sent");
sendBytes(fis, output);
fis.close();
}
private static void sendBytes(FileInputStream f,OutputStream op)throws Exception
{
byte[] buffer=new byte[1024];
int bytes=0;
while((bytes=f.read(buffer))!=-1)
{
op.write(buffer,0,bytes);
}
}
fis - FileInputStream 对象 output - OutputStream 对象 (Socket.getOutputStream())
而客户端代码是:
File f=new File(dir,"file2");
FileOutputStream fos=new FileOutputStream(f);
DataOutputStream dops=new DataOutputStream(fos);
System.out.println("2nd Stage");
while(done)
{
fc2=br.read();
if(fc2==-1)
{
done=false;
}
else
{
dops.write(fc2);
}
}
fos.close();
System.out.println("File Recieved");
我使用正确的流吗?
【问题讨论】:
-
你记得把传输模式设置为IMAGE吗?
-
为什么在服务端和客户端使用不同的读取循环?服务器代码要好得多。
-
@raymond 对不起,如果这听起来很愚蠢,但是我如何将传输模式设置为图像也将传输比图像文件更多类型的文件
-
您使用
TYPE命令。请参阅 RFC 959。
标签: java sockets file-handling