1、靜態(tài)html文件中,可以是任意元素的點(diǎn)擊事件觸發(fā)下面的js腳本程序。解決了firefox和ie的文件名亂碼的問題。
2、js文件:
function loadMobileClient(){
var url = "mobileClient/loadMobileClient.htm?";
window.location.href=url + 'filename=' + encodeURIComponent("19.wmv");
}
3、java文件:
public ModelAndView loadMobileClient(HttpServletRequest request, HttpServletResponse response){
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String filename=request.getParameter("filename");
File file=new File("d:/"+filename);
response.setCharacterEncoding("UTF-8");
//寫明要下載的文件的大小
response.setContentLength((int)file.length());
response.reset();
response.setContentType("application/octet-stream");
String agent = (String)request.getHeader("USER-AGENT");
if(agent != null && agent.indexOf("MSIE") == -1) {
// FF
String enableFileName = "";
try {
enableFileName = new String(filename.getBytes("UTF-8"),"ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment; filename=" + enableFileName);
} else {
try {
response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
response.setHeader("Connection", "close");
try {
FileInputStream fis=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);
byte [] b=new byte[1024];//
long k=0;
OutputStream myout=response.getOutputStream();
while(k<file.length()){
int j=buff.read(b,0,1024);
k+=j;
myout.write(b,0,j);
}
myout.flush();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
已測(cè)試,完成!
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。