2013-08-22 62 views
2

我有這種Java方法來上傳文件。我正在嘗試通過將該文件夾壓縮成zip文件並上載它來嘗試上傳文件夾的用戶。出於某種原因,在我的情況下,file.isDirectory()file.isFile()工作不正常。即使文件名不包含任何擴展名,file.isFile()返回true,isDirectory()返回false。另外directory.list()也是通過返回null來表現怪異的。將文件夾壓縮成ZipFile

可能是什麼問題?難道我做錯了什麼?

public File uploadFile(FileItem item, String filename, int ticket_id) throws IOException 
{ 
    FileOutputStream out = null; 
    InputStream fileContent = null; 
    File file = null; 

    try 
    { 
     //fullpath returns C://MyDocuments//zerafbe//Documents//apache-tomcat-7.0.29//webapps//attachments//t50\test 

     StringBuffer fullPath = new StringBuffer(); 
     fullPath.append(Attachment.attachments_path); 
     fullPath.append("t"); 
     fullPath.append(Integer.toString(ticket_id)); 
     fullPath.append(File.separator); 
     fullPath.append(filename); 

     System.out.println("filename " + filename); 

     file = new File(fullPath.toString()); 

     if (!file.exists()) 
     { 
      // if directory does not exist, create it 
      file.getParentFile().mkdirs(); 
     } 

     if (file.isFile()) 
     { 
      // if file is not a folder     
      out = new FileOutputStream(file); 
      fileContent = item.getInputStream(); 

      int read = 0; 
      final byte[] bytes = new byte[1024]; 

      // read all the file and write it to created file 
      while ((read = fileContent.read(bytes)) != -1) 
      { 
       out.write(bytes, 0, read); 
      } 
     } 
     else if (file.isDirectory()) 
     { 
      ZipFile appZip = new ZipFile(fullPath.toString()); 
      appZip.generateFileList(file); 
      appZip.zipIt(filename + ".zip"); 
     } 
    } 
    catch (FileNotFoundException e) 
    { 
     LogFile.logError("[FileUpload.uploadFile()] " + e.getMessage()); 
    } 
    catch (IOException e1) 
    { 
     LogFile.logError("[FileUpload.uploadFile()] " + e1.getMessage()); 
    } 
    finally 
    { 
     if (out != null) 
     { 
      out.close(); 
     } 

     if (fileContent != null) 
     { 
      fileContent.close(); 
     } 
    } 

    return file; 
} 

這是我使用zip文件類

public class ZipFile 
{ 
    List<String> fileList = null; 
    String source_folder = ""; 

    public ZipFile(String source_folder) 
    { 
     fileList = new ArrayList<String>(); 
     this.source_folder = source_folder; 
    } 

    public void zipIt(String zipFile) 
    { 
     byte[] buffer = new byte[1024]; 
     String source = ""; 

     try 
     { 
      try 
      {      
       source = source_folder.substring(source_folder.lastIndexOf("\\") + 1, source_folder.length()); 
      } 
      catch(Exception e) 
      { 
       source = source_folder; 
      } 

      FileOutputStream fos = new FileOutputStream(zipFile); 
      ZipOutputStream zos = new ZipOutputStream(fos); 

      for (String file : this.fileList) 
      { 
       ZipEntry ze = new ZipEntry(source + File.separator + file); 
       zos.putNextEntry(ze); 

       FileInputStream in = new FileInputStream(source_folder + File.separator + file); 

       int len; 
       while ((len = in.read(buffer)) > 0) 
       { 
        zos.write(buffer, 0, len); 
       } 

       in.close(); 
      } 

      zos.closeEntry(); 
      //remember close it 
      zos.close(); 
     } 
     catch(IOException ex) 
     { 
      ex.printStackTrace(); 
     } 
    } 

    public void generateFileList(File node) 
    { 
     // add file only 
     if(node.isFile()) 
     {   
      fileList.add(generateZipEntry(node.toString())); 
     } 

     if(node.isDirectory()) 
     {   
      String[] subNode = node.list(); 

      if (subNode != null) { 

       for(String filename : subNode) 
       { 
        generateFileList(new File (node, filename)); 
       } 
      } 
     } 
    } 

    private String generateZipEntry(String path) 
    { 
     return path.substring(source_folder.length() + 1, path.length()); 
    } 
} 

file.list()generateFileList方法正在做的ZipFile類。我知道這是返回null,因爲我試圖通過使用filename.indexOf(".")而不是isDirectory()isFile()來檢測文件是文件夾還是文件,因爲它們不起作用。但我希望我對此有一個解釋。

感謝您的幫助!

+0

什麼你想達到什麼目的?您首先創建父目錄並測試存在,然後使用數據。這很奇怪。它在運行之前是否存在? –

+0

我測試了我想要上傳文件的文件夾的存在。我正在嘗試處理上傳文件是文件夾的情況 – Bernice

+0

誰將創建此上傳目錄?如果沒有人,那麼Java將創建常規文件而不是目錄。 –

回答

1
if (!file.exists()) { 
    // if directory does not exist, create it 
    file.mkdirs(); 
} 

將創建目錄和測試file.isDirectory()將返回true

0

這可能是一個路徑問題?

C://MyDocuments//zerafbe//Documents//apache-tomcat-7.0.29//webapps//attachments//t50\test 

你是混合斜槓反斜槓...

+0

剛剛注意到,並改變了路徑爲所有\但仍然是相同的問題:/ – Bernice

+0

路徑現在是:C:\ MyDocuments \ zerafbe \ Documents \ apache-tomcat-7.0.29 \ webapps \ attachments \ t50 \ test,顯然我修改ZipFile爲source = source_folder.substring(source_folder.lastIndexOf(File.separator)+ 1,source_folder.length()); – Bernice

0

我測試你的代碼塊

ZipFile appZip = new ZipFile(file.toString()); 
appZip.generateFileList(file); 
appZip.zipIt(filename + ".zip"); 

與本地文件夾,它的工作完美。我想你正在傳遞一條無效的路徑。這可能是原因isFileisDirectory方法行爲奇怪。嘗試在使用文件 API generateFileList方法的開始添加一個驗證聲明:

if(!node.exists) { 
    // return some flag to signify error OR throw a suitable Exception 
} 

這應該工作。

+0

謝謝blackSmith。你證實我的問題是@Leos Literak說的。當我使用該文件夾創建目錄時,java會爲我創建一個文件,因此isDirectory()將始終返回false – Bernice

0
public String compressData(String srcDir) { 

     String zipFile = srcDir+".zip"; 
       try { 

        // create byte buffer 
        byte[] buffer = new byte[1024]; 

        FileOutputStream fos = new FileOutputStream(zipFile); 

        ZipOutputStream zos = new ZipOutputStream(fos); 

        File dir = new File(srcDir); 

        File[] files = dir.listFiles(); 

        for (int i = 0; i < files.length; i++) { 

         System.out.println("Adding file: " + files[i].getName()); 

         FileInputStream fis = new FileInputStream(files[i]); 

         // begin writing a new ZIP entry, positions the stream to the start of the entry data 
         zos.putNextEntry(new ZipEntry(files[i].getName())); 

         int length; 

         while ((length = fis.read(buffer)) > 0) { 
          zos.write(buffer, 0, length); 
         } 

        zos.closeEntry(); 

         // close the InputStream 
         fis.close(); 
        } 


    // close the ZipOutputStream 
        zos.close(); 

       } 
       catch (IOException ioe) { 
        System.out.println("Error creating zip file" + ioe); 
       } 
       return zipFile; 

      } 
相關問題