WordCommonUtil

来源:互联网 发布:角色建模软件 编辑:程序博客网 时间:2024/06/06 20:01

WordExport


package comutil.word;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.io.Writer;import java.net.URLEncoder;import java.util.Map;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletResponse;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;/** *  *  *  */public class WordExport {private Configuration configuration = null;public WordExport(ServletContext servletContext, String tmpPath) {configuration = new Configuration();configuration.setDefaultEncoding("UTF-8");configuration.setServletContextForTemplateLoading(servletContext,tmpPath);// 获取web下存放路径方式}// 生成wordpublic void createWord(Map<String, Object> data, String temName, File file) {Template template = null;try {template = configuration.getTemplate(temName);Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));try {template.process(data, out);out.close();} catch (TemplateException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 生成解压包public void zip(HttpServletResponse response, String absolutePath,String folderName) {// TODO Auto-generated method stubtry {ZipUtils.zipFile(absolutePath);download(response, folderName + ".zip", absolutePath + ".zip");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 下载public void download(HttpServletResponse response, String fileName,String filePath) {// TODO Auto-generated method stubresponse.reset();response.setContentType("application/x-msdownload;charset=UTF-8");try {response.setHeader("Content-Disposition", "attachment;  filename="+ URLEncoder.encode(fileName, "UTF-8"));} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}InputStream inputStream;OutputStream outputStream;try {inputStream = new FileInputStream(filePath);outputStream = response.getOutputStream();byte[] buffer = new byte[1024];int i = 0;while ((i = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, i);}outputStream.flush();outputStream.close();inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


ZipUtils


package avicit.comutil.word;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.zip.CRC32;import java.util.zip.CheckedOutputStream;import java.util.zip.ZipFile;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipOutputStream;import org.springframework.web.multipart.MultipartFile;/** *  *  * */public class ZipUtils {static final int BUFFER = 2048;private static File zipFile;// 删除文件夹public static void dropFolderOrFile(File file) {if (file.isDirectory()) {File[] fs = file.listFiles();for (File f : fs) {dropFolderOrFile(f);}}file.delete();}// 导入zip文件public static void importZip(String fileUrl, MultipartFile file) {// TODO Auto-generated method stubFile s = new File(fileUrl);if (!s.exists()) {s.mkdirs();}InputStream is;try {is = file.getInputStream();String tempath = fileUrl + File.separator+ file.getOriginalFilename();BufferedInputStream bis = new BufferedInputStream(is);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tempath));byte[] byt = new byte[1024];int len;while ((len = bis.read(byt)) != -1) {bos.write(byt, 0, len);}bos.close();bis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 解压zip文件public static ZipFile decompression(String tempath) {// TODO Auto-generated method stubtry {return new ZipFile(tempath);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}// 文件读取public static void stream(ZipFile zipFile,java.util.zip.ZipEntry zipElement, String folderName) {// TODO Auto-generated method stubtry {File folder = new File(folderName);if (!folder.exists()) {folder.mkdirs();}InputStream read = zipFile.getInputStream(zipElement);String filePath = folderName + File.separator+ zipElement.getName();File file2 = new File(filePath);if (file2.exists()) {file2.delete();} else {file2.createNewFile();}BufferedOutputStream write = new BufferedOutputStream(new FileOutputStream(file2));int cha = 0;while ((cha = read.read()) != -1) {write.write(cha);}write.flush();write.close();read.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 将指定文件夹打包成zippublic static void zipFile(String folder) throws IOException {ZipOutputStream out = null;zipFile = new File(folder + ".zip");FileOutputStream fileOutputStream = new FileOutputStream(zipFile);CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,new CRC32());out = new ZipOutputStream(cos);out.setEncoding(System.getProperty("sun.jnu.encoding"));File dir = new File(folder);File[] fs = dir.listFiles();if (fs != null) {for (File f : fs) {compress(f, out, "");}out.closeEntry();out.close();}}private static void compress(File file, ZipOutputStream out, String basedir) {/* 判断是目录还是文件 */if (file.isDirectory()) {ZipUtils.compressDirectory(file, out, basedir);} else {ZipUtils.compressFile(file, out, basedir);}}/** 压缩一个目录 */private static void compressDirectory(File dir, ZipOutputStream out,String basedir) {if (!dir.exists())return;File[] files = dir.listFiles();for (int i = 0; i < files.length; i++) {/* 递归 */compress(files[i], out, basedir + dir.getName() + "/");}}/** 压缩一个文件 */private static void compressFile(File file, ZipOutputStream zipout,String basedir) {if (!file.exists()) {return;}try {zipout.putNextEntry(new ZipEntry(basedir + file.getName()));FileInputStream fileInputStream = new FileInputStream(file);byte buf[] = new byte[2048];BufferedInputStream origin = new BufferedInputStream(fileInputStream, 2048);int len;while ((len = origin.read(buf, 0, 2048)) != -1) {zipout.write(buf, 0, len);}zipout.closeEntry();origin.close();fileInputStream.close();} catch (Exception e) {throw new RuntimeException(e);}}}



0 0