1 2 Previous Next 17 Replies Latest reply: Jan 30, 2013 1:54 AM by EJP RSS

    inputstream and ftp server issue

    940014
      Dear All

      I am trying to write a ftp server.
      I cannot get the correct non-ASCII file name when the ftp client upload file to server.
      I found that the problems would occur when I am using windows explorer on PC, es file manager on android platform.
      However, the problem will be trigger when I am using command prompt based ftp client on windows platform.
      I am using inputstream to get data from socket, however, it does not help.
      And here is my code.
      package com;
      
      import java.io.*;
      import java.net.ServerSocket;
      import java.net.Socket;
      import java.net.SocketException;
      import java.util.StringTokenizer;
      public class QQ {
           public static void main(String[] args) throws FileNotFoundException 
           {
                int serverPort=21,byteCount,i,responseCount=0;
                ServerSocket ssock = null;
                Socket clientSocket=null;
                PrintStream pstream=null;
                BufferedInputStream incommingCommand=null;
                BufferedOutputStream fileOut=new BufferedOutputStream(new FileOutputStream("C:\\Users\\cstsang\\Desktop\\abc.txt"));
                String commandString,param=new String(),clientIP=new String(),temp;
                int clientPort = 0;
                byte []data=new byte[1024];
                try
                {
                     ssock = new ServerSocket(serverPort);
                     clientSocket = ssock.accept();
                     System.out.println("Connected");
                     pstream = new PrintStream(clientSocket.getOutputStream(),true);
                     incommingCommand=new BufferedInputStream(clientSocket.getInputStream());
                     pstream.println("220 Welcome to my server");
                     boolean isRunning=true;
                     while (isRunning)
                     {
                          byteCount=incommingCommand.read(data,0,1024);
                          
                          if (byteCount!=-1)
                          {
                                commandString=new String(data,0,byteCount);
                                //commandString=new String(commandString);
                                commandString=commandString.toUpperCase().trim();
                                 if (commandString.indexOf(" ")>-1)
                                {     param=commandString.substring(commandString.indexOf(" ")+1);
                                     commandString=commandString.substring(0,commandString.indexOf(" "));
                                }
                                System.out.println(commandString);
                                switch (commandString)
                                {
                                     case "USER":pstream.println("331 Password");
                                                    break;
                                     case "PASS":pstream.println("230 User");
                                                   break;
                                     case "SITE":
                                     case "TYPE":
                                     case "NOOP":     
                                     case "OPTS":pstream.println("200 OK");
                                                    break;
                                     case "PWD" :pstream.println("257 \"/\" is working directory");
                                                    break;
                                     case "CWD" :pstream.println("257 \"/\" is working directory");
                                                   break;
                                     case "NLST":               
                                     case "LIST":pstream.println("150 Opening data");
                                                    Socket fsSocket=new Socket(clientIP,clientPort);
                                                    PrintWriter fpstream = new PrintWriter(new OutputStreamWriter(fsSocket.getOutputStream()));
                                                    //fpstream.print("..\r\n");
                                                     fpstream.flush();
                                                    fpstream.close();
                                                     fsSocket.close();
                                                    pstream.println("226 ok");
                                                    break;
                                     case "STOR":pstream.println("550 Syntax error");
                                                     pstream.flush();
                                                    break;
                                     case "PORT":param=param.trim();
                                                    StringTokenizer p=new StringTokenizer(param,",");
                                                    if (p.countTokens()!=6)
                                                    {
                                                         pstream.println("501 syntax error");
                                                    }
                                                    else
                                                    {
                                                          
                                                         clientIP="";
                                                         for (i=0;i<4;i++)
                                                         {
                                                              temp=p.nextToken();
                                                              if ((Integer.parseInt(temp)<0)||(Integer.parseInt(temp)>255))
                                                              {
                                                                   pstream.println("501 syntax error");
                                                                   return;
                                                              }
                                                              else
                                                              {
                                                                   clientIP+=temp+".";
                                                              }
                                                         }
                                                         clientIP=clientIP.substring(0,clientIP.length()-1);
                                                         int high = Integer.parseInt(p.nextToken());
                                                        int low = Integer.parseInt(p.nextToken());
                                                        if (high < 0 || high > 255 || low < 0 || low > 255)
                                                         {
                                                              pstream.println("501 syntax error");
                                                              return;
                                                         }                    
                                                        else
                                                        {
                                                            clientPort=(high << 8) + low;
                                                            System.out.println("clientIP="+clientIP+",clientPort="+clientPort);
                                                              pstream.println("200 PORT command successfully");
                     
                                                        }
                                                    }
                                                    break;
                                     case "SYST":pstream.println("215 UNIX Type");
                                                    break;
                                     case "QUIT":isRunning=false;
                                                    break;
                                }
                                pstream.flush();
                               fileOut.write(data,0,byteCount);
                               fileOut.flush();
                          }                    
                     }
                }
                catch (SocketException se)
                {
                     System.out.println("connection lost");
                }
                catch (IOException e) 
                {
                     // TODO Auto-generated catch block
                     System.out.println("connection lost");
                     e.printStackTrace();
                }
                finally
                {
                     if (pstream !=null)
                     {
                          pstream.close();
                          pstream =null;
                     }
                     if (incommingCommand!=null)
                     {
                          try {
                               incommingCommand.close();
                          } catch (IOException e) {
                               // TODO Auto-generated catch block
                               e.printStackTrace();
                          }
                          incommingCommand=null;
                     }
                     if (clientSocket!=null)
                     {
                          try {
                               clientSocket.close();
                          } catch (IOException e) {
                               // TODO Auto-generated catch block
                               e.printStackTrace();
                          }
                          clientSocket=null;
                     }
                     try {
                          fileOut.flush();
                          fileOut.close();
                     } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                     }
                     fileOut=null;
                }
           }
      }
      Edited by: 937011 on 2013/1/28 上午 6:25

      Edited by: EJP on 29/01/2013 10:10: code tags
        • 1. Re: inputstream and ftp server issue
          EJP
          Well as all you're doing on STOR is printing 'syntax error' this is hardly to be wondered at.

          What's your question?
          • 2. Re: inputstream and ftp server issue
            940014
            As you can see the output file (i.e. C:\\Users\\cstsang\\Desktop\\abc.txt) content, it store all the commands that sent by ftp client.
            All data are written to this file in binary format;I suppose all data should be stored in file without any changes.
            As I know the STOR command format as the following:

            STOR filename

            where filename contain some non-ASCII character
            Unforunately,these characters cannot be stored to file correctly,even in binary format.
            So,is there any tricks to read non-ASCII character from inputstream?

            Edited by: 937011 on 2013/1/28 下午 8:10

            Edited by: 937011 on 2013/1/28 下午 8:24
            • 3. Re: inputstream and ftp server issue
              EJP
              Non-ASCII character such as what? What are you looking at the file with? It sounds to me like everything is working correctly, not otherwise.
              • 4. Re: inputstream and ftp server issue
                940014
                Non-ASCII character,umm....,such as chinese character(both simplified and transditional)
                for example I am using windows explorer to upload a file named"花車聯絡.xls",
                suppose the file should contain "STOR 花車聯絡.xls",however, it store "STOR ?梯??舐窗.xls".

                As you can see my program, I save all incomming command to file in binary mode.
                So, I wonder whether an issue occur in inputstream object,it because I write what I got from inputstream to file only,
                why all non-ascii character cannot be stored file correctly?
                • 5. Re: inputstream and ftp server issue
                  EJP
                  I repeat. What are you looking at the file with? Are you sure it's capable of displaying those characters?
                  • 6. Re: inputstream and ftp server issue
                    940014
                    Sure, it is because the ftp client sent "OPT UTF8 on" command before it upload the file, so I assume the character encoding of incoming command should be UTF8, and I write what I got from inputstream to file(i.e. the abc.txt). Therefore, the character encoding of output file should be UTF8, however, I open the file using different type of editor and character encoding,it still cannot be display correctly.
                    • 7. Re: inputstream and ftp server issue
                      Kayaman
                      937011 wrote:
                      so I assume the character encoding of incoming command should be UTF8
                      Yes.
                      and I write what I got from inputstream to file(i.e. the abc.txt).
                      How do you write it? Are you making sure that the encoding is still UTF8 when you write?
                      Therefore, the character encoding of output file should be UTF8
                      Only if it's the default encoding, or if you explicitly tell it to use UTF8.
                      however, I open the file using different type of editor and character encoding,it still cannot be display correctly.
                      I assume you're opening it in UTF8, otherwise it wouldn't make sense.
                      • 8. Re: inputstream and ftp server issue
                        940014
                        I am using bufferedoutputstream to output what I got from inputstream to file(so no need to deal with character encoding issue).
                        So, if the character encoding of incoming command is UTF8, I suppose the output file should be UTF8 also.(Would you correct me, if I am wrong?)
                        I have tried to open file in UTF8,UTF16-LE,UTF16-BE, however,,it still cannot be display correctly.
                        • 9. Re: inputstream and ftp server issue
                          Kayaman
                          937011 wrote:
                          I am using bufferedoutputstream to output what I got from inputstream to file(so no need to deal with character encoding issue).
                          So, if the character encoding of incoming command is UTF8, I suppose the output file should be UTF8 also.(Would you correct me, if I am wrong?)
                          I have tried to open file in UTF8,UTF16-LE,UTF16-BE, however,,it still cannot be display correctly.
                          You could check the file with a hex editor to see what bytes there are. If you see 0x3F chars in the filename, those are questionmarks and mean that the error in conversion has happened in your program somewhere, otherwise it would mean the editor is b0rked (although it seems that's not the issue).
                          Then you need to check the settings on your FTP clients.
                          • 10. Re: inputstream and ftp server issue
                            EJP
                            I repeat. What are you looking at the file with? Are you sure it's capable of displaying those characters?
                            Sure, it is because the ftp client sent "OPT UTF8 on" command before it upload the file, so I assume the character encoding of incoming command should be UTF8, and I write what I got from inputstream to file(i.e. the abc.txt). Therefore, the character encoding of output file should be UTF8, however, I open the file using different type of editor and character encoding,it still cannot be display correctly.
                            You haven't answered the question. What editor? And are you sure that editor is capable of displaying those characters?

                            Third time of asking.
                            • 11. Re: inputstream and ftp server issue
                              940014
                              I am using Ultraedit which can display chinese character correctly.
                              • 12. Re: inputstream and ftp server issue
                                940014
                                I am using ultraedit which can display chinese character correctly.
                                I use it to check the data in file in hex mode, I found that the file name byte value are not correctly.

                                Edited by: java fans on Jan 29, 2013 9:51 PM
                                • 13. Re: inputstream and ftp server issue
                                  EJP
                                  Getting information out of you is like pulling hen's teeth. 'Were not correct' how? What did you get? and what did you expect?

                                  And let me tell you that I don't expect to have to ask every question three times to get an answer: and please note that this is the second time that I have asked this one. You seem determined to waste time. I don't care about yours, but mine is valuable.
                                  • 14. Re: inputstream and ftp server issue
                                    940014
                                    I am using windows explorer to upload a file named"花車聯絡.xls",
                                    I expect the output file should contain "STOR 花車聯絡.xls",however, it contain "STOR ?梯??舐窗.xls" only.
                                    I tested the program with filezilla, it works fine.
                                    I wonder this is IE bug.
                                    1 2 Previous Next