zoukankan      html  css  js  c++  java
  • 采用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 }
  • 相关阅读:
    MIME 部分扩展名与类型对应
    sql server 表变量、表类型、临时表
    SqlBulkCopy使用注意事项
    SQL Server为啥使用了这么多内存?
    SQL SERVER下有序GUID和无序GUID作为主键&聚集索引的性能表现
    DQL、DML、DDL、DCL的概念与区别
    IIS解决CPU和内存占用率过高的问题
    SQL Server 表变量和临时表的区别
    I Count Two Three(打表+排序+二分查找)
    AC自动机入门经典题目(两种表达方式)
  • 原文地址:https://www.cnblogs.com/huzi007/p/2736237.html
Copyright © 2011-2022 走看看