some questions(continue)

bonmot 2003-08-22 04:22:52
2.is there simple way to zip a dir and its recursive files
3.how to download a file in jsp page

I try:

response.setContentType("application/msexcel");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition","attachment; filename=courseware.xls");
os.write(bytes);
os.close();

but can't effect


...全文
28 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbhyk 2003-08-25
  • 打赏
  • 举报
回复
3.

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileZip
{
static final char SEPARATOR = File.separatorChar;
private int preffixLen = 0;

public void compress(OutputStream out, File file) throws IOException
{
preffixLen = file.getParentFile().getAbsolutePath().length() + 1;
ZipOutputStream zos = new ZipOutputStream(out);
addFile(zos, file);
zos.close();
}

private void addFile(ZipOutputStream zos, File file) throws IOException
{
String entryName = entryName(file);
if (entryName.equals("") || entryName.equals("."))
return;

System.out.println("adding " + entryName);
ZipEntry entry = new ZipEntry(entryName);
entry.setTime(file.lastModified());
boolean isDirectory = file.isDirectory();
if (isDirectory)
{
entry.setMethod(0);
entry.setSize(0L);
entry.setCrc(0L);
}
else
entry.setSize(file.length());

zos.putNextEntry(entry);
if (isDirectory)
{
File[] files = file.listFiles();
for(int i=0; i<files.length; i++)
addFile(zos, files[i]);
}
else
{
byte buf[] = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int bytes;
while ((bytes = bis.read(buf, 0, buf.length)) != -1)
zos.write(buf, 0, bytes);
bis.close();
}
zos.closeEntry();
}

private String entryName(File file)
{
String entryName = file.getPath();
if (file.isDirectory())
entryName = entryName.endsWith(File.separator) ? entryName : entryName + File.separator;
entryName = entryName.replace(File.separatorChar, '/').substring(preffixLen);
return entryName;
}

public static void main(String args[])
{
try
{
FileOutputStream fos = new FileOutputStream("test.zip");
FileZip zip = new FileZip();
zip.compress(new BufferedOutputStream(fos), new File("d:\\test\\jsp")); //compress folder d:\test\jsp
fos.close();
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
bonmot 2003-08-25
  • 打赏
  • 举报
回复
up
bonmot 2003-08-25
  • 打赏
  • 举报
回复
正确。我再来加一段从JSP download 一个目录中的所有文件的代码。已经过测试

<%!
public void doZipDirectory(File d, ZipOutputStream out,String rootDir)throws IOException, IllegalArgumentException {
// Check that the directory is a directory, and get its contents
if (!d.isDirectory())throw new IllegalArgumentException("Compress: not a directory: " +d);
String[] entries = d.list();
byte[] buffer = new byte[4096]; // Create a buffer for copying
int bytes_read;

// Loop through all entries in the directory
for(int i = 0; i < entries.length; i++) {
File f = new File(d, entries[i]);
//System.out.println(entries[i]);
if (f.isDirectory()) {
doZipDirectory(f, out,rootDir);
}
else
{
FileInputStream in = new FileInputStream(f); // Stream to read file
String path=f.getPath();
String relevantPath=path.substring(path.indexOf(rootDir)+rootDir.length());
ZipEntry entry = new ZipEntry(relevantPath); // Make a ZipEntry
out.putNextEntry(entry); // Store entry
while((bytes_read = in.read(buffer)) != -1) // Copy bytes
out.write(buffer, 0, bytes_read);
in.close(); // Close input stream
}
}
}

%>
<%
try{
//getParameter
String courseware_ID=PatchUtil.nvl(request.getParameter("courseware_ID"));
String coursewareName=getCoursewareName(courseware_ID);

//find dir
java.io.File file=new java.io.File("");
String separator=java.io.File.separator;
String rootDir=file.getAbsolutePath()+separator+"config"+separator+"nebo"+
separator+"applications"+separator+"NEBOWEBAPP"+separator+"resource"+separator;
String strCourseDir=rootDir+"Course_"+coursewareName+separator;
File courseDir=new java.io.File(strCourseDir);
if(courseDir.exists() && courseDir.isDirectory());
else
courseDir.mkdirs();

//generate zip file
//download
response.reset();
response.setContentType("application/msdownload");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition","attachment; filename=courseware.zip");

ZipOutputStream zos=new ZipOutputStream(os);
doZipDirectory(new File(strCourseDir),zos,rootDir);

zos.close();
}
catch(Exception e){
e.printStackTrace();
//throw new Exception(e);
}
%>
yz_ivan 2003-08-22
  • 打赏
  • 举报
回复
帮你UP一下
bonmot 2003-08-22
  • 打赏
  • 举报
回复
自己up
bonmot 2003-08-22
  • 打赏
  • 举报
回复
download is okay now
now only left :
ow to easily zip a directory
cbhyk 2003-08-22
  • 打赏
  • 举报
回复
response.setContentType("application/msdownload");
bonmot 2003-08-22
  • 打赏
  • 举报
回复
自己up
cbhyk 2003-08-22
  • 打赏
  • 举报
回复
3. download.jsp:
<%
response.reset();
response.setContentType("application/msexcel");
OutputStream os = response.getOutputStream();
response.setHeader("Content-disposition","attachment; filename=courseware.xls");
byte[] bytes = ...
os.write(bytes);
os.close();
%>
newchina 2003-08-22
  • 打赏
  • 举报
回复
up
bonmot 2003-08-22
  • 打赏
  • 举报
回复
同样up有分

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧