0

I have an application that uploads a file. I need to pass this file into another program but for that I need the file name only. Is there any simple code for that, using only Java or a servlet procedure?

while (files.hasMoreElements()) 
{ 
      name = (String)files.nextElement();
      type = multipartRequest.getContentType(name); 
      filename = multipartRequest.getFilesystemName(name); 
      originalFilename = multipartRequest.getOriginalFileName(name);  

      //extract the file extension - this can be use to reject a 
      //undesired file extension                      
      extension1 = filename.substring
            (filename.length() - 4, filename.length());
      extension2 = originalFilename.substring
            (originalFilename.length() - 4, originalFilename.length());

      //return a File object for the specified uploaded file
      File currentFile = multipartRequest.getFile(name);
      //InputStream inputStream = new BufferedInputStream
             (new FileInputStream(currentFile));
      if(currentFile == null) {
            out.println("There is no file selected!");
            return;
      }
Blue Ice
  • 7,888
  • 6
  • 32
  • 52

1 Answers1

0

There's a method in apache commons-io to get the file's extension. There's also guava Files class, with its getFileExtension method.

Community
  • 1
  • 1
hd1
  • 33,938
  • 5
  • 80
  • 91