在ZipFile zipfile = new ZipFile("X");
中设置压缩文件X的路径时遇到问题。
我不想对路径进行硬编码,使其成为ZipFile zipfile = new ZipFile("C:/docs/data.zip");
。
我想做一些类似的事情:
ZipFile zipfile = new ZipFile(getServletContext().getResourceAsStream("/WEB-INF/" + request.getAttribute("myFile").toString());
其中zip文件的路径由用户的选择来确定。但是,这会给出一个错误,因为这只适用于InputStream。
在此之前,我已经检索到了multipart/form数据并获得了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());
有什么解决方案吗?
发布于 2010-09-30 01:59:05
我不明白你为什么不使用你已经拥有的真实路径。
无论如何,您可以使用ZipInputStream
。
这样你就可以把你的文件当作一个简单的流来处理了。唯一最大的区别是您不能直接访问的getName()
方法和size()
。有了ZIS,你就能读懂每一个条目。
资源:
发布于 2010-09-30 02:07:57
您可以通过两种方式将webcontent的相对路径转换为磁盘文件系统的绝对路径:
ServletContext#getRealPath()
即可。ZipFile = zipfile ZipFile(getServletContext().getRealPath("/WEB-INF/“+ request.getAttribute("myFile").toString()));
ServletContext#getResource()
。它返回一个URL
。在上面调用getPath()
。新ZipFile = zipfile ZipFile(getServletContext().getResource("/WEB-INF/“+ request.getAttribute("myFile").toString()).getPath());
方法#1是首选。
https://stackoverflow.com/questions/3824443
复制相似问题