DWR第六篇之文件下载

1. 在第五篇架构基础上进行修改

2. 修改jsp页面

 1 <html>
 2 <head>
 3 <base href="<%=basePath%>">
 4 
 5 <title>dwr</title>
 6 <script type='text/javascript' src='dwr/engine.js'></script>
 7 <script type='text/javascript' src='dwr/util.js'></script>
 8 <script type='text/javascript' src='dwr/interface/CoreServlet.js'></script>
 9 </head>
10 <body>
11 
12     <a id="fileName" href="javascript:downloadFile('E://main.jpg');">点击下载</a>
13 
14 </body>
15 <script type="text/javascript">
16     function downloadFile(fileUrl) {
17         CoreServlet.downloadFile(fileUrl, function(data) {
18             dwr.engine.openInDownload(data);
19         });
20     }
21 </script>
22 </html>

3. 编写后台代码:

 1 public class CoreServlet {
 2 
 3     public FileTransfer downloadFile(String fileUrl) throws Exception {
 4         String fileName = fileUrl.substring(fileUrl.lastIndexOf("//") + 2, fileUrl.length());
 5         File file = new File(fileUrl);
 6         FileInputStream fis = new FileInputStream(file);
 7         return new FileTransfer(fileName, "text/plain", file.length(), fis);
 8     }
 9 
10 }
posted @ 2017-09-04 14:22  我滴个小张张  阅读(347)  评论(0编辑  收藏  举报