一般的我们做oa系统之类的,仅仅用到导出pdf、csv、excel、word、那么突然间让你导出一个压缩包,是不是很懵逼,下面我也仅仅是对自己做一个记录,毕竟再是自己写的代码,也会忘掉,那么废话结束,let me go
//导出证件照
public void exportProfilePic(){
StringBuffer sql = new StringBuffer();
sql.append(" SELECT ");
sql.append(" o.`name` oName, ");
sql.append(" t.`name` tName, ");
sql.append(" p.`name` pName, ");
sql.append(" p.id_card, ");
sql.append(" p.profile_picture ");
sql.append(" FROM ");
sql.append(" person p, ");
sql.append(" organization o, ");
sql.append(" person_team pt, ");
sql.append(" team t ");
sql.append(" WHERE ");
sql.append(" p.fk_organization_id = o.id ");
sql.append(" AND pt.fk_person_id = p.id ");
sql.append(" AND pt.fk_team_id = t.id ");
sql.append(" AND p.id IN ( ");
sql.append(" SELECT ");
sql.append(" p.id ");
sql.append(" FROM ");
sql.append(" team_person_category tpc, ");
sql.append(" person p, ");
sql.append(" role r ");
sql.append(" WHERE ");
sql.append(" tpc.fk_athlete_id = p.id ");
sql.append(" AND p.fk_role_id = r.id ");
sql.append(" AND r.`code` = ? ");
sql.append(" AND p.game_code = ? ");
sql.append(" UNION ");
sql.append(" SELECT ");
sql.append(" p.id ");
sql.append(" FROM ");
sql.append(" team_person_category tpc, ");
sql.append(" person p, ");
sql.append(" role r ");
sql.append(" WHERE ");
sql.append(" INSTR(tpc.fk_athlete_ids, p.id) > 0 ");
sql.append(" AND p.fk_role_id = r.id ");
sql.append(" AND r.`code` = ? ");
sql.append(" AND p.game_code = ? ");
sql.append(" AND tpc.fk_athlete_ids IS NOT NULL ");
sql.append(" ) ");
try {
//目标压缩文件目录
String filePath = this.getRootPath() + "/" + Constant.FILE_BASE_PATH + "/" + this.getSystemGame().getCode() + "/signupPic/";
List lists = this.getGeneralService().getBySQL(sql.toString(), new Object[]{Constant.ROLE_ATHLETE_CODE,this.getSystemGame().getCode(),Constant.ROLE_ATHLETE_CODE,this.getSystemGame().getCode()});
if(lists.size() > 0) {
for (Object object : lists) {
Object[] result = (Object[]) object;
String orgName = result[0] == null ? "" : result[0].toString();
String teamName = result[1] == null ? "" : result[1].toString();
String pName = result[2] == null ? "" : result[2].toString();
String idCard = result[3] == null ? "" : result[3].toString();
String proFilePic = result[4] == null ? "" : result[4].toString();
String personName = "";
if(!AppTools.StrIsEmpty(pName)){
personName += pName;
if(!AppTools.StrIsEmpty(idCard)){
personName +=" "+ idCard;
}
}
String file = filePath + orgName + "/" + teamName + "/" + personName + "/" ;
File pNameFile = new File(filePath + orgName + "/" + teamName + "/" + personName + "/");
if(!pNameFile.exists()) {
pNameFile.getParentFile().mkdirs();
System.out.println("创建了文件夹");
}
String fileName = getFileName(proFilePic);
File picFile = new File(proFilePic);
//复制图片到指定位置
copy(proFilePic, file, fileName );
}
}
//压缩包名称
String zipName = "证件照.zip";
//压缩源路径
File src = new File(filePath);
//压缩目的路径
File zipFile = new File(this.getRootPath() + "/" + Constant.FILE_BASE_PATH + "/" + this.getSystemGame().getCode() + "/" + zipName);
if (!src.exists()) {//判断源路径是否存在
throw new RuntimeException(filePath + "不存在");
}
//创建zip文件
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
String baseDir = "";
//按照原路径的类型就行压缩。文件路径直接把文件压缩,
compressbyType(src, zos, baseDir);
zos.close();
// 以流的形式下载文件。
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/pdf;charset=UTF-8");
InputStream fis = new BufferedInputStream(new FileInputStream(zipFile));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 按照原路径的类型就行压缩。文件路径直接把文件压缩,
* @param src
* @param zos
* @param baseDir
*/
private static void compressbyType(File src, ZipOutputStream zos,String baseDir) {
if (!src.exists())
return;
//判断文件是否是文件,如果是文件调用compressFile方法,如果是路径,则调用compressDir方法;
if (src.isFile()) {//src是文件
compressFile(src, zos, baseDir);
} else if (src.isDirectory()) {//src是文件夹
compressDir(src, zos, baseDir);
}
}
/**
* 压缩文件
*/
private static void compressFile(File file, ZipOutputStream zos,String baseDir) {
if (!file.exists())
return;
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zos.putNextEntry(entry);
int count;
byte[] buf = new byte[1024];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
bis.close();
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* 压缩文件夹
*/
private static void compressDir(File dir, ZipOutputStream zos,String baseDir) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
if(files.length == 0){
try {
zos.putNextEntry(new ZipEntry(baseDir + dir.getName()+File.separator));
} catch (IOException e) {
e.printStackTrace();
}
}
for (File file : files) {
compressbyType(file, zos, baseDir + dir.getName() + File.separator);
}
}
/**
* 从地址中取文件名,
* @param fileName 地址
* @return 格式为文件名.后缀
*/
public String getFileName(String fileName) {
File tempFile =new File( fileName.trim());
fileName = tempFile.getName();
System.out.println("fileName = " + fileName);
return fileName;
}
/**
* 复制文件:给定一个源文件的文件路径和文件名,将他拷贝到目标路径取,
* @param url1 源文件的路径加文件名
* @param url2 目标路径
* @param fileName 目标文件名
* @throws Exception
*/
public void copy(String url1, String url2,String fileName) throws Exception {
if(!AppTools.StrIsEmpty(url1)) {
url1 = this.getRootPath() + "/" + url1 ;
File file = new File(url1);
File copyFile = null;
if (file.exists()) { //当第一个文件存在的时候
copyFile = new File(url2);
if (!copyFile.exists()) { //如果copyfile不存在的话,就新建文件夹
copyFile.mkdirs();
}
//文件io
FileInputStream in = new FileInputStream(new File(url1));
FileOutputStream out = new FileOutputStream(new File(url2 + "\\" + fileName));
byte[] buff = new byte[512];
int n = 0;
System.out.println("复制文件:\n源路径:" + url1 + "\n" + "目标路径:" + url2 + "\\" + fileName);
while ((n = in.read(buff)) != -1) {
out.write(buff, 0, n);
}
out.flush();
in.close();
out.close();
System.out.println("复制完成");
} else {
System.out.println("源文件不存在");
}
}
}
ok 以上就是全部内容了,本人小白,不喜勿喷哦,可能很low,但就这样吧
用到的是Java7 的与zip有关的包