当前位置: 移动技术网 > IT编程>开发语言>Java > jsp SmartUpload 实现上传功能代码

jsp SmartUpload 实现上传功能代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网