采用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/archive/2012/10/23/2736237.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值