最近客户在用的项目中,分页页面在导出excel抛出
java.lang.IllegalArgumentException: URLDecoder: Incomplete trailing escape (%) pattern
该页面采用的是DWR分页,经过一番搜罗,终于修成正果.解决办法
大致意思都懂了,我们只需要将传入后台的参数字符在decode之前使用replaceAll('%','%25')一下即可
try {
pageTitle = java.net.URLDecoder.decode(pageTitle,"UTF-8");
sc = java.net.URLDecoder.decode(sc ,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
修正后代码如下:
try {
pageTitle = java.net.URLDecoder.decode(pageTitle.replaceAll("%", "%25"),"UTF-8");
sc = java.net.URLDecoder.decode(sc.replaceAll("%", "%25") ,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
特别注意:
有些时候导出excel时采用的是get方式导致URL字符串长度过长,改用POST方式可以解决以上问题。
使用js实现POST表单提交代码片段:
function post(URL, PARAMS) { //创建一个临时表单 var tempForm = document.createElement("form"); tempForm.action = URL; tempForm.method = "post"; tempForm.style.display = "none"; //遍历各个参数,将文本域添加至表单中 for (var x in PARAMS) { var opt = document.createElement("textarea"); opt.name = x; opt.value = PARAMS[x]; tempForm.appendChild(opt); } //将表单添加至当前页面中. document.body.appendChild(tempForm); //提交表单. tempForm.submit(); }
也可参照此方法解决:http://blog.csdn.net/zhensoft163/article/details/7298161