我有一个动态文本文件,根据用户的查询从数据库中选择内容.我必须将此内容写入文本文件并将其压缩到servlet中的文件夹中.我该怎么做?
我一直在测试所有可能的变体和排列,但我似乎无法使用zip/jar方案为包含空格的路径(URI)构建FileSystemProvider.Oracle Docs提供了一个非常简单的测试用例.我冒昧地修改了这个例子,只是在URI中添加空格,它就停止工作了.下面的代码段:
import java.util.*;
import java.net.URI;
import java.nio.file.*;
public class Test {
public static void main(String [] args) throws Throwable {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
URI uri = new URI("jar:file:/c:/dir%20with%20spaces/zipfstest.zip");
Path dir = Paths.get("C:\\dir with spaces");
if(Files.exists(dir) && Files.isDirectory(dir)) {
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行此代码(Windows,JDK7u2,x32和x64)时,我得到以下异常:
java.lang.IllegalArgumentException: Illegal character in path at index 12: file:/c:/dir with spaces/zipfstest.zip
at com.sun.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:87)
at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:107)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:322)
at java.nio.file.FileSystems.newFileSystem(FileSystems.java:272)
Run Code Online (Sandbox Code Playgroud)
如果我使用+而不是%20作为空格转义字符,则抛出另一个异常:
java.nio.file.NoSuchFileException: c:\dir+with+spaces\zipfstest.zip
at …Run Code Online (Sandbox Code Playgroud)