jsp SmartUpload 实现上传功能代码

复制代码 代码如下:

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