- 浏览: 139909 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
ling凌yue月:
Yes,it's good!
JavaScript实现ReplaceAll 方法 -
lj1214388:
请问楼主,我导入了struts-jquery-plugin 的 ...
Jquery Ui 日期控件
StringBuffer sb = new StringBuffer();
//生成StringBuffer
sb = createStringBuffer();
//导出到Word
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
FileExport fe = new FileExport(String.valueOf(System.currentTimeMillis()), FileConstants.FILE_EXT_NAME_DOC);
fe.exportFile(response, is);
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import com.idea.webframe.comm.log.LogWritter;
/**
* Title: 系统组件 文件导出
* Description: 中国税务税收征管信息系统
* Copyright: Copyright (c) 2006
* Company: 中国软件与技术服务股份有限公司
* @author 康水明
* @version 1.0
*/
public class FileExport {
/**
* 默认构造器
* @param fileName文件名
* @param contentTypeName 文件类型
*/
public FileExport(String fileName, String contentTypeName) {
initContentTypeMap();
this.contentTypeName = contentTypeName;
this.contentType = (String) contentTypeMap.get(contentTypeName);
this.fileName = fileName;
}
/**
* 私有构造器
*/
private FileExport() {
}
//----------------------------公共接口----------------------------------------
//对外提供的方法1 byte[]
public void exportFile(HttpServletResponse response, byte[] bytes) throws IOException {
if (!isZip && contentTypeName.equals(FileConstants.FILE_EXT_NAME_ZIP)) {
LogWritter.sysDebug("执行doZippedExport(response, bytes, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doZippedExport(response, bytes, encodeType);
} else {
LogWritter.sysDebug("执行doExport(response, bytes, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doExport(response, bytes, encodeType);
}
}
//对外提供的方法2 InputStream
public void exportFile(HttpServletResponse response, InputStream in) throws IOException {
if (!isZip && contentTypeName.equals(FileConstants.FILE_EXT_NAME_ZIP)) {
LogWritter.sysDebug("执行doZippedExport(response, in, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doZippedExport(response, in, encodeType);
} else {
LogWritter.sysDebug("执行doExport(response, in, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doExport(response, in, encodeType);
}
}
//对外提供的方法3 File 如果file是目录的话,需使用zip方式
public void exportFile(HttpServletResponse response, File file) throws IOException {
if (!isZip && contentTypeName.equals(FileConstants.FILE_EXT_NAME_ZIP)) {
LogWritter.sysDebug("执行doZippedExport(response, file, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doZipExport(response, file, encodeType);
} else {
LogWritter.sysDebug("将file:" + file.getName() + " 转换成InputStream。。。。");
FileInputStream in = new FileInputStream(file);
LogWritter.sysDebug("执行doExport(response, in, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doExport(response, in, encodeType);
}
}
//对外提供的方法4 List files 此种情况只支持输出为zip
public void exportFile(HttpServletResponse response, List files) throws IOException {
//先将头信息设置为zip类型的
this.contentTypeName = FileConstants.FILE_EXT_NAME_ZIP;
this.contentType = (String) contentTypeMap.get(contentTypeName);
LogWritter.sysDebug("执行 doExport4FileList(response, files, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doExport4FileList(response, files, encodeType);
}
//对外提供的方法4 List files 此种情况只支持输出为zip
public void exportFile(HttpServletResponse response, List bytes, List fileNames) throws IOException {
//先将头信息设置为zip类型的
this.contentTypeName = FileConstants.FILE_EXT_NAME_ZIP;
this.contentType = (String) contentTypeMap.get(contentTypeName);
LogWritter.sysDebug("执行 doExport4FileList(response, files, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doExport4ByteList(response, bytes,fileNames, encodeType);
}
//对外提供的方法5 String path
public void exportFile(HttpServletResponse response, String path) throws IOException {
if (!isZip && contentTypeName.equals(FileConstants.FILE_EXT_NAME_ZIP)) {
FileInputStream in = new FileInputStream(path);
doZippedExport(response, in, encodeType);
} else {
LogWritter.sysDebug("获得路径" + path + "上文件的InputStream.......");
FileInputStream in = new FileInputStream(path);
LogWritter.sysDebug("执行doExport(response, in, encodeType)方法,参数为fileName:" + fileName +
"; contentTypeName:" + contentTypeName + "; contentType:" + contentType);
doExport(response, in, encodeType);
}
}
//------------------------------私有方法---------------------------------
/**
* 初始化头类型Map
*/
private void initContentTypeMap(){
if (contentTypeMap.isEmpty()) {
contentTypeMap.put(FileConstants.FILE_EXT_NAME_CSV, FileConstants.CSV_CONTENT_TYPE);
contentTypeMap.put(FileConstants.FILE_EXT_NAME_ZIP, FileConstants.ZIP_CONTENT_TYPE);
contentTypeMap.put(FileConstants.FILE_EXT_NAME_XML, FileConstants.XML_CONTENT_TYPE);
contentTypeMap.put(FileConstants.FILE_EXT_NAME_TXT, FileConstants.TXT_CONTENT_TYPE);
contentTypeMap.put(FileConstants.FILE_EXT_NAME_DAT, FileConstants.DAT_CONTENT_TYPE);
contentTypeMap.put(FileConstants.FILE_EXT_NAME_XLS, FileConstants.XLS_CONTENT_TYPE);
}
}
//支持参数是file list 的export方法
private void doExport4FileList(HttpServletResponse response,
List files, String encoding) throws IOException {
//Set http response header
response.reset();
response.setContentType(contentType + " charset=" + encoding);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + contentTypeName);
//Zip and export.
ZipOutputStream os = null;
try {
//取得zip输出流
os = new ZipOutputStream(response.getOutputStream());
//循环处理每一个file,将其放入zip中
int i = 0;
File theFile;
for (i = 0; i < files.size(); i++) {
theFile = (File) files.get(i);
zip(os, theFile, theFile.getName());
}
os.flush();
}
catch (Exception e) {
LogWritter.sysError("将fileList导出为zip文件时出现错误。 ", e);
e.printStackTrace();
}
finally {
closeStream(os);
}
}
//支持参数是byte list 的export方法
private void doExport4ByteList(HttpServletResponse response,
List bytes, List fileNames, String encoding) throws IOException {
//Set http response header
response.reset();
response.setContentType(contentType + " charset=" + encoding);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + contentTypeName);
//Zip and export.
ZipOutputStream os = null;
try {
//取得zip输出流
os = new ZipOutputStream(response.getOutputStream());
//循环处理每一个file,将其放入zip中
int i = 0;
int size = bytes.size();
byte[] b;
String fileName;
for (i = 0; i < size; i++) {
b = (byte[]) bytes.get(i);
fileName = (String)fileNames.get(i);
zip(os, b, fileName);
}
os.flush();
}
catch (Exception e) {
LogWritter.sysError("将fileList导出为zip文件时出现错误。 ", e);
e.printStackTrace();
}
finally {
closeStream(os);
}
}
/**
* Export comma-separated-values file
* @param response HttpServletResponse
* @param bytes byte[] Bytes from csv formatted string
* @param encoding String Encoding for http content type.
* @throws IOException
*/
private void doExport(HttpServletResponse response, byte[] bytes,
String encoding) throws IOException {
//Set http response header
response.reset();
response.setContentType(contentType + " charset=" + encoding);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + contentTypeName);
//Do export
BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(bytes));
BufferedOutputStream bo = null;
try {
bo = new BufferedOutputStream(response.getOutputStream());
byte[] buf = new byte[bufferSize];
int b;
while ((b = in.read(buf, 0, buf.length)) != -1) {
bo.write(buf, 0, b);
}
bo.flush();
} catch (IOException e) {
LogWritter.sysError("[doExport] Export csv file exception ", e);
throw e;
} finally {
closeStream(bo, in);
}
}
/**
* Export comma-separated-values file
*
* @param response HttpServletResponse
* @param bytes byte[] Bytes from csv formatted string
* @param encoding String Encoding for http content type.
* @throws IOException
*/
private void doExport(HttpServletResponse response, InputStream in,
String encoding) throws IOException {
//Set http response header
response.reset();
response.setContentType(contentType + " charset=" + encoding);
response.setHeader("Content-disposition", "attachment;filename=" + fileName + contentTypeName);
//Do export
BufferedInputStream bin = new BufferedInputStream(in);
BufferedOutputStream bo = null;
try {
bo = new BufferedOutputStream(response.getOutputStream());
byte[] buf = new byte[bufferSize];
int b;
while ((b = bin.read(buf, 0, buf.length)) != -1) {
bo.write(buf, 0, b);
}
bo.flush();
} catch (IOException e) {
LogWritter.sysError("[doExport] Export csv file exception ", e);
throw e;
} finally {
closeStream(bo, in);
}
}
/**
* Multi-file zip and export
* @param response HttpServletResponse
* @param bytesList List of several files in bytes form.
* @param encoding Encoding for http content type.
* @throws IOException
*/
private synchronized void doZippedExport(HttpServletResponse response,
InputStream in, String encoding) throws
IOException {
if (in == null) {
LogWritter.sysInfo("[doZippedExport] Nothing to be exported");
return;
}
//Set http response header
response.reset();
response.setContentType(FileConstants.ZIP_CONTENT_TYPE + " charset=" + encoding);
response.setHeader("Content-disposition", "filename=" + fileName + FileConstants.FILE_EXT_NAME_ZIP);
//Zip and export.
ZipOutputStream os = null;
BufferedInputStream is = null;
try {
os = new ZipOutputStream(response.getOutputStream());
LogWritter.sysInfo("fileName:" + fileName);
os.putNextEntry(new ZipEntry(fileName));
is = new BufferedInputStream(in);
int b = 0;
byte[] buf = new byte[bufferSize];
while ((b = is.read(buf, 0, buf.length)) != -1) {
os.write(buf, 0, b);
}
os.flush();
}
catch (Exception e) {
LogWritter.sysError("[doZippedExport] Exception ", e);
}
finally {
closeStream(os, is);
}
}
/**
* Multi-file zip and export
* @param response HttpServletResponse
* @param bytesList List of several files in bytes form.
* @param encoding Encoding for http content type.
* @throws IOException
*/
private synchronized void doZipExport(HttpServletResponse response,
File file, String encoding) throws
IOException {
//Set http response header
response.reset();
response.setContentType(FileConstants.ZIP_CONTENT_TYPE + " charset=" + encoding);
response.setHeader("Content-disposition", "filename=" + fileName + FileConstants.FILE_EXT_NAME_ZIP);
//Zip and export.
ZipOutputStream os = null;
try {
os = new ZipOutputStream(response.getOutputStream());
zip(os, file, file.getName());
os.flush();
}
catch (Exception e) {
LogWritter.sysError("[doZippedExport] Exception ", e);
e.printStackTrace();
}
finally {
closeStream(os);
}
}
/**
* zip 文件压缩
* @param out zip输出流
* @param f 文件名
* @param base 文件目录路径
* @throws Exception
*/
private void zip(ZipOutputStream out, File f, String base) throws Exception {
LogWritter.sysDebug("Zipping " + f.getName());
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new ZipEntry(base + FileConstants.FILE_DIR_SPLIT_DIAGONAL));
base = base.length() == 0 ? "" : base + FileConstants.FILE_DIR_SPLIT_DIAGONAL;
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[bufferSize];
int bb;
while ((bb = in.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, bb);
}
in.close();
}
}
/**
* zip 文件压缩
* @param out zip输出流
* @param b byte[]
* @param fileName 文件名
* @throws Exception
*/
private void zip(ZipOutputStream out, byte[] b, String fileName) throws Exception {
LogWritter.sysDebug("Zipping " + fileName);
out.putNextEntry(new ZipEntry(fileName));
BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(b));
byte[] buf = new byte[bufferSize];
int bb;
while ((bb = in.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, bb);
}
in.close();
}
/**
* Export zipped comma-separated-values file
*
* @param response
* HttpServletResponse
* @param bytes
* byte[] Bytes from csv formatted string
* @param encoding
* String Encoding for http content type.
* @throws IOException
*/
private synchronized void doZippedExport(HttpServletResponse response,
byte[] bytes, String encoding) throws IOException {
//Set http response header
response.reset();
response.setContentType(FileConstants.ZIP_CONTENT_TYPE + " charset=" + encoding);
response.setHeader("Content-disposition", "filename=" + fileName + FileConstants.FILE_EXT_NAME_ZIP);
//Zip and export.
ZipOutputStream os = null;
BufferedInputStream is = null;
try {
os = new ZipOutputStream(response.getOutputStream());
os.putNextEntry(new ZipEntry(fileName));
is = new BufferedInputStream(new ByteArrayInputStream(bytes));
int b = 0;
byte[] buf = new byte[bufferSize];
while ((b = is.read(buf, 0, buf.length)) != -1) {
os.write(buf, 0, b);
}
os.flush();
} catch (IOException e) {
LogWritter.sysError("[doZippedExport] Exception ", e);
throw e;
} finally {
closeStream(os, is);
}
}
/**
* Close byte stream
* @param os OutputStream
* @param is InputStream
* @throws IOException
*/
private void closeStream(OutputStream os, InputStream is) {
try {
if (os != null) {
os.close();
os = null;
}
if (is != null) {
is.close();
is = null;
}
} catch (IOException e) {
LogWritter.sysError("Exception while closing Stream", e);
e.printStackTrace();
}
}
/**
* 关闭输出流
* @param os
*/
private void closeStream(OutputStream os) {
try {
if (os != null) {
os.close();
os = null;
}
} catch (IOException e) {
LogWritter.sysError("Exception while closing OutputStream", e);
e.printStackTrace();
}
}
//------------------------------属性和setter和getter方法---------------------------------------------
private String contentTypeName; //向外提供的传入头类型名字 使用FileConstants中的类型 例FileConstants.FILE_EXT_NAME_CSV
private String encodeType = FileConstants.CHARACTER_ENCODING_UTF8; //enconding名字,默认为UTF8,若不指定 用默认
private String fileName; //文件名,不含后缀名
private String contentType; //导出文件的类型
private int bufferSize = 2048; //Stream buffer size
private static Map contentTypeMap = new HashMap();
//当前导出的文件是否已经压缩,默认为未压缩
private boolean isZip = false;
/**
* @return the isZip
*/
public boolean isZip() {
return isZip;
}
/**
* @param isZip the isZip to set
*/
public void setZip(boolean isZip) {
this.isZip = isZip;
}
//get set 方法
public String getEncodeType() {
return encodeType;
}
public void setEncodeType(String encodeType) {
this.encodeType = encodeType;
}
public String getContentTypeName() {
return contentTypeName;
}
public void setContentTypeName(String contentTypeName) {
this.contentTypeName = contentTypeName;
//此处为映射
this.contentType = (String) contentTypeMap.get(contentTypeName);
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* Set stream buffer size
*
* @param bufferSize int New stream buffer size
*/
public void setBufferSize(int bufferSize) {
this.bufferSize = bufferSize;
}
public int getBufferSize() {
return bufferSize;
}
/**
* Set exported file name
* @param fileName The fileName to set.
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
}
- 相关文件.zip (374.1 KB)
- 下载次数: 38
发表评论
-
Goole 地图 根据经纬度获取地址
2012-12-10 22:40 798package com.jueyue; import jav ... -
百度地图根据地址获取经纬度
2012-12-10 21:31 8399package com.jueyue; import jav ... -
Java对象的序列化和反序列化实践
2012-12-10 19:52 624当两个进程在进行 ... -
HttpClient 实现访问 HTTPS
2012-11-08 11:24 0避免HttpClient的”javax.net.ssl.SSL ... -
实现一个线程池
2012-05-17 22:40 769public class SjgxrwStartQuest e ... -
通过 HttpClient 下载 文件
2011-10-27 13:49 973需要有的Jar; * commons-httpclie ... -
Java 文件拷贝
2011-10-26 18:26 760public static void saveFiles(S ... -
生成jar文件的方法
2011-10-13 01:20 682JAR --Java Archive File,顾 ... -
文件下载
2011-08-30 13:58 677public ModelAndView downloadFil ... -
根据Map 动态生成一个类 动态打印
2011-07-15 23:28 878import java.lang.reflect.Field; ... -
Java实现类排序
2011-07-12 16:07 864用Java实现类排序 如下步骤: 1 待排序的类实现 ... -
web service(axis)例子HelloService步骤说明
2011-07-12 11:36 9321.将axis1.1目录下webapps下的axis包拷贝到t ... -
解决一台机器同时运行多个Tomcat服务
2011-07-12 10:07 857如果不加任何修改,在一台服务器上同时运行两个Tomcat服务显 ... -
Java 解析 Word Word 中的表格
2011-07-04 22:15 4324import java.io.File; import ... -
Java 解析 PDF, pdfbox读取PDF内容
2011-07-04 20:59 6321import java.io.ByteArrayOutputS ... -
Java 页面表格导出Word
2011-06-21 10:25 1294StringBuffer sb = new StringBuf ... -
利用lucene对整个数据库建立索引(lucene,SQL,JDBC)(
2011-06-16 23:38 1039导言: 如果要对整个数据库做精确查询或模糊查询,我们怎么才可 ... -
Java Mail 收发邮件
2011-06-15 22:24 829import javax.mail.Address;impor ... -
Java对象序列化
2011-03-09 22:47 701被序列化的类要implements Serializab ... -
JavaScript 为Select添加节点
2011-03-01 09:53 947function addOption(){ var ...
相关推荐
最近因项目开发的需要,整理了一份用JAVA导出WORD文档,其部署步骤如下: 1、将jacob-1.14.3-x86.dll放在服务器的系统盘(或运行本机的系统):\WINDOWS\system32目录下。 2、将jacob-1.14.3-x86.dll放在JDK 的 bin ...
网络上的根据模板填充Word我都看过一些, 它们的功能在数据换行的时候用的是run对象的.addCarriageReturn()方法,或者是直接用\n实现换行。这些都不符合我的需求, 因为我要的是分段,而不是换行。换行的word导致另一...
java freemarker导出word -包含多张图片导出
在日常的开发工作中,我们时常会遇到导出Word文档报表的需求,比如公司的财务报表、医院的患者统计报表、电商平台的销售报表等等。 导出Word方式多种多样,通常有以下几种方式: 1. 使用第三方Java工具类库Hutool的...
Java 导出 Word。 可直接运行,有说明文档,有注释,有jar文件。
Java读取Word中的表格(Excel),并导出文件为Excel
POI报表Word导出
包含 的代码实例修改于 网上一帅哥的代码示例,自己添加了 图片动态的展示和 数据表的动态的行和列
用java实现word统计报表和图形统计的导出
stax-api-1.0.1.jar xmlbeans-2.4.0.jar ooxml-schemas-1.0.jar 这三个主要是java导出为word文件的jar包
一个使用Apache的poi使用word模板循环输出表格行并导出的例子
java导出到word,进行写操作,如表格、生成图片等.
JAVA根据数据库表生成word表格文档
java 导入导出Word、excel的Jar包+例子,函API,及部分Demo
java jacob 操作word 文档,进行写操作,如生成表格,添加 图片
这是grails框架的类似java开发,只是类型用def定义所有类型。
java 使用iText生成word文档,包中带有源码与所用jar包,生成文档格式标准。可分页,可带页眉,可生成表格、图片和文字等word元素内容,文字带字号,字体,颜色等。
使用java Apache poi 根据word模板生成word报表 仅支持docx格式的word文件,大概是word2010及以后版本,doc格式不支持。 使用说明:https://blog.csdn.net/u012775558/article/details/79678701