前几天因为项目需要 做了一个导出 是在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;
}
}