【问题标题】:Certain file formats corrupted over FTP某些文件格式通过 FTP 损坏
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多