下载的zip文件返回零,具有0字节的大小 [英] downloaded zip file returns zero has 0 bytes as size
问题描述
它的工作原理如下:
1.用户获得与他/她的标准相符的文件列表
2.如果用户喜欢文件,并希望下载他/她通过选中复选框
3.选择它。用户然后点击下载
4.文件然后压缩并存储在服务器上a
5.然后用户显示一个包含可下载zip文件的链接的页面a
6.但是下载zip文件下载的文件大小为0个字节
我已经检查了远程服务器,正在正确创建zip文件,剩下的就是以某种方式向用户提供文件,您可以看到我可能会出错,还是建议更好的方式来提供zip文件。
创建链接的代码是:
<%
String zipFileURL =(String)request.getAttribute(zipFileURL); %GT;
< p>< a href =<%out.print(zipFileURL);%>> Zip文件链接< / a>< / p>
创建zipFileURL变量的代码是:
public static String zipFiles(ArrayList< String> fileList,String contextRootPath){
// time-stamping
Date date = new Date();
Timestamp timeStamp = new Timestamp(date.getTime());
迭代器fileListIterator = fileList.iterator();
String zipFileURL =;
try {
String ZIP_LOC = contextRootPath +WEB-INF+ SEP +TempZipFiles+ SEP;
BufferedInputStream origin = null;
zipFileURL = ZIP_LOC
+FITS。 + timeStamp.toString()。replaceAll(:,。)。replaceAll(,。)+.zip;
FileOutputStream dest = new FileOutputStream(ZIP_LOC
+FITS。+ timeStamp.toString()。replaceAll(:,。)。replaceAll(,。)+。压缩);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
// out.setMethod(ZipOutputStream.DEFLATED);
byte data [] = new byte [BUFFER];
while(fileListIterator.hasNext()){
String fileName =(String)fileListIterator.next();
System.out.println(Adding:+ fileName);
FileInputStream fi = new FileInputStream(fileName);
origin = new BufferedInputStream(fi,BUFFER);
ZipEntry entry = new ZipEntry(fileName);
out.putNextEntry(entry);
int count;
while((count = origin.read(data,0,BUFFER))!= -1){
out.write(data,0,count);
}
origin.close();
}
out.close();
} catch(Exception e){
e.printStackTrace();
}
返回zipFileURL;
}
URL无法访问任何文件(直接)在WEB-INF下。我建议使用servlet将文件从任何保存的位置返回到
还建议将文件保存在您的webapp上下文之外
I have written a Java web application that allows a user to download files from a server. These files are quite large and so are zipped together before download.
It works like this:
1. The user gets a list of files that match his/her criteria
2. If the user likes a file and wants to download he/she selects it by checking a checkbox
3. The user then clicks "download"
4. The files are then zipped and stored on a servera
5. The user this then presented with a page which contains a link to the downloadable zip filea
6. However on downloading the zip file the file that is downloaded is 0 bytes in sizea
I have checked the remote server and the zip file is being created properly, all that is left is to serve the file the user somehow, can you see where I might be going wrong, or suggest a better way to serve the zip file.
The code that creates the link is:
<%
String zipFileURL = (String) request.getAttribute("zipFileURL"); %>
<p><a href="<% out.print(zipFileURL); %> ">Zip File Link</a></p>
The code that creates the zipFileURL variable is:
public static String zipFiles(ArrayList<String> fileList, String contextRootPath) {
//time-stamping
Date date = new Date();
Timestamp timeStamp = new Timestamp(date.getTime());
Iterator fileListIterator = fileList.iterator();
String zipFileURL = "";
try {
String ZIP_LOC = contextRootPath + "WEB-INF" + SEP + "TempZipFiles" + SEP;
BufferedInputStream origin = null;
zipFileURL = ZIP_LOC
+ "FITS." + timeStamp.toString().replaceAll(":", ".").replaceAll(" ", ".") + ".zip";
FileOutputStream dest = new FileOutputStream(ZIP_LOC
+ "FITS." + timeStamp.toString().replaceAll(":", ".").replaceAll(" ", ".") + ".zip");
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
dest));
// out.setMethod(ZipOutputStream.DEFLATED);
byte data[] = new byte[BUFFER];
while(fileListIterator.hasNext()) {
String fileName = (String) fileListIterator.next();
System.out.println("Adding: " + fileName);
FileInputStream fi = new FileInputStream(fileName);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(fileName);
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
}
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return zipFileURL;
}
A URL cannot access any files (directly) under WEB-INF. I'd suggest using a servlet to return the file from whatever location it was saved to
Would also suggest saving the file outside the context of your webapp
这篇关于下载的zip文件返回零,具有0字节的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!