復(fù)制代碼 代碼如下:
服務(wù)器端:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
req.setCharacterEncoding("GBK");
// 新建一個(gè)SmartUpload對象
SmartUpload su = new SmartUpload();
su.initialize(this.getServletConfig(),req,resp);
// 設(shè)定contentDisposition為null以禁止瀏覽器自動(dòng)打開文件,
//保證點(diǎn)擊鏈接后是下載文件。若不設(shè)定,則下載的文件擴(kuò)展名為
//doc時(shí),瀏覽器將自動(dòng)用word打開它。擴(kuò)展名為pdf時(shí),
//瀏覽器將用acrobat打開。
su.setContentDisposition(null);
//下載文件
try {
su.downloadFile("D://upload/"+"test.txt");
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
復(fù)制代碼 代碼如下:
客戶端:
script>
function getFile(){
$.get("%=basePath%>LoadFileServlet","",function(data){
alert(data);
});
}
/script>
a href="#" onclick="getFile()">getfile/a>
txt中的中文內(nèi)容不顯示,如果通過超鏈接訪問server端,下載后沒有亂碼
先MARK下,以后再看看