使用servlet在eclipse中获取上传文件名

时间:2014-02-16 04:49:27

标签: eclipse servlets

我有一个上传文件的应用程序。我需要将此文件传递到另一个程序,但为此我只需要文件名。有没有简单的代码,只使用Java或servlet程序?

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;
      }

1 个答案:

答案 0 :(得分:0)

有一个method in apache commons-io来获取文件的扩展名。还有guava Files类,带有getFileExtension方法。