package com.hesc.wpc.common.utils; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Date; import java.util.List; import java.util.Map; import javax.mail.internet.MimeUtility; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; import com.hesc.wpc.web.controller.admin.meeting.view.MeetingView; import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; import sun.misc.BASE64Encoder; /** * 生成word文档工具类 * @author ypf * @date 2017-05-17 */ public class WordUtil { private static final Logger logger = LoggerFactory.getLogger(MeetingView.class); private static Configuration configuration = null; static { configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); try { ResourceLoader resourceLoader = new DefaultResourceLoader(); //指定模板目录在类路径:WEB-INF/classes Resource resource = resourceLoader.getResource("/"); File file = resource.getFile(); //设置要解析的模板所在的目录,并加载模板文件 configuration.setDirectoryForTemplateLoading(file); ///设置包装器,并将对象包装为数据模型 configuration.setObjectWrapper(new DefaultObjectWrapper()); } catch (IOException e) { e.printStackTrace(); } } /** * 根据类路径获取模板 * @param templatePath * @param templateName * @return * @throws IOException */ private static Template getTemplate(String templatePath, String templateName) throws IOException { Configuration configuration = new Configuration(Configuration.VERSION_2_3_23); configuration.setDefaultEncoding("UTF-8"); configuration.setDirectoryForTemplateLoading(new File(templatePath)); return configuration.getTemplate(templateName, "UTF-8"); } /** * 导出word 文档 * @param typeName 会议类型名称 * @param dataMap 数据 * @param xmlpath xml 模板路径 * @param response * @param request */ public static void createWord(String typeName,MapdataMap,String xmlpath,HttpServletResponse response, HttpServletRequest request){ Template t = null; try { t = getTemplate(request.getSession().getServletContext().getRealPath("/"), xmlpath); } catch (IOException e1) { e1.printStackTrace(); } File file = null; InputStream fin = null; ServletOutputStream out =null; file = createDocx(dataMap,t,request); try { fin = new FileInputStream(file); String filename=encodeFilename(typeName+".doc", request); response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=\""+filename+"\""); out = response.getOutputStream(); byte [] buffer=new byte[512]; int byteToRead = -1; while((byteToRead = fin.read(buffer)) != -1){ out.write(buffer, 0, byteToRead); } } catch (IOException e) { logger.error("word文档导出错误:"+e.getMessage()); }finally{ try { if(null != fin){ fin.close(); } if(null != out){ out.flush(); out.close(); } } catch (IOException e) { logger.error("word文档导出错误:"+e.getMessage()); } } } /** * 导出word 文档 * @param typeName 会议类型名称 * @param dataMap 数据 * @param xmlpath xml 模板路径 * @param response * @param request */ public static void createWord(String typeName,List
package com.hesc.wpc.common.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ZipUtils { private static final Logger log = LoggerFactory.getLogger(ZipUtils.class); private ZipUtils(){ } public static void doCompress(String srcFile, String zipFile) throws IOException { doCompress(new File(srcFile), new File(zipFile)); } /** * 文件压缩 * @param srcFile 目录或者单个文件 * @param zipFile 压缩后的ZIP文件 */ public static void doCompress(File srcFile, File zipFile) throws IOException { ZipOutputStream out = null; try { out = new ZipOutputStream(new FileOutputStream(zipFile)); doCompress(srcFile, out); } catch (Exception e) { throw e; } finally { out.close();//记得关闭资源 } } public static void doCompress(String filelName, ZipOutputStream out) throws IOException{ doCompress(new File(filelName), out); } public static void doCompress(File file, ZipOutputStream out) throws IOException{ doCompress(file, out, ""); } public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException { if ( inFile.isDirectory() ) { File[] files = inFile.listFiles(); if (files!=null && files.length>0) { for (File file : files) { String name = inFile.getName(); if (!"".equals(dir)) { name = dir + "/" + name; } ZipUtils.doCompress(file, out, name); } } } else { ZipUtils.doZip(inFile, out, dir); } } public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException { String entryName = null; if (!"".equals(dir)) { entryName = dir + "/" + inFile.getName(); } else { entryName = inFile.getName(); } ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); int len = 0 ; byte[] buffer = new byte[1024]; FileInputStream fis = new FileInputStream(inFile); while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); out.flush(); } out.closeEntry(); fis.close(); } /** * 创建ZIP文件 * @param sourcePath 文件或文件夹路径 * @param zipPath 生成的zip文件存在路径(包括文件名) */ public static void createZip(String sourcePath, String zipPath) { FileOutputStream fos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(zipPath); zos = new ZipOutputStream(fos); writeZip(new File(sourcePath), "", zos); } catch (FileNotFoundException e) { log.error("ZipUtils createZip Failed to create ZIP file", e); } finally { try { if (zos != null) { log.debug("ZipUtils createZip Create a ZIP file successfully! the path in:{}",zipPath); zos.close(); //压缩成功后,删除打包前的文件 deleteFile( new File(sourcePath) ); } } catch (IOException e) { log.error("ZipUtils createZip Failed to create ZIP file", e); } } } private static void writeZip(File file, String parentPath, ZipOutputStream zos) { if (file.exists()) { if (file.isDirectory()) {// 处理文件夹 parentPath += file.getName() + File.separator; File[] files = file.listFiles(); for (File f : files) { writeZip(f, parentPath, zos); } } else { FileInputStream fis = null; try { fis = new FileInputStream(file); ZipEntry ze = new ZipEntry(parentPath + file.getName()); zos.putNextEntry(ze); byte[] content = new byte[1024]; int len; while ((len = fis.read(content)) != -1) { zos.write(content, 0, len); zos.flush(); } } catch (FileNotFoundException e) { log.error("ZipUtils createZip Failed to create ZIP file",e); } catch (IOException e) { log.error("ZipUtils createZip Failed to create ZIP file",e); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { log.error("ZipUtils createZip Failed to create ZIP file",e); } } } } } /** * 删除文件夹 * @param file */ public static void deleteFile(File file) { if (file.exists()) { // 判断文件是否存在 if (file.isFile()) { // 判断是否是文件 file.delete(); } else if (file.isDirectory()) { // 否则如果它是一个目录 File files[] = file.listFiles(); // 声明目录下所有的文件 files[]; for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件 deleteFile(files[i]); // 把每个文件 用这个方法进行迭代 } } file.delete(); } } /* public static void main(String[] args) throws IOException { doCompress("D:/java/", "D:/java.zip"); }*/ }