0

我已经编写了一个服务器和一个客户端来实现 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");

我在使用正确的流吗?

4

1 回答 1

1

听起来您正在以 ASCII 模式发送二进制文件。

在通过发送PORTPASV命令设置数据通道之前,在控制通道上发送TYPE I而不是TYPE A。

于 2011-09-13T20:09:25.817 回答