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

JSP 上传和下载实例 源码加数据库
中文乱码已经处理 部分数据验证
[img]http://lvp.iteye.com/upload/picture/pic/32531/58fc74b2-dcea-335b-863d-b59bb963b439.bmp[/img]
[img]http://lvp.iteye.com/upload/picture/pic/32533/0bc65b37-c155-3090-b5bc-8b08cf494135.bmp[/img]
部分源码
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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值