在实际项目中,经常需要将数据库读出的数据导入到 文件中,而现在用得最多的就是ORM工具,得出的就是一个个对象,而将这些对象中的数据写入文件还需要很多操作. 这里结合java放射技术直接将对象作为参数,然后经过反射解析得出数据然后写入文件。
1。 类设计图(运用工厂设计模式)。
Download |
PdfExport |
XmlExport |
ExcelExport |
IExport |
ExportFactory |
Extends |
Extends |
Extends |
implements |
implements |
implements |

2 。类实现
Download类提供了通用的文件下载方法并在IExport接口中声明。
/**
* 文件下载方法
* @param outfileName,输出显示文件名
* @param outfile,需要下载的文件名
* @param response
*/
public int download(String outfileName,String outfile,HttpServletResponse response);
/**
* 文件下载方法
* @param outfileName,输出显示文件名
* @param outfile,需要下载的文件名
* @param response
*/
public int download(String outfileName,String outfile,WebResponse response);
Download实现:
package org.xiaohongli.common.export;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.tapestry.util.ContentType;
import org.apache.tapestry.web.WebResponse;
import org.xiaohongli.common.Contenttype;
public class Download {
public int download(String outfileName, String outfile, HttpServletResponse response) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
try {
response.setContentType(Contenttype.getContentType(outfileName));
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode(outfileName, "utf-8"));
java.io.File file = new java.io.File(outfile);
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5*1024];
while ((bytesRead = bis.read(buffer,0,5*1024)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件发送到客户端
}
bos.flush() ;
}
catch (IOException e) {
response.reset();
return Util.ERROR ;
}
finally {
try {
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
if (fis != null) {
fis.close();
}
if (bis != null) {
bis.close();
}
}
catch (IOException e) {
return Util.ERROR ;
}
}
return Util.SUCCESS ;
}
public int download(String outfileName, String outfile, WebResponse response) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
try {
ContentType contentType = new ContentType(Contenttype.getContentType(outfileName));
response.setHeader("Content-disposition", "attachment;filename="
+ URLEncoder.encode(outfileName, "utf-8"));
fos = response.getOutputStream(contentType) ;
java.io.File file = new java.io.File(outfile);
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5*1024];
while ((bytesRead = bis.read(buffer,0,5*1024)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件发送到客户端
}
bos.flush() ;
}
catch (IOException e) {
response.reset();
return Util.ERROR ;
}
finally {
try {
if (fos != null) {
fos.close();
}
if (bos != null) {
bos.close();
}
if (fis != null) {
fis.close();
}
if (bis != null) {
bis.close();
}
}
catch (IOException e) {
return Util.ERROR ;
}
}
return Util.SUCCESS ;
}
}
Contenttype类提供了解析文件类型,并获得正确的ContentType,实现
package org.xiaohongli.common;
public class Contenttype {
public static String getContentType(String fileName) {
String fileNameTmp = fileName.toLowerCase();
String ret = "";
if (fileNameTmp.endsWith("txt")) {
ret = "text/plain";
}
if (fileNameTmp.endsWith("gif")) {
ret = "image/gif";
}
if (fileNameTmp.endsWith("jpg")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("jpeg")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("jpe")) {
ret = "image/jpeg";
}
if (fileNameTmp.endsWith("zip")) {
ret = "application/zip";
}
if (fileNameTmp.endsWith("rar")) {
ret = "application/rar";
}
if (fileNameTmp.endsWith("doc")) {
ret = "application/msword";
}
if (fileNameTmp.endsWith("ppt")) {
ret = "application/vnd.ms-powerpoint";
}
if (fileNameTmp.endsWith("xls")) {
ret = "application/vnd.ms-excel";
}
if (fileNameTmp.endsWith("html")) {
ret = "text/html";
}
if (fileNameTmp.endsWith("htm")) {
ret = "text/html";
}
if (fileNameTmp.endsWith("tif")) {
ret = "image/tiff";
}
if (fileNameTmp.endsWith("tiff")) {
ret = "image/tiff";
}
if (fileNameTmp.endsWith("pdf")) {
ret = "application/pdf";
}
return ret;
}
}
Util.SUCCESS 和Util.ERROR是定义的两个静态常量。标识操作成功和失败。
未完待续。。。。。。。。。。。