在“ZipFile zipfile = new ZipFile(“X”);”中设置路径 X
我在 ZipFile zipfile = new ZipFile("X");
中设置 zip 文件 X 的路径时遇到问题。
我不想对路径进行硬编码,使其变为 ZipFile zipfile = new ZipFile("C:/docs/data.zip");
。
我想做类似的事情:
ZipFile zipfile = new ZipFile(getServletContext().getResourceAsStream("/WEB-INF/" + request.getAttribute("myFile").toString());
其中 zip 文件的路径由用户的选择确定。但是,这会产生错误,因为这只适用于 InputStream。
之前,我已经检索了多部分/表单数据并获取了 zip 文件的真实路径:
String path = getServletContext().getRealPath("/WEB-INF");
UploadBean bean = new UploadBean();
bean.setFolderstore(path);
MultipartFormDataRequest multiPartRequest = new MultipartFormDataRequest(request);
bean.store(multiPartRequest); //store in WEB-INF
// get real path / name of zip file which is store in the WEB-INF
Hashtable files = multiPartRequest.getFiles();
UploadFile upFile = (UploadFile) files.get("file");
if (upFile != null) request.setAttribute("myFile", upFile.getFileName());
对此有解决方案吗?
I'm having problems setting the path of the zip file, X, in ZipFile zipfile = new ZipFile("X");
.
I don't want to hardcode the path such that it becomes ZipFile zipfile = new ZipFile("C:/docs/data.zip");
.
I want to do something like :
ZipFile zipfile = new ZipFile(getServletContext().getResourceAsStream("/WEB-INF/" + request.getAttribute("myFile").toString());
Where the path of the zip file is determined by the selection of the user. But, this gives an error, because this only works for InputStream.
Previously, I've already retrieved the multipart/form data and gotten the real path of the zip file:
String path = getServletContext().getRealPath("/WEB-INF");
UploadBean bean = new UploadBean();
bean.setFolderstore(path);
MultipartFormDataRequest multiPartRequest = new MultipartFormDataRequest(request);
bean.store(multiPartRequest); //store in WEB-INF
// get real path / name of zip file which is store in the WEB-INF
Hashtable files = multiPartRequest.getFiles();
UploadFile upFile = (UploadFile) files.get("file");
if (upFile != null) request.setAttribute("myFile", upFile.getFileName());
Any solutions to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过两种方式将 webcontent 相对路径转换为绝对磁盘文件系统路径:
只需使用
ServletContext#getRealPath()
正如您之前所做的那样。使用<改为代码>ServletContext#getResource()。它返回一个
URL
。对其调用getPath()
。方式 #1 是首选。
You can convert webcontent-relative paths to absolute disk file system paths in two ways:
Just use
ServletContext#getRealPath()
as you previously already did.Use
ServletContext#getResource()
instead. It returns anURL
. CallgetPath()
on it.Way #1 is preferred.
我不明白你为什么不使用已有的真实路径。
无论如何,您可以使用
ZipInputStream
。这样您就可以将文件作为简单的 Stream 来处理。唯一较大的区别是您无法直接访问的
getName()
方法和size()
。有了 ZIS,您将能够阅读每个条目。资源:
I don't understand why you don't use the real path that you already have.
Anyway, you can work with a
ZipInputStream
.This way you can handle your file as a simple Stream. The only big differences are the
getName()
method andsize()
that you can't directly access. With a ZIS you will be able to read every entry.Resources :