lvp 发表于 2013-2-7 19:29:05

JSP 上传和下载DEMO 源码加数据库

JSP 上传和下载实例 源码加数据库
中文乱码已经处理 部分数据验证
http://lvp.iteye.com/upload/picture/pic/32531/58fc74b2-dcea-335b-863d-b59bb963b439.bmp
http://lvp.iteye.com/upload/picture/pic/32533/0bc65b37-c155-3090-b5bc-8b08cf494135.bmp
部分源码
doupload.jsp
<%@ page language="java" pageEncoding="GBK"%><%@page import="com.jspsmart.upload.SmartUpload"%><%@page import="com.jspsmart.upload.Request"%><%@page import="simon.MyPathUtil"%><%@page import="simon.UploadDao"%><html><head><title>My JSP 'doupload.jsp' starting page</title></head><body><%//创建一个SmartUpload对象SmartUpload su = new SmartUpload();//上传初始化su.initialize(pageContext);//设定上传限制每个上传文件的最大长度su.setMaxFileSize(1000000000);//限制总上传数据的总长度su.setTotalMaxFileSize(2000000000);//允许上传的文件 通过扩展名限制su.setAllowedFilesList("doc,txt,jpg,rar,xls,gif,png,ppt");boolean sign = true;try {//设定禁止上传的文件 通过扩展名限制su.setDeniedFilesList("exe,bat,jsp,html,htm");//上传文件su.upload();//创建自己的Request 对象Request myRequest = su.getRequest();//得到名称String name = myRequest.getParameter("name");//得到文件后缀String ext = su.getFiles().getFile(0).getFileExt();//组成自定义的文件名称MyPathUtil util = new MyPathUtil();String fileName = util.getIPTimeRand()+"."+ext;//得到文本内容String content = myRequest.getParameter("content");//得到文件大小int longSize= su.getSize();//将上传文件保存到指定目录 // 保存文件   su.getFiles().getFile(0).saveAs(getServletContext().getRealPath("/") +"upload//"+fileName) ;UploadDao dao = new UploadDao();int res = dao.addFile(name,fileName,longSize,content);if(res<=0){sign=false;}} catch (Exception e) {sign = false;}if(sign){out.println("<script>alert('上传成功!');</script>");}else{out.println("<script>alert('上传失败!');</script>");}out.println("<script>location.href='index.jsp';</script>");%></body></html>

download.jsp
<%@ page language="java" pageEncoding="GBK"%><%@page import="com.jspsmart.upload.SmartUpload"%><%@page import="com.jspsmart.upload.Request"%><%@page import="simon.MyPathUtil"%><%@page import="simon.UploadDao"%><html><head><title>My JSP 'doupload.jsp' starting page</title></head><body><%//创建一个SmartUpload对象SmartUpload su = new SmartUpload();//上传初始化su.initialize(pageContext);//设定上传限制每个上传文件的最大长度su.setMaxFileSize(1000000000);//限制总上传数据的总长度su.setTotalMaxFileSize(2000000000);//允许上传的文件 通过扩展名限制su.setAllowedFilesList("doc,txt,jpg,rar,xls,gif,png,ppt");boolean sign = true;try {//设定禁止上传的文件 通过扩展名限制su.setDeniedFilesList("exe,bat,jsp,html,htm");//上传文件su.upload();//创建自己的Request 对象Request myRequest = su.getRequest();//得到名称String name = myRequest.getParameter("name");//得到文件后缀String ext = su.getFiles().getFile(0).getFileExt();//组成自定义的文件名称MyPathUtil util = new MyPathUtil();String fileName = util.getIPTimeRand()+"."+ext;//得到文本内容String content = myRequest.getParameter("content");//得到文件大小int longSize= su.getSize();//将上传文件保存到指定目录 // 保存文件   su.getFiles().getFile(0).saveAs(getServletContext().getRealPath("/") +"upload//"+fileName) ;UploadDao dao = new UploadDao();int res = dao.addFile(name,fileName,longSize,content);if(res<=0){sign=false;}} catch (Exception e) {sign = false;}if(sign){out.println("<script>alert('上传成功!');</script>");}else{out.println("<script>alert('上传失败!');</script>");}out.println("<script>location.href='index.jsp';</script>");%></body></html>

showall.jsp下载的页面
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@page import="simon.UploadDao"%><%@page import="simon.MyFile"%><html><head>    <title>显示所有的下载资源</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><link rel="stylesheet" type="text/css" href="css/styles.css"></head><body>   <div id="mainDiv">   <h3 align="center">资源下载页面</h3>   <form action="dosearch.jsp" method="post" name="myform">   <div style="display:inline;margin-right:650px;">   <a href="index.jsp">上传资源</a>   </div>      </form>   <hr>   <%   UploadDao dao = new UploadDao();   ArrayList<MyFile> list = dao.getAlList();          %>   <table width="80%" align="center" border="0" padding="2">   <tr>   <td width="5%">编号</td>   <td width="15%">名称<td>   <td width="40%">描述</td>   <td width="10%">大小</td>   <td width="20%">上传时间</td>   <td width="10%"> </td>   </tr>   <%   for(int i=0;i<list.size();i++){   MyFile file = list.get(i);   %>   <tr>   <td width="5%"><%=i+1 %></td>   <td width="20%"><%=file.getName() %><td>   <td width="40%"><%=file.getNote() %> </td>   <td width="5%"><%=file.getSize()/1024 %> KB</td>   <td width="20%"><%=file.getUploadTime() %></td>   <td width="10%">   <a href="download.jsp?file=<%=file.getThePath()%>">下载</a>   </td>   </tr>   <%   }    %>   </table>   </div></body></html>
页: [1]
查看完整版本: JSP 上传和下载DEMO 源码加数据库