2013-08-22 61 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; 

      } 
相关问题