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"))。