前几天因为项目需要 做了一个导出 是在Oracle中blob字段导出数据生成word 然后再下载到客户端
blob为一个特殊字段 可以保存4G的数据 包括图片、视频等
/**
* @功能信息 :导出Word文档
* @参数信息 :outfile:文件路径,id:文档编号
* @返回值信息 :
* @异常信息 :
*/
public boolean readDoc(String outfile, String id) throws Exception {
DbaObj = new DBstep.iDBManager2000(); // 创建数据库对象
FileOutputStream fos = null;
InputStream is = null;
boolean mResult = false;
String Sql = "SELECT FileBody,FileSize FROM info_document_file WHERE RecordID='"
+ id + "'";
try {
if (DbaObj.OpenConnection()) {
try {
ResultSet result = DbaObj.ExecuteQuery(Sql);
if (result.next()) {
try {
int fileSize = result.getInt("FileSize");
Blob blob = result.getBlob("FileBody");
file = new File(outfile);
if (!file.exists()) {
file.createNewFile();// 如果文件不存在,则创建
}
fos = new FileOutputStream(file);
is = blob.getBinaryStream();// 读出数据后转换为二进制流
byte[] data = new byte[fileSize];
int size = 0;
while ((size = is.read(data)) != -1) {
fos.write(data, 0, size);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
result.close();
mResult = true;
} catch (SQLException e) {
e.printStackTrace();
mResult = false;
}
}
} finally {
DbaObj.CloseConnection();
fos.close();
is.close();
}
return (mResult);
}
注:因为用到了第三方控件 所以这块未使用Hibernate 以后有时间 可以进行修改
/**
* 导出功能
*
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
public ActionForward export(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
List list = new ArrayList();
// 生成随机文件名
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyymmddhhmmss");
String result = format.format(new java.util.Date());
ReportForm report = (ReportForm) form;
String[] exportlist = report.getIds();
//String str = "导出失败";
String path = request.getRealPath("/") + "upload/";
for (int i = 0; i < exportlist.length; i++) {
if (readDoc(path + exportlist[i] + ".doc", exportlist[i])) {
//str = "导出成功";
list.add(exportlist[i] + ".doc");
// download("http://localhost:8080/infoOA/upload/"+exportlist[i]+".doc",exportlist[i]+".doc");
}
}
// 先生成zip为文件 再删除doc文件
getZip(list, path, result);
for (int i = 0; i < exportlist.length; i++) {
deleteFile(new File(path+exportlist[i] + ".doc"));
}
downLoad(result, path, response);
//删除zip文件
deleteFile(new File(path+result+ ".zip"));
//request.setAttribute("message", str);
return null;
}
/**
* 压缩文件zip
* @功能信息 :
* @参数信息 :
* @返回值信息 :
* @异常信息 :
*/
public void getZip(List list,String path,String fileName) throws Exception{
byte[] buffer = new byte[1024];
String strZipName = fileName + ".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(path
+ strZipName));
for (int j = 0; j < list.size(); j++) {
String name = list.get(j).toString();
FileInputStream fis = new FileInputStream(path + name);
out.putNextEntry(new ZipEntry(name));
int len;
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.closeEntry();
fis.close();
}
out.close();
System.out.println("生成Demo.zip成功");
}
/**
* 文件下载
*
* @功能信息 :
* @参数信息 :
* @返回值信息 :
* @异常信息 :
*/
public void downLoad(String fileName,String path, HttpServletResponse response) throws IOException {
String strZipName = fileName + ".zip";
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream os = null;
InputStream is = null;
try {
File file = new File(path + strZipName);
if (!file.exists()) {
System.out.println("文件不存在");
}
is = new FileInputStream(file);
bis = new BufferedInputStream(is);
os = response.getOutputStream();
bos = new BufferedOutputStream(os);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("application/x-msdownload;charset=utf-8");
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode(strZipName, "utf-8"));
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
is.close();
bis.close();
os.close();
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文件信息 根据boolean类型判断文件是否存在
*
* @param file
*/
private boolean deleteFile(File file) {
if (file.exists()) {
if (file.isFile()) {
file.delete();
} else if (file.isDirectory()) {
File files[] = file.listFiles();
for (int i = 0; i < files.length; i++) {
this.deleteFile(files[i]);
}
}
file.delete();
return true;
} else {
return false;
}
}
分享到:
相关推荐
主要为大家详细介绍了java后台批量下载文件并压缩成zip下载的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本例是把多个文件压缩成一个zip文件,然后下载到本地。 项目完整,下载解压即可运行。
Java把文件压缩成zip,粘贴在项目中即可使用
用java实现文件/文件夹zip格式的压缩
zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java
JAVA文件压缩与解压缩实践,java解压缩zip文件,Java源码
java压缩文件成zip实现无乱码 内置类与jar包
应用java来压缩需要打包的文件,在系统管理中将毛哥路径或者某个文件夹压缩成zip包
java解压zip压缩文件,读取压缩文件中的内容并输出到输出文件中。
java实现压缩和压缩zip格式文件的功能,网络上看到的,觉得不错上传出来,供大家一起共同分享学习。
java 操作Zip文件(压缩、解压、加密) zip4j-1.3.2.jar ant-1.10.6.jar
java 解压缩zip文件 java 解压缩zip文件 java 解压缩zip文件
java 生成zip压缩文件 java 生成zip压缩文件
super("用ZIP压缩多个文件"); //调用父类构造函数 fileChooser=new JFileChooser(); //实例化文件选择器 files=new Vector(); //实例化文件数据Vector fileList=new JList(files); //实例化已选择文件...
这段Spring Boot代码展示了如何实现文件的批量下载与压缩。主要功能包括通过HTTP响应将多个文件打包成zip压缩包,并提供给用户一次性下载。以下是代码的主要功能点: 1.文件模拟数据:使用Map模拟了一组文件,其中...
Java实现的带密码压缩与解压zip文件源码,可将多个文件使用不同的密码压进同一个zip文件;可以解压带密码的zip文件(store压缩模式)
zip,然后下载删除源文件与压缩文件。 3、可以指定文件都为最后生成为zip文件下载,且删除源文件。 1、压缩文件或者目录(只是压缩不删除):zipFileOrDirectory 2、程序自动判断如果是文件那么就是源文件下载,...
包含了使用的jar包,以及一个Java类,实现了使用Java对多目录多层级的文件进行打包,以及对ZIP包进行解压缩的操作。
基于GUI用IO流中的ZipOutputStream ,ZipInputStream实现文件的解压与压缩, 如文件解压函数如下 private void unZip(String zipFileName, String outputDirectory)throws Exception { InputStream in=null; ...
java的zip压缩图片和文件。并且可以直接压缩整个文件夹。及解压功能