JspSmartUpload使用笔记

来源:互联网 发布:环迅网络 编辑:程序博客网 时间:2024/05/22 16:07
SmartUpload使用过程中出现的错误!

 

1.参数传送时,Form的必须加上以下属性:

enctype=multipart/form-data

2.因为SmartUpload组件已经加载了java.io.* 包,页面再次加载将被覆盖,要使用java.io包的方法无法使用,比如创建文件夹,目前还不知道很好的解决方法!!!

3.使用:

<%@ page import="com.jspsmart.upload.*"%>//加载

<%
      SmartUpload  mySmartUpload = new SmartUpload();
       mySmartUpload.initialize(pageContext);
    mySmartUpload.upload();
%>//初始化

//上传

<%
  final String UpFileType="pdf|doc";        //上传文件类型
  final int MaxFileSize=102400;        //上传文件大小限制
  boolean ERR=false;     //错误标志
  // 取当前文件
  com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
 

 //文件效验
  // 取得文件并保存
  if (!myFile.isMissing())
  {
   //得到文件扩展名
   String FileType=myFile.getFileExt();
   FileType=FileType.toLowerCase();   //将扩展名转换成小写
   if (UpFileType.indexOf(FileType)==-1)
   {
    ERR=true;  
    response.sendRedirect("xwb_error.jsp?err=type");//文件格式不对
   }  
   //得到文件大小
   int FileSize=myFile.getSize();
   if (ERR==false&&FileSize>MaxFileSize)
   {
    ERR=true;
       response.sendRedirect("xwb_error.jsp?err=size");//文件太大
   }  
   
  }
  else
  {
   response.sendRedirect("xwb_error.jsp?err=noext");
  }

//上传文件
   String filename="";
       filename=myFilegetFileName();
    out.println(filename);
 try{
      if (!myFile.isMissing())

        {
       filename=myFile.getFileName();
                     java.util.Date date=new java.util.Date();
                     long time=date.getTime();
                     String title=String.valueOf(time);
                     String extn=filename.substring(filename.lastIndexOf('.'),filename.length());
                     filename=title+extn;
     
                    myFile.saveAs("/test/wangzhe/filetest/"+filename);
       }   //END OF IF 
   }

catch (Exception e)

{
   out.println("<b>Wrong selection : </b>" + e.toString());
      }

 

%>

原创粉丝点击