java打包下载多个word
1.jar包
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
2.工具类
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import sun.misc.BASE64Encoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;
public class WordUtil {
private Configuration configuration;
private String encoding;
public WordUtil(String encoding) {
this.encoding = encoding;
configuration = new Configuration(Configuration.VERSION_2_3_22);
configuration.setDefaultEncoding(encoding);
//configuration.setClassForTemplateLoading(this.getClass(), "/templates/");
try {
//configuration.setDirectoryForTemplateLoading(new File("G:\\ideaWorkspace\\zwxtzhzx\\src\\main\\java\\com\\sx\\common\\wordexp\\templates"));
//请将模板文件放在resource文件夹下的templates文件夹中
configuration.setDirectoryForTemplateLoading(new File(this.getClass().getResource("/").getPath() + "templates"));
} catch (IOException e) {
e.printStackTrace();
}
}
public Template getTemplate(String name) throws Exception {
return configuration.getTemplate(name);
}
public String getImageStr(String image) throws IOException {
InputStream is = new FileInputStream(image);
BASE64Encoder encoder = new BASE64Encoder();
byte[] data = new byte[is.available()];
is.read(data); is.close();
return encoder.encode(data);
}
/** * 对文件流输出下载的中文文件名进行编码 屏蔽各种浏览器版本的差异性 * @param request * @param pFileName * @return * @throws UnsupportedEncodingException */
public static String encodeChineseDownloadFileName(
HttpServletRequest request, String pFileName) throws Exception {
String filename = null;
String agent = request.getHeader("USER-AGENT");
if (null != agent){
if (-1 != agent.indexOf("Firefox")) {
//Firefox
filename = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(pFileName.getBytes("UTF-8"))))+ "?=";
}else if (-1 != agent.indexOf("Chrome")) {
//Chrome
filename = new String(pFileName.getBytes(), "ISO8859-1");
} else {
//IE7+
filename = java.net.URLEncoder.encode(pFileName, "UTF-8");
filename = filename.replace("+", "%20");
}
} else {
filename = pFileName;
}
return filename;
}
/** * 通过模板获取word流 * @param modelName 模板文件名(包含后缀ftl) * @param dataMap 数据Map */
public ByteArrayOutputStream wordBaos(String modelName, Map dataMap){
ByteArrayOutputStream byteOut = null;
Writer writer = null;
try {
byteOut = new ByteArrayOutputStream();
Template template = this.getTemplate(modelName);
writer = new OutputStreamWriter(byteOut, "UTF-8");
template.process(dataMap, writer);
return byteOut;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(writer != null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/** * 将多个文件流合并到压缩包下载 * @param baoss 字节输出流数组 * @param fileNames 文件名数组 * @param response HttpServletResponse */
public void downLoadZip(ByteArrayOutputStream[] baoss, String[] fileNames, String zipName, HttpServletRequest request, HttpServletResponse response){
//压缩包输出流
ZipArchiveOutputStream zous = null;
try {
zous = new ZipArchiveOutputStream(response.getOutputStream());
zous.setUseZip64(Zip64Mode.AsNeeded);
//response.setContentType("application/octet-stream");
//编码文件名并加上后缀(压缩包内部文件名不需要编码)
String filename = encodeChineseDownloadFileName(request, zipName) + ".zip";
response.setHeader("Content-disposition", "attachment; filename=" + filename);
for(int i = 0; i < baoss.length; i++){
ByteArrayOutputStream baos = baoss[i];
//给文件名
String fileName = fileNames[i];
//下面三行是吧excel的文件以流的形式转为byte[]
byte[] bytes = baos.toByteArray();
ArchiveEntry entry = new ZipArchiveEntry(fileName);
zous.putArchiveEntry(entry);
zous.write(bytes);
zous.closeArchiveEntry();
if (baos != null) {
baos.close();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(zous != null){
zous.flush();
zous.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.制作导出word模板,然后另存为xml。
放到resources》templates下(必须是),修改后缀为ftl。
4.测试
function wordPro() {
$("#form1").attr("action","<%=basePath%>stu/wordPro");
$("#form1").submit();
}
@RequestMapping("/wordPro")
public void wordPro(HttpServletRequest request, HttpServletResponse response){
try{
WordUtil maker = new WordUtil("UTF-8");
String zipName = "压缩包名";
ByteArrayOutputStream[] baoss= new ByteArrayOutputStream[2];
String[] str = new String[2];
str[0]="word1--文件名1.doc";//后缀一定加
str[1]="word2--文件名2.doc";
Map<String, Object> data1 = new HashMap<String, Object>();
Map<String, Object> data2 = new HashMap<String, Object>();
Stu stu1=new Stu();
stu1.setStuName("小明");
stu1.setStuSex("男");
stu1.setStuMz("汉族");
stu1.setStuGj("中国--1");
stu1.setStuPhone("110110");
Stu stu2=new Stu();
stu2.setStuName("小花");
stu2.setStuSex("女");
stu2.setStuMz("维吾尔族");
stu2.setStuGj("中国--2");
stu2.setStuPhone("110110");
data1.put("vbs",stu1);
data2.put("vbs",stu2);
baoss[0] = maker.wordBaos("word.ftl", data1);
baoss[1] = maker.wordBaos("word.ftl", data2);
maker.downLoadZip(baoss,str, zipName, request, response);
}catch (Exception e){
e.printStackTrace();
}
}
5.成功
今天的文章java打包下载多个文件_java将多个文件打包成zip分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/89124.html