主页 > 知识库 > jsp SmartUpload 实现上传功能代码

jsp SmartUpload 实现上传功能代码

热门标签:银行业务 服务器配置 Linux服务器 Mysql连接数设置 电子围栏 团购网站 科大讯飞语音识别系统 阿里云
复制代码 代码如下:

%@ 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 中文乱码问题解决

标签:萍乡 枣庄 江苏 大理 衡水 蚌埠 衢州 广元

巨人网络通讯声明:本文标题《jsp SmartUpload 实现上传功能代码》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266