采用jspSmartUpload组件进行文件的下载

                       刚才讲了上传的功能,现在讲讲文件的下载功能吧......

  

1.先导入jspsmartUpload的一个包

2.jsp中的代码

  

 1 <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
 2 <%@page import="java.io.File"%>
 3 <%
 4 String path = request.getContextPath();
 5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>
11     <base href="<%=basePath%>">
12     
13     <title>My JSP 'down.jsp' starting page</title>
14     
15     <meta http-equiv="pragma" content="no-cache">
16     <meta http-equiv="cache-control" content="no-cache">
17     <meta http-equiv="expires" content="0">    
18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19     <meta http-equiv="description" content="This is my page">
20     <!--
21     <link rel="stylesheet" type="text/css" href="styles.css">
22     -->
23 
24   </head>
25   
26   <body>
27     <%
28         String url=super.getServletContext().getRealPath("/")+"imges";
29         
30         File f=new File(url);//新建一个对象
31         
32         File[] files=f.listFiles();//得到所在目录下的所有的文件
33         
34         for(File file:files){
35             
36             out.print("<a href='Downservlet?fileName="+file.getName()+"'>"+file.getName()+"</a><br>");
37         }
38      %>
39   </body>
40 </html>

3.Downservlet中的java代码

  

 1 package com.zuxia.servlet;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 import com.jspsmart.upload.SmartUpload;
10 
11 public class Downservlet extends HttpServlet {
12 
13     
14     public void doGet(HttpServletRequest request, HttpServletResponse response)
15             throws ServletException, IOException {
16         doPost(request, response);
17     }
18 
19     
20     public void doPost(HttpServletRequest request, HttpServletResponse response)
21             throws ServletException, IOException {
22         
23         
24         
25         String fileName=request.getParameter("fileName");
26         
27         //Get方式设置编码的格式
28         fileName=new String(fileName.getBytes("ISO-8859-1"),"GB2312");
29         
30         //第一步:创建对象
31         SmartUpload smart=new SmartUpload();
32         
33         smart.initialize(super.getServletConfig(), request, response);//初始化
34         
35         //第三步:设置浏览器的行为(始终以附件的形式处理文件)
36         smart.setContentDisposition(null);
37         
38         try {
39             
40             //第四部:将文件下到指定的位置
41             smart.downloadFile("/imges/"+fileName);
42             
43             System.out.println("ok");
44         } catch (Exception e) {
45             // TODO: handle exception
46         }
47         
48         
49         
50         
51         
52     }
53 
54 }
原文地址:https://www.cnblogs.com/huzi007/p/2736237.html