/**
* 通用文件下载
*/
public static void commonDownLoadFile(File file, String name, HttpServletResponse response) throws IOException {
//设置以流的形式下载文件
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes("utf-8"), "ISO8859-1"));
response.setContentLength((int) file.length());
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
byte[] buffer = new byte[128];
int count = 0;
while ((count = fis.read(buffer)) > 0) {
response.getOutputStream().write(buffer, 0, count);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
response.getOutputStream().flush();
response.getOutputStream().close();
fis.close();
}
}
/**
* 将xml渲染到doc
*
* @param dataMap 参数
* @param fileName 废弃暂时不用
* @param xmlFileName xml文件名称
* @return File
*/
public static File genWord2ByMap(Map<String, String> dataMap, String fileName, String xmlFileName) {
try {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDefaultEncoding("utf-8");
//这个需要部署到测试站
configuration.setClassForTemplateLoading(WordUtil.class, "/templates");
URL resource = ClassUtils.getDefaultClassLoader().getResource("");
URL url = new URL(resource.toString());
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
File outFile = FileUtil.inputStreamToFile(inputStream, "test.doc");
Template t = configuration.getTemplate(xmlFileName, "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close();
System.out.println("生成成功");
return outFile;
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
System.out.println("生成失败");
}
return null;
}
public static File genWordByMap(Map<String, Object> dataMap, String fileName, String xmlFileName) {
try {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDefaultEncoding("utf-8");
//这个需要部署到测试站
configuration.setClassForTemplateLoading(WordUtil.class, "/templates");
URL resource = ClassUtils.getDefaultClassLoader().getResource("");
URL url = new URL(resource.toString());
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
File outFile = FileUtil.inputStreamToFile(inputStream, "test.doc");
Template t = configuration.getTemplate(xmlFileName, "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close();
System.out.println("生成成功");
return outFile;
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
System.out.println("生成失败");
}
return null;
}
public static File genWord3(Map<String, Object> dataMap) {
try {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDefaultEncoding("utf-8");
//这个需要部署到测试站
configuration.setClassForTemplateLoading(WordUtil.class, "/templates");
URL resource = ClassUtils.getDefaultClassLoader().getResource("");
URL url = new URL(resource.toString());
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
File outFile = FileUtil.inputStreamToFile(inputStream, "test.doc");
Template t = configuration.getTemplate("xxxx表.xml", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close();
System.out.println("生成成功");
return outFile;
} catch (Exception e) {
log.error(e.getMessage());
e.printStackTrace();
System.out.println("生成失败");
}
return null;
}
public static MultipartFile createMfileByFile(File file) {
MultipartFile mFile = null;
try {
FileInputStream fileInputStream = new FileInputStream(file);
String fileName = file.getName();
fileName = fileName.substring((fileName.lastIndexOf("/") + 1));
mFile = new MockMultipartFile(fileName, fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
} catch (Exception e) {
log.error("封装文件出现错误:{}", e);
//e.printStackTrace();
}
return mFile;
}
@PostMapping("/test")标签:文件,word,File,file,new,utf,configuration,out From: https://www.cnblogs.com/wjsqqj/p/17728944.html
public R test3(HttpServletResponse response) throws IOException {
Map<String, Object> map = Maps.newHashMap();
map.put("applyOrgName", "apply.getApplyOrgName()");
map.put("applyBasis", "apply.getApplyOrgName()");
map.put("appsceneName", "apply.getApplyOrgName()");
//事项清单是列表 需要看doc怎么生成数据 暂时处理成第一个额外处理
//List<String> matterList = Lists.newArrayList("1", "222", "33333", "44445");
List<String> matterList = Lists.newArrayList("1");
map.put("matterName", matterList.get(0));
File file = WordUtil.genWord3(map);
MultipartFile multipartFile = WordUtil.createMfileByFile(file);
FileUtil.commonDownLoadFile(file, file.getName(), response);
return new R<>(multipartFile.getInputStream());
}