实现代码如下:
<%@ page contentType=”text/html;charset=gb2312″%>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″>
</head>
<body leftmargin=”0″ topmargin=”0″ bgcolor=”#ffffff”>
<form action=”upfilea.jsp” method=”post” name=”form1″
enctype=”multipart/form-data”>
<input name=”FileName” type=”FILE” size=”30″>
<input type=”submit” name=”Submit” value=”上传” >
</form>
</body>
</html>
上传页面
<%@ page contentType=”text/html;charset=gb2312″%>
<%@ page import=”java.sql.*”%>
<%@ page import=”java.util.*”%>
<%@ page import=”java.io.*”%>
<%@ page import=”com.jspsmart.upload.*” %>
<%
//实例化上载bean
com.jspsmart.upload.SmartUpload mySmartUpload=new
com.jspsmart.upload.SmartUpload();
//初始化
mySmartUpload.initialize(pageContext);
//设置上载的最大值
mySmartUpload.setMaxFileSize(500 * 1024*1024);
//上载文件
mySmartUpload.upload();
//循环取得所有上载的文件
for (int i=0;i <mySmartUpload.getFiles().getCount();i++)
{
//取得上载的文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得上载的文件的文件名
String myFileName=myFile.getFileName();
//取得不带后缀的文件名
String suffix=myFileName.substring(0,myFileName.lastIndexOf(‘.’));
//取得后缀名
String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
if(!(ext.equals(“gif”)||ext.equals(“jpg”)||ext.equals(“wmv”)
||ext.equals(“avi”)||ext.equals(“swf”))){
out.print(” <script language=’javascript’><!–
alert(‘上传失败,图片格式为
GIF或者JPG,视频格式为WMV或者AVI,falsh为swf请重试!’);
// –></script>”);
out.print(” <script language=’javascript’><!–
window.location.href =
‘uploadb.jsp’
// –></script>”);
}else
{
//取得文件的大小
int fileSize=myFile.getSize();
//保存路径
String aa=getServletContext().getRealPath(“/”)+”upload/”;
Random mynum=new Random(1);
int num=mynum.nextInt(10000);
num=(int)Math.random()*10000;
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat
(“yyMMddhhmmss”);
java.util.Date date=new java.util.Date();
String days=formatter.format(date);
days=days+num;
//System.out.println(“days=”+days);
//System.out.println(“pre=”+pre);
//取得别的参数
request.setCharacterEncoding(“GB2312”);
/*if(request.getSession(true).getAttribute(“id”)!=””)
{
id = request.getSession(true).getAttribute(“id”).toString();
}
myFile.saveAs(“upload/”+days+myFile.getFileName());
String pic=”upload/”+days+myFile.getFileName();*/
myFile.saveAs(“upload/”+days+”6.”+ext);
String pic=”upload/”+days+”6.”+ext;
session.setAttribute(“pic”,pic);
out.print(” <script
language=’javascript’><!–
parent.heheForm.pic.value='”+pic+”‘;
// –></script>”);
out.print(” <script language=’javascript’><!–
alert(‘上传成功’);
// –></script>”);
//下面的是将上载的文件保存到数据库中
//将文件保存在服务器端
//将文件写到数据库中
//response.sendRedirect(“uploada.jsp”);
}}else{out.print(” <script language=’javascript’><!–
alert(‘文件上传失
败!’);location=”
// –></script>”);}}
//与前面的if对应
%>
<html>
<head> </head>
<body leftmargin=”0″ topmargin=”0″ bgcolor=”#ffffff”>
<form>
<input type=”text” name=”pic” value=” <%=session.getAttribute(“pic”)%>”
size=”30″>
</form>
</body>
</html>
以上就是【jsp SmartUpload 实现上传功能代码】的全部内容了,欢迎留言评论进行交流!
© 版权声明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!邮箱:cxysz1@tom.com
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
丞旭猿论坛
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
丞旭猿论坛
THE END
暂无评论内容