try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filedoc),"utf-8"));
response.setContentType("application/x-msdownload;");
response.addHeader("Content-disposition", "inline; filename=myword.doc");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"utf-8"));//设置utf-8输出流字符集,编码,不设置就会有乱码
// BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path + "/" +StringUtils.getText(map.get("eventCompanyName"))+filename +".doc"),"utf-8"));//struts文档下载去配置文件里配置就ok了
String line = "";
StringBuilder strBuil = new StringBuilder();
while((line = br.readLine()) != null){
for(Map.Entry<String, String> entry: map.entrySet()) {
if(line.contains(">"+entry.getKey()) || line.contains(">("+entry.getKey())){//35个汉字一行
String getKey = entry.getKey().toString();
String getValue = map.get(getKey).toString();
strBuil.append(line.replace(entry.getKey(), map.get(entry.getKey()))).append("\n");//做个替换字符
//strBuil.append(line.replace(entry.getKey(), map.get(entry.getKey()).replace("\n", "</w:t></w:r></w:p><w:p><w:pPr><w:spacing w:line=\"480\" w:line-rule=\"auto\" /><w:ind w:first-line-chars=\"200\" /><w:rPr><w:rFonts w:ascii=\"仿宋_GB2312\" w:fareast=\"仿宋_GB2312\" w:h-ansi=\"宋体\" w:hint=\"fareast\" /><wx:font wx:val=\"仿宋_GB2312\" /><w:sz w:val=\"24\" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:ascii=\"仿宋_GB2312\" w:fareast=\"仿宋_GB2312\" w:h-ansi=\"宋体\" w:hint=\"fareast\" /><wx:font wx:val=\"仿宋_GB2312\" /><w:sz w:val=\"24\" /></w:rPr><w:t>"))).append("\n");
//word文档中存在如下内容
//w:val="24" /><w:lang w:val="EN-US" w:fareast="ZH-CN" w:bidi="AR-SA" /></w:rPr></w:style><w:style
//当map中含有key值为area时出现BUG
//所以判断条件由line.contains(entry.getKey())改为line.contains(">"+entry.getKey())
//word中xml加下划线的换行为 </w:t></w:r></w:p><w:p><w:pPr><w:wordWrap w:val=\"off\"/><w:rPr><w:rFonts w:fareast=\"仿宋_GB2312\"/><w:sz w:val=\"24\"/><w:u w:val=\"single\"/></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:fareast=\"仿宋_GB2312\" w:hint=\"fareast\"/><wx:font wx:val=\"仿宋_GB2312\"/><w:sz w:val=\"24\"/><w:u w:val=\"single\"/></w:rPr><w:t>
bflag = true;
}else{
continue;
}
}
if(bflag == false){
strBuil.append(line).append("\n");
}
bflag = false;
}
bw.write(strBuil.toString());
System.out.println(strBuil.toString());
String docPath =StringUtils.getText(map.get("eventCompanyName"))+filename + ".doc";//下载文件
br.close();
bw.close();
return docPath;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}