import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.centit.cmip.app.common.dao.DefaultDao;
import com.centit.cmip.biz.common.util.Office2PDF;
import com.centit.cmip.biz.nhutils.WaterMarkUtils;
import com.centit.cmip.framework.action.BaseAction;
import com.centit.cmip.sdk.common.utils.DateUtil;
import com.centit.cmip.util.FileUtils;
import com.centit.cmip.util.PrivateKeyUtil;
import com.centit.cmip.webapp.assetmgr.entity.AssetEntity;
import com.centit.cmip.webapp.assetmgr.service.AssetService;
import com.centit.cmip.webapp.house.service.HouseService;
@Controller("downloadAction")
@Scope("prototype")
public class DownLoadAction extends BaseAction {
private static final long serialVersionUID = 2358985464071330705L;
private static Logger logger = Logger.getLogger(DownLoadAction.class);
private AssetEntity asset;
@Value("${upload.filedir}")
private String uploadDir;
@Value("${image.prefix}")
private String imagePrefix;
@Value("${filepush.url}")
private String filepushUrl;//文件转发目标服务器地址
@Value("${filepush.switch}")
private String filepushSwitch;//文件转发开关
@Value("${persistence.type}")
private String persistenceType;//持久化类型
@Resource
private AssetService assetService;
@Resource
private PrivateKeyUtil privateKeyUtil;
@Resource
private Office2PDF office2PDF;
@Resource
private DefaultDao defaultDao;
@Resource
private HouseService houseService;
@PostConstruct
public void init() {
if (null == asset) {
asset = new AssetEntity();
}
}
/**
* 图片文件批量打包下载/单个下载
*/
@SuppressWarnings("unchecked")
public void downZipFiles(){
String ids = getRequest().getParameter("ids");
String creator = getRequest().getParameter("creator");
String userid = getRequest().getParameter("userid");
String[] idss = FileUtils.StringToArray(ids, ",");
String fileName = "";
String fileDownloadPath = "";
try {
Map<String, Object> param = new HashMap<String, Object>();
param.put("idArr", idss);
param.put("creator", creator);
param.put("userid", userid);
//根据上传人userid 查询上传人部门 根据下载人userid 查询下载人姓名
List<Map<String, Object>> userList=(List<Map<String, Object>>)defaultDao.queryForList("queryuploaddept", param);
String uploaddept = "";
String username = "";
for(Map<String, Object> map:userList) {
uploaddept = (String)map.get("rolename");
username = (String)map.get("username");
}
//根据id 查询文件的信息
List<Map<String, Object>> imgPathList=(List<Map<String, Object>>)defaultDao.queryForList("selectFilePathList", param);
if (imgPathList.size() > 0){
fileName = username+"_"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip";
fileDownloadPath = uploadDir + "/"+fileName;
byte[] buffer = new byte[1024];
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(fileDownloadPath));
for (Map<String, Object> map : imgPathList) {
FileInputStream fis = null;
try {
String imageUrl = (String)map.get("STORE_PATH");
String imageName = (String)map.get("IMAGE_NAME");
String type = (String)map.get("TYPE");
//判断文件类型 如果是图片类型 则将图片加水印 然后放入压缩包
if(type.equals("image/jpeg")||type.equals("image/png")||type.equals("jpg")) {
//源图片地址
String srcImgPath=uploadDir + imageUrl;
//水印图片路径
String imgpath = "/resource/download/" + DateUtil.current("yyyy-MM-dd")+"/" +imageName;
//水印图片磁盘路径
String tarImgPath = uploadDir+imgpath;
//创建下载文件夹 否则找不到相应的路径
File file = new File(tarImgPath);
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
//水印内容
String waterMarkContent=uploaddept+"_"+username+"_"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); //水印内容
Color color=new Color(0, 204, 238,100); //水印图片色彩以及透明度
new WaterMarkUtils().addWaterMark(srcImgPath, tarImgPath, waterMarkContent, color);
imageUrl = imgpath;
}
File file = new File(uploadDir + imageUrl);
fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry(imageName));
//设置压缩文件内的字符编码,不然会变成乱码
out.setEncoding("GBK");
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.closeEntry();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
out.close();
File fileDownload = new File(fileDownloadPath);
if (fileDownload.exists()) {
getResponse().setContentType("application/octet-stream");
getResponse().setHeader("Content-Disposition","attachment; filename="+new String((fileName).getBytes("gbk"),"iso8859-1"));
FileInputStream fins = new FileInputStream(fileDownload);
FileUtils.writeIntoOut(fins, getResponse().getOutputStream());
fileDownload.delete();
} else {
getResponse().sendError(404);
}
}else {
//无图片附件
//this.getResponse().setContentType(Constant.CONTENTTYPE);
//this.getResponse().getWriter().write(JSONObject.fromObject("无图片").toString());
}
} catch (Exception e) {
e.printStackTrace();
logger.error("未找到文件!");
}
}
}