使用Java和JSP将PDF文件输出为Zip文件
我试图通过点击一个按钮将多个PDF文件输出到一个Zip文件中。通过单击“下载规格表Zip文件”,专门输出所有规格表,每个规格表由一个PDF文件组成。附件是一张更好地说明这一点的图片 现在,我的代码被设置为将所有规范表输出到我的本地下载文件夹,作为一种测试(正在工作)。我需要修改它,以便将规格表输出到单个Zip文件中 这是我的密码:使用Java和JSP将PDF文件输出为Zip文件,java,file-io,zip,zipfile,fileoutputstream,Java,File Io,Zip,Zipfile,Fileoutputstream,我试图通过点击一个按钮将多个PDF文件输出到一个Zip文件中。通过单击“下载规格表Zip文件”,专门输出所有规格表,每个规格表由一个PDF文件组成。附件是一张更好地说明这一点的图片 现在,我的代码被设置为将所有规范表输出到我的本地下载文件夹,作为一种测试(正在工作)。我需要修改它,以便将规格表输出到单个Zip文件中 这是我的密码: public void addToZipFile(String fileUrl, ZipOutputStream zos, eclUser user) th
public void addToZipFile(String fileUrl, ZipOutputStream zos, eclUser user) throws FileNotFoundException, IOException {
URL u = new URL(fileUrl);
URLConnection uc = u.openConnection();
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
if (contentType.startsWith("text/") || contentLength == -1) {
throw new IOException("This is not a binary file.");
}
InputStream raw = uc.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[contentLength];
int bytesRead = 0;
int offset = 0;
while (offset < contentLength) {
bytesRead = in.read(data, offset, data.length - offset);
if (bytesRead == -1)
break;
offset += bytesRead;
}
in.close();
if (offset != contentLength) {
throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
}
String fileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
String outputPath = eclSystem.getUserDownloadDir(user) + eclSystem.getDirSep() + fileName;
String absPath = eclSystem.file2Absolute(outputPath);
FileOutputStream out = new FileOutputStream(absPath);
out.write(data);
out.flush();
out.close();
}
public void addToZipFile(字符串fileUrl,ZipOutputStream zos,eclUser user)抛出FileNotFoundException,IOException{
URL u=新URL(fileUrl);
URLConnection uc=u.openConnection();
字符串contentType=uc.getContentType();
int contentLength=uc.getContentLength();
if(contentType.startsWith(“text/”)| | contentLength==-1){
抛出新IOException(“这不是二进制文件”);
}
InputStream raw=uc.getInputStream();
InputStream in=新的BufferedInputStream(原始);
字节[]数据=新字节[contentLength];
int字节读取=0;
整数偏移=0;
while(偏移量
任何帮助都将不胜感激。提前谢谢 查看java.util.zip包()或ApacheCommons压缩,获取创建zip文件所需的java类