【发布时间】:2016-12-18 01:56:14
【问题描述】:
我有一个 JSP 应用程序,它允许用户上传 ZIP 文件,然后该应用程序将读取 ZIP 中的所有文件并将它们存储在 MySQL 中。
根据建议,我决定使用“Zip File System Provider”来处理 ZIP 文件:
Path zipPath = Paths.get(zipFile.getSubmittedFileName());//returns the path to the ZIP file
FileSystem fs = FileSystems.newFileSystem(zipPath, null);//creates the file system
我尝试使用以下方法遍历它:
for (FileStore store: fs.getFileStores()) {
System.err.println("Store: " + store.name());
}
但是它只循环一次并返回tmp.zip这是整个ZIP。如何将物理图像文件一个一个提取出来,以便将它们存储在 MySQL 中。
【问题讨论】:
-
从
FileSystem的根目录开始,而不是文件存储:for (Path rootDir : fs.getRootDirectories) -
它返回空值。自己怎么访问文件,已经可以拿到目录了,只有用户可以上传的一个。
-
只需使用
Files.walk- 大约是 1 行代码。您正在遍历底层的 store,这当然是 zip。您需要查看文件系统。以fs.getPath("/")为例。 -
什么返回
null;fs.getRootDirectories()是否返回null?那真的很奇怪。 -
正如@Jesper 所说,
FileSystem.getRootDirectories()不能返回null。
标签: java jsp filesystems zip