在“ZipFile zipfile = new ZipFile(“X”);”中设置路径 X

发布于 2024-09-25 07:04:05 字数 980 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_失温 2024-10-02 07:04:05

您可以通过两种方式将 webcontent 相对路径转换为绝对磁盘文件系统路径:

  1. 只需使用 ServletContext#getRealPath() 正如您之前所做的那样。

    ZipFile zipfile = new ZipFile(getServletContext().getRealPath("/WEB-INF/" + request.getAttribute("myFile").toString()));
    
  2. 使用<改为代码>ServletContext#getResource()。它返回一个URL。对其调用 getPath()

    ZipFile zipfile = new ZipFile(getServletContext().getResource("/WEB-INF/" + request.getAttribute("myFile").toString()).getPath());
    

方式 #1 是首选。

You can convert webcontent-relative paths to absolute disk file system paths in two ways:

  1. Just use ServletContext#getRealPath() as you previously already did.

    ZipFile zipfile = new ZipFile(getServletContext().getRealPath("/WEB-INF/" + request.getAttribute("myFile").toString()));
    
  2. Use ServletContext#getResource() instead. It returns an URL. Call getPath() on it.

    ZipFile zipfile = new ZipFile(getServletContext().getResource("/WEB-INF/" + request.getAttribute("myFile").toString()).getPath());
    

Way #1 is preferred.

山有枢 2024-10-02 07:04:05

我不明白你为什么不使用已有的真实路径。

无论如何,您可以使用 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 and size() that you can't directly access. With a ZIS you will be able to read every entry.


Resources :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文