SmartUpload组件实现文件的上传和下载

上传:

upload.jsp:

<%@ page language="java" import="java.io.*" contentType="text/html;Charset=GBK"%>
<html>
 <head>
  <title>列出目录下所有文件下载</title>
 </head>
 <body>
  <form action="upload_do.jsp" method="post" enctype="multipart/form-data">
   文件一:<input type="file" name="file1"><br>
   文件二:<input type="file" name="file2"><br>
   文件三:<input type="file" name="file3"><br>
   <input type="submit" value="上传">
  </form>
 </body>
</html>
upload_do.jsp:
<%@ page language="java" import="com.jspsmart.upload.*,java.io.*" contentType="text/html;Charset=GBK"%>
<html>
 <head>
  <title>文件下载</title>
 </head>
 <body>
 <%
  SmartUpload su = new SmartUpload() ;
  su.initialize(pageContext) ;
  su.upload() ;
  try{
   su.save(this.getServletContext().getRealPath("/")+"upload") ;
  }catch(Exception e){
   e.printStackTrace() ;
  }
 %>
 </body>
</html>

下载:
download.jsp:

<%@ page language="java" import="java.io.*" contentType="text/html;Charset=GBK"%>
<html>
 <head><title>批量文件下载</title></head>
 <body>
  <%
   String path = request.getParameter("path") ;
   int count = 1 ;
   try{
    File dir = new File(path) ;
    if(!dir.exists()||!dir.isDirectory()){
  %>
   <h3><font color="red">输入的目录无效!</font><h3>
  <%
    }
    File[] files = dir.listFiles() ;
    for(int i=0;i<files.length;i++){
     if(files[i].isFile()){
  %>
    <%=count++%>、<a href="download_do.jsp?file=<%=path+File.separator+files[i].getName()%>"><%=files[i].getName()%></a><br>
  <%
     }
    }
   }catch(Exception e){}
  %>
  <form action="" method="post">
   下载目录路径:<input type="text" name="path">(格式:d:\test)<br>
   <input type="submit" value="确定">
  </form> 
 </body>
</html>


download_do.jsp:

<%@ page language="java" import="com.jspsmart.upload.*" contentType="text/html;Charset=GBK"%>
<html>
 <head><title>批量文件下载</title></head>
 <body>
  <%
   String file = request.getParameter("file") ;
   SmartUpload upload = new SmartUpload() ;
   response.reset() ;
   out.clear() ;
   out = pageContext.pushBody() ;
   upload.initialize(pageContext) ;
   try{
    upload.setContentDisposition(null) ;
    upload.downloadFile(file) ;
   }catch(Exception e){}
  %>
 </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值