Results 1 to 4 of 4
  1. #1
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default checking file extensions

    Hi,

    The code i have made i believe should check for a certain file extension and then send it out the socket.

    Java Code:
        public void run() {
            Server sv = new Server();
            File folder = new File(sv.getFilePath());
            File[] listOfFiles = folder.listFiles();
    
            out.println(listOfFiles.length);
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {                                //make sure its a file
                    String fileName = listOfFiles[i].getName();     //put file name into string
                    int pos = fileName.lastIndexOf(".");              //find the pos of the . in the filename
                    String ext = fileName.substring(pos + 1);      // get extention name and place into string ext
                    if (ext.equals("jpg") )                                //check the file extension is a jpg file type
                    {
                        out.println(fileName);      //send filename to client
                    }
                }
            }
        }
    if i remove the "if statement" where it checks for the file type it runs fine only with every file type in that directory being sent to the client. Netbeans does not show any errors at all either.

    thanks

    gisler

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Possible spellings of a "jpg" extension can be:
    "jpg", "jpeg", "JPG", and "JPEG"
    Try lowercasing the ext and testing for both spellings.

  3. #3
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    how would that matter since i am simply comparing what is in my ext string to the string "jpg". Wouldn't it just simply output nothing if the spelling was wrong?
    Also i have stepped through the program. There is 8 jpg files and 1 desktop.ini file. The if statement does compare the extensions successfully. however the client program freezes unless i remove the
    Java Code:
    if (ext.equals("jpg") )
    from my server thread.

    When the program gets to the end of my run() method it then starts a thread called Thread.java[r/o]. If i am looking at my variables as well it gives me an error saying "Variable information not available, source compiled without -g option]"

    Not sure if any of that is of any help. Hope that all makes sense

    many thanks

    gisler

  4. #4
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    I managed to solve the problem. The problem was because i was sending out more items than what the client was expecting to recieve.

    Thanks for the help hardwired.

Similar Threads

  1. Checking if file exists in a directory
    By achilles in forum New To Java
    Replies: 7
    Last Post: 08-10-2009, 03:43 AM
  2. reading an Html file and checking for urls
    By sudukrish in forum Advanced Java
    Replies: 1
    Last Post: 04-25-2009, 02:39 AM
  3. Declaring Custom File Extensions
    By Jesdisciple in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-07-2008, 11:44 PM
  4. Checking of file was modified on the server
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 08:18 PM
  5. How to compare file extensions?
    By Rubin in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •