---
title: "JSP 上传和下载DEMO 源码加数据库"
author: "lvp"
date: "2009-03-04T22:16:12Z"
tags: []
slug: "jsp-demo"
draft: false
lastmod: "2009-04-07T23:38:53Z"
id: 343239
url: "https://lvp.iteye.com/blog/343239"
---

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

```java

<%@ 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

```java

<%@ 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  下载的页面

```java

<%@ 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%">&nbsp;</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>

```