JAVA利用FreeMarker產生(匯出)Word文檔(Bug修改)__java

來源:互聯網
上載者:User
Bug一:把代碼放到伺服器上,能下載Word檔案;但是,下載到伺服器的C盤上了;
 解決:
 修改部分export.js代碼:
 function table01(){
var stime = $('#day').val();
var etime = $('#end').val();
$.ajax({
type:"POST",
url:path+"/yuqing/table01",
data:{
stime:stime,
etime : etime
},
cache:false,
dataType:"json",
async:false,
success:function(data){
downword();
}
});
}
function downword(){
var url = path + '/indexdownload/downword';
window.location.href=encodeURI(url);
}
在com.yuanls.index.controller.IndexDownloadController.java 中:(部分代碼:)
@Controller
@RequestMapping(value = "/indexdownload")
@SuppressWarnings("serial")
public class IndexDownloadController extends BaseController{
@ResponseBody
@RequestMapping(value = "/downword")
public void downword(HttpServletRequest request,HttpServletResponse response) throws Exception {
response.setCharacterEncoding("utf-8");
//String name = request.getParameter("filename");
String name = "1.doc";
//name = URLDecoder.decode(name, "UTF-8");
//if (name.lastIndexOf(".htm") == name.length() - 4) {
//name = name.substring(0, name.length() - 4) + ".doc";
//}
// String webPath=IndexDownloadUtils.getPathWeb()+name;
//name = name + ".doc";
Properties pro = IndexDownloadUtils.getProperties("zd.properties");
String zdpath = pro.get("zdpath").toString();
zdpath = zdpath + "yqjcword/";
//zdpath = zdpath + "qxfw/";
String webPath=zdpath+name;
File f=new File(webPath);
if(!f.exists()){
PrintWriter out=response.getWriter();
out.print("<script>alert('file is not exists。');history.back();</script>");
}else{
String fileNamePath = name.replace("\\", "/");
String aa = fileNamePath.substring(fileNamePath.lastIndexOf("/") + 1,
fileNamePath.length());
// String[] files=aa.split("\\.");
String fileName = aa.substring(0, aa.lastIndexOf("."));
String suffix = aa.substring(aa.lastIndexOf("."));
BufferedOutputStream write = null;
BufferedInputStream read = null;
try {
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("gb2312"), "ISO8859-1")
+ suffix);
// response.addHeader("Content-Disposition",
// "attachment;filename=" + new String(files[0].getBytes("gb2312"),
// "ISO8859-1") 
// + "."+files[1]);
// String webpath = IndexDownloadUtils.getPathWeb();
//String webpath = "E:/zd/qxfw/";
String webpath = "E:/zd/yqjcword/";
File file = new File(webpath + name);
byte[] by = new byte[1024];
int i;
read = new BufferedInputStream(new FileInputStream(file));
write = new BufferedOutputStream(response.getOutputStream());
while (-1 != (i = read.read(by, 0, by.length))) {
write.write(by, 0, i);
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception("系統找不到指定的檔案");
} finally {
try {
if (write != null) {
write.flush();
write.close();
}
if (write != null)
read.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
}
註:以上這樣寫代碼,在下載的時候可以選擇任意盤符和檔案夾,還可以修改檔案名稱;
 
 Bug二:下載到C盤上的Word檔案打不開;報錯:XML 非法字元;
 解決:
把下面這一行: 
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); 設定編碼
改成下面這一行:

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));



我們在做模板匯出時需要注意以下三處編碼集的設定,我的中文亂碼問題是因為第三處沒有設定引起的。

(1)configuration.setDefaultEncoding("UTF-8");
(2)Template t = configuration.getTemplate("模板檔案","UTF-8");
(3)Writer out = new BufferedWriter(new OutputStreamWriter(檔案輸出資料流 fos, "UTF-8"))。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.