即上次单文件导出为zip压缩包文件,增加多文件导出到同一zip文件中,接口调用,获取图片byte字节数组后,保存为图片文件,存入zip文件中。
/**
* 压缩多个文件到一个zip压缩包中
* @description :
* @date 2016-11-4
* @author liucong
* @param request
* @param response
* @throws Exception
*
*
*/
@RequestMapping("/zipMoreFiles")
public void zipMoreFiles(HttpServletRequest request,HttpServletResponse response) throws Exception{
String rootPath = request.getRealPath("/");
File file = new File(rootPath+"temp_download");
if(!file.exists()){
file.mkdir();
}
String name = "图片压缩包下载";
String fileName = name+new Date().getTime();
String zipFileName = fileName + ".zip";
File zipFile = null;
String path = rootPath + "temp_download";
File files = new File("F:\\images\\");
File[] filelist = files.listFiles();
zipFile = new File(path + "/" + zipFileName);
FileOutputStream zipFos = new FileOutputStream(zipFile);
ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
if (archOut instanceof ZipArchiveOutputStream) {
ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
for(File imageFile : filelist){
ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName());
zos.putArchiveEntry(zipEntry);
zos.write(FileUtils.readFileToByteArray(imageFile));
zos.closeArchiveEntry();
zos.flush();
}
zos.close();
}
OutputStream out = null;
out = response.getOutputStream();
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1"));
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
out.write(FileUtils.readFileToByteArray(zipFile));
out.flush();
out.close();
if (zipFile.exists()) {
zipFile.delete();
}
}
调用接口获取图片保存为ZIP文件
/**
* 思路:
* 如果ex_enFlag : 1---查询出口流水列表
* 0---查询入库哦流水列表
* 1、获取出口获取入口流水信息列表
* 2、如果是出口,判断路段编号是否相同,如果相同,则调用路段接口获取,如果不相同,则调用平台接口获取
* 3、如果是入口,则直接调用路段入口图像接口获取。
*
* @description :出入口导出图片公用方法
* @date 2016-11-2
* @author liucong
* @param request
* @param response
* @param sqlString : 高级查询中的sql语句
* @param cpucardsnno : 通行卡号
* @param laneserialno : 出口流水号,入口流水号
* @param ex_enFlag 代表出入口标识,ex---代表出口导出图片,en----代表入口导出图片
* @throws Exception
*/
@RequestMapping("/exportPicture")
public void exportPicture(HttpServletRequest request,HttpServletResponse response,String sqlString,String cpucardsnno,String laneserialno,Integer condition,String ex_enFlag) throws Exception {
int roadid = SessionUtil.getSessionUser().getDefaultroadid();
Map<String, Object> params = new HashMap<String, Object>();
String sqlInfo = "";
try {
laneserialno = URLDecoder.decode(URLDecoder.decode(laneserialno,"UTF-8"),"UTF-8");
cpucardsnno = URLDecoder.decode(URLDecoder.decode(cpucardsnno,"UTF-8"),"UTF-8");
sqlInfo = URLDecoder.decode(URLDecoder.decode(sqlString,"UTF-8"),"UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
Page page = null;
if("ex".equals(ex_enFlag)){
if(condition == null){
params.put("laneexserialno", "-00000");
}else if(condition == 0){
params.put("laneexserialno",laneserialno);
params.put("excpucardsnno", cpucardsnno);
}else{
params.put("sqlInfo", sqlInfo);
}
page = roadChargeExportDataService.exportData(request, LaneexlistDAO.class.getName(), "getLaneexlistListByCondition", "countLaneexlistByCondition", params,ExportImageCount);
}else{
if(condition == null){
params.put("laneenserialno", "-00000");
}else if(condition==1){
params.put("sqlInfo", sqlInfo);
}else{
params.put("laneenserialno", laneserialno);
params.put("encpucardsnno",cpucardsnno);
}
page = roadChargeExportDataService.exportData(request, LaneenlistDAO_RS.class.getName(), "getLaneenlistListByCondition", "countLaneenlistByCondition", params,ExportImageCount);
}
String rootPath = request.getRealPath("/");
int count = page.getTotalCount();
if(count <= ExportImageCount){
List<Laneenlist> laneenlistList = new ArrayList<Laneenlist>();
List<Laneexlist> laneexlistList = new ArrayList<Laneexlist>();
if("ex".equals(ex_enFlag)){
if(count > 0){
laneexlistList = page.getResult();
}
}else{
if(count >0 ){
laneenlistList = page.getResult();
}
}
try{
File file = new File(rootPath+"temp_download");
if(!file.exists()){
file.mkdir();
}
String zipFileName = "";
if("ex".equals(ex_enFlag)){
zipFileName = "出口流水图片.zip";
}else{
zipFileName = "入口流水图片.zip";
}
File zipFile = null;
String path = rootPath + "temp_download";
zipFile = new File(path + "/" + zipFileName);
FileOutputStream zipFos = new FileOutputStream(zipFile);
ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
if (archOut instanceof ZipArchiveOutputStream) {
ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
if("ex".equals(ex_enFlag)){
for(Laneexlist laneexlist : laneexlistList){
byte[] imgData = null;
String imageSerialNo = laneexlist.getImageserialno();
Long enRoadid = laneexlist.getEnroadid();
if(roadid == enRoadid){
try{
imgData = listimageService.getImageByExSerialNo(imageSerialNo);
}catch (Exception e) {
}
}else{
try{
imgData = commonDealService.getPTServiceImage(request, response, imageSerialNo, enRoadid.toString());
}catch (Exception e) {
}
}
File imageFile = commonDealService.save(path, ex_enFlag, imgData, imageSerialNo);
commonDealService.putZipFiles(imageFile, zos);
}
zos.close();
}else{
for(Laneenlist laneenlist : laneenlistList){
String imageSerialNo = laneenlist.getImageserialno();
byte[] imgData = null;
try{
imgData = listimageService.getImageByEnSerialNo(imageSerialNo);
}catch(Exception e){
}
File imageFile = commonDealService.save(path, ex_enFlag, imgData, imageSerialNo);
commonDealService.putZipFiles(imageFile, zos);
}
zos.close();
}
}
OutputStream out = null;
out = response.getOutputStream();
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1"));
response.setContentType("application/octet-stream; charset=utf-8");
response.setCharacterEncoding("UTF-8");
out.write(FileUtils.readFileToByteArray(zipFile));
out.flush();
out.close();
if (zipFile.exists()) {
zipFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}else{
response.setCharacterEncoding("gbk");
try {
PrintWriter writer = response.getWriter();
writer.write("记录数大于"+ExportImageCount+",请分时间段导出!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
CommonDealService代码
/**
* 将图片文件保存到本地
* @author liucong
* 思路:调用路段接口进行查询图片信息,获取byte数据,保存文件。
*/
public File save(String path,String ex_enFlag,byte[] data,String imageSerialno) throws Exception //保存图片到相应的路径下
{
File imageFile = null;
if(data != null && data.length >0){
Random random = new Random();
imageFile = new File(path+File.separator+ex_enFlag+"_"+imageSerialno+".jpg");
FileOutputStream outStream = new FileOutputStream(imageFile);
outStream.write(data);
outStream.close();
}
return imageFile;
}
/**
* 将文件put进入zip 文件中,
* @description :
* @date 2016-11-4
* @author liucong
* @param imageFile 图片文件
* @param zos zip输出流
* @throws IOException
*
*
*/
public void putZipFiles(File imageFile,ZipArchiveOutputStream zos) throws IOException{
if(imageFile != null && imageFile.exists()){
ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName());
zos.putArchiveEntry(zipEntry);
zos.write(FileUtils.readFileToByteArray(imageFile));
zos.closeArchiveEntry();
zos.flush();
imageFile.delete();
}
}