사용자가 서버에서 파일을 다운로드 할 수있는 Java 웹 응용 프로그램을 작성했습니다. 이 파일들은 꽤 크고 따라서 다운로드 전에 함께 압축됩니다.다운로드 된 zip 파일의 0으로 반환되는 크기는 0 바이트입니다.
그것은 다음과 같이 작동 : 사용자는 그/그녀의 기준에 일치하는 파일의 목록을 가져옵니다
1.
사용자가 그녀가 체크 박스
를 확인하여 선택/파일을 좋아하고 그가를 다운로드하고자 할 경우 (2) 3. 사용자는
4. 파일을 다음 압축하고 ServerA가
5.이가 다음
6. 그러나 다운로드 우편 FILEA에 대한 링크가 포함 된 페이지가 표시 사용자에 저장되어있는 "다운로드"클릭 zip 파일을 다운로드 할 때 다운로드되는 파일의 크기는 0 바이트입니다.
원격 서버를 검사했는데 zip 파일이 제대로 생성되고 있습니다. 남은 것은 사용자에게 어떻게 든 파일을 제공하는 것입니다. 내가 잘못 될 수있는 곳을 볼 수 있습니까? 아니면 zip 파일을 제공하는 더 좋은 방법을 제안 할 수 있습니까? .
링크를 생성하는 코드는 다음과 같습니다
는<%
String zipFileURL = (String) request.getAttribute("zipFileURL"); %>
<p><a href="<% out.print(zipFileURL); %> ">Zip File Link</a></p>
zipFileURL 변수를 생성하는 코드는 다음과 같습니다
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;
}
내가에 서블릿을 사용하는 방법의 코드를 부탁드립니다 파일을 반환하십시오. 감사합니다 – Terman
여기에 예 : http://helpdesk.objects.com.au/java/how-can-i-return-an-image-using-a-servlet – objects