上传:
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>