开发者

Certain file formats corrupted over FTP

I Have written a server and a client to implement FTP and if i create text files and send them they work fine . but as soon as i send files of other formats the received file on the clients end is a corrupted one. here the code where i,m sending the file

           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 object output - OutputStream object (Socket.getOutputStream())

and the client code is:

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.开发者_JAVA技巧println("File Recieved");

am i using the right streams ?


It sounds like you are sending Binary files in ASCII mode.

Send TYPE I instead of TYPE A on the control channel before you set up the data channel by sending the PORT or PASV command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜