我们的照片存储在数据库,需要下载。开发思路是:先把数据库的照片保存到文件,保存完后就把文件压缩成zip文件,然后提供下载zip文件的途径
以下是源码
package com.zxl.action.;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.tools.zip.ZipOutputStream;
import com.datalook.action.base.BaseAction;
import com.datalook.model.CardUserImg;
import com.datalook.service.CardUserImgService;
import com.datalook.util.common.DateUtil;
public class CardUserImgAction extends BaseAction{
/**
*
*/
private static final long serialVersionUID = 3907740440946817870L;
private CardUserImgService cardUserImgService;
private String departid;
private String pcodes;
private String idserial;
private String positionMsg;
private InputStream imageStream;
private String downloadFilename;
private String message;
public CardUserImgService getCardUserImgService() {
return cardUserImgService;
}
public void setCardUserImgService(CardUserImgService cardUserImgService) {
this.cardUserImgService = cardUserImgService;
}
public String getDepartid() {
return departid;
}
public void setDepartid(String departid) {
this.departid = departid;
}
public String getPcodes() {
return pcodes;
}
public void setPcodes(String pcodes) {
this.pcodes = pcodes;
}
public String getIdserial() {
return idserial;
}
public void setIdserial(String idserial) {
this.idserial = idserial;
}
public String getPositionMsg() {
return positionMsg;
}
public void setPositionMsg(String positionMsg) {
this.positionMsg = positionMsg;
}
public InputStream getImageStream() {
return imageStream;
}
public void setImageStream(InputStream imageStream) {
this.imageStream = imageStream;
}
public String getDownloadFilename() {
return downloadFilename;
}
public void setDownloadFilename(String downloadFilename) {
this.downloadFilename = downloadFilename;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/**
*
*功能描述:打开照片下载页面
*时间:2014-3-28
*@author:zengxinliang
* @return
*/
public String openDownloadImg(){
positionMsg=this.baseService.getPosition();
/*String filePath=this.getServletRequest().getSession().getServletContext().getRealPath("/")+"pic/";
filePath=filePath.replaceAll("\\\\", "/");
File fil=new File(filePath);
//看看pic文件是否存在,存在先删除后创建,主要是为了删除里面以前下载过的照片
if(!fil.exists()){
fil.mkdir();
}else{
fil.delete();
fil.mkdir();
}*/
return "openDownloadImg";
}
/**
* '
*功能描述:导出照片
*时间:2014-3-28
*@author:zengxinliang
* @return
*/
public String exportImg(){
positionMsg=this.baseService.getPosition();
if(pcodes!=null&&!pcodes.equals("")){
pcodes=pcodes.replace(";", ",");
}
if(departid!=null&&!departid.equals("")){
departid=this.queryDeptIdCascadeByClickedNodes(departid);
}
List<CardUserImg> cardUserImgList=this.cardUserImgService.downloadImg(departid, pcodes, idserial);
for(int i=0;i<cardUserImgList.size();i++){
CardUserImg cardUserImg=cardUserImgList.get(i);
savePicture(cardUserImg);
}
if(!cardUserImgList.isEmpty()){
String path=this.getServletRequest().getSession().getServletContext().getRealPath("/")+"pic/"+DateUtil.getDateString()+"/";
System.out.println(path);
String imgPath = path.replaceAll("\\\\", "/");
try {
zip(imgPath);//压缩文件为zip格式
message="1";//导出成功
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error("导出照片信息出错:", e);
e.printStackTrace();
}
}else{
message="0";//没有照片
}
return "openDownloadImg";
}
/**
*
*功能描述:保存照片到指定路径
*时间:2014-3-28
*@author:zengxinliang
* @param cardUserImg
* @return
*/
public String savePicture(CardUserImg cardUserImg){
imageStream = new ByteArrayInputStream(cardUserImg.getImg());
try{
String path=this.getServletRequest().getSession().getServletContext().getRealPath("/")+"pic/";
path+=DateUtil.getDateString()+"/";
String imgPath = path.replaceAll("\\\\", "/");
String imgName = cardUserImg.getIdserial()+".jpg";
File file=new File(imgPath,imgName);//可以是任何图片格式.jpg,.png等
FileOutputStream fos=new FileOutputStream(file);
byte[] b = new byte[1024];
int nRead = 0;
while ((nRead = imageStream.read(b)) != -1) {
fos.write(b, 0, nRead);
}
fos.flush();
fos.close();
//file.delete();
imageStream.close();
}catch(Exception e){
logger.error("保存照片信息出错:", e);
e.printStackTrace();
}
return null;
}
public void zip(String inputFileName) throws Exception {
String path=this.getServletRequest().getSession().getServletContext().getRealPath("/")+"pic/";
path=path.replaceAll("\\\\", "/");
String zipFileName = path+DateUtil.getDateString()+".zip"; //打包后文件保存的路径
zip(zipFileName, new File(inputFileName));
}
private void zip(String zipFileName, File inputFile) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out, inputFile, "");
System.out.println("zip done");
out.close();
}
/**
*
* 功能描述:压缩时写文件
* 时间:2014-4-28
* @author:zengxinliang
*/
private void zip(ZipOutputStream out, File f, String base) throws Exception {
if (f.isDirectory()) {
File[] fl = f.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
}else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
//System.out.println(base);
while ( (b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
/**
*
* 功能描述:获得输出流
* 时间:2014-4-28
* @author:zengxinliang
*/
public InputStream getInputStream(){
//String path=this.getServletRequest().getSession().getServletContext().getRealPath("/")+"pic\\";
//path=path.replaceAll("\\\\", "/");
String path="/pic/";
path = path+DateUtil.getDateString()+".zip";
return ServletActionContext.getServletContext().getResourceAsStream(path);
}
/**
*
* 功能描述:下载照片
* 时间:2014-4-28
* @author:zengxinliang
*/
public String downloadImg(){
try {
String path="/pic/";
path = path+DateUtil.getDateString()+".zip";
if(ServletActionContext.getServletContext().getResourceAsStream(path)==null){
positionMsg=this.baseService.getPosition();
message="2";//没有导出照片
return "openDownloadImg";
}
downloadFilename=new String((DateUtil.getDateString()+".zip").getBytes(),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
logger.error("下载照片信息出错:", e);
e.printStackTrace();
}
return SUCCESS;
}
/*public static void main(String args[]) throws Exception{
zip("E:/software/Tomcat/tomcat-6.0.16/webapps/DlWebSysTY/pic/140428");
}*/
}
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${downloadFilename}"
</param>
<param name="bufferSize">1048576000</param>
</result>