|
| 1 | +package com.example.demo.freemarker; |
| 2 | + |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileInputStream; |
| 6 | +import java.io.FileNotFoundException; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.OutputStreamWriter; |
| 10 | + |
| 11 | +import org.apache.commons.codec.binary.Base64; |
| 12 | +import org.apache.commons.io.IOUtils; |
| 13 | + |
| 14 | +import freemarker.core.ParseException; |
| 15 | +import freemarker.template.Configuration; |
| 16 | +import freemarker.template.MalformedTemplateNameException; |
| 17 | +import freemarker.template.Template; |
| 18 | +import freemarker.template.TemplateException; |
| 19 | +import freemarker.template.TemplateExceptionHandler; |
| 20 | +import freemarker.template.TemplateNotFoundException; |
| 21 | + |
| 22 | +public class ExportDocUtil { |
| 23 | + |
| 24 | + private Configuration configuration = null; |
| 25 | + |
| 26 | + private String encoding; |
| 27 | + |
| 28 | + public ExportDocUtil(String encoding) { |
| 29 | + this.encoding = encoding; |
| 30 | + configuration = new Configuration(Configuration.VERSION_2_3_27); |
| 31 | + configuration.setDefaultEncoding(encoding); |
| 32 | + try { |
| 33 | + configuration.setDirectoryForTemplateLoading(new File("/where/you/store/templates")); |
| 34 | + configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); |
| 35 | + configuration.setLogTemplateExceptions(false); |
| 36 | + configuration.setWrapUncheckedExceptions(true); |
| 37 | + } catch (IOException e) { |
| 38 | + e.printStackTrace(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public Template getTemplate(String templateName) { |
| 43 | + Template template = null; |
| 44 | + try { |
| 45 | + template = configuration.getTemplate(templateName); |
| 46 | + } catch (TemplateNotFoundException e) { |
| 47 | + e.printStackTrace(); |
| 48 | + } catch (MalformedTemplateNameException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } catch (ParseException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } catch (IOException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } |
| 55 | + return template; |
| 56 | + } |
| 57 | + |
| 58 | + public static String getImageStr(String imagePath) { |
| 59 | + FileInputStream inputStream = null; |
| 60 | + try { |
| 61 | + inputStream = new FileInputStream(imagePath); |
| 62 | + byte[] data = new byte[inputStream.available()]; |
| 63 | + inputStream.read(data); |
| 64 | + return data != null ? Base64.encodeBase64String(data) : ""; |
| 65 | + } catch (FileNotFoundException e) { |
| 66 | + e.printStackTrace(); |
| 67 | + } catch (IOException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } finally { |
| 70 | + IOUtils.closeQuietly(inputStream); |
| 71 | + } |
| 72 | + return ""; |
| 73 | + } |
| 74 | + |
| 75 | + public void exportDoc(String filePath, String templateName, Object data) { |
| 76 | + FileOutputStream fileOutputStream = null; |
| 77 | + OutputStreamWriter outputStreamWriter = null; |
| 78 | + BufferedWriter writer = null; |
| 79 | + try { |
| 80 | + fileOutputStream = new FileOutputStream(filePath); |
| 81 | + outputStreamWriter = new OutputStreamWriter(fileOutputStream, encoding); |
| 82 | + writer = new BufferedWriter(outputStreamWriter); |
| 83 | + getTemplate(templateName).process(data, writer); |
| 84 | + } catch (TemplateException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + } catch (IOException e) { |
| 87 | + e.printStackTrace(); |
| 88 | + } finally { |
| 89 | + IOUtils.closeQuietly(fileOutputStream); |
| 90 | + IOUtils.closeQuietly(outputStreamWriter); |
| 91 | + IOUtils.closeQuietly(writer); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments