欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 前端技术 > javascript >内容正文

javascript

jsp上传组件 -凯发ag旗舰厅登录网址下载

发布时间:2024/10/8 javascript 0 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 jsp上传组件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
第1个上传组件commons-fileupload
=============commons-fileupload ================

common-fileupload组件是apache的一个开源项目之一,可以从http://jakarta.apache.org/commons/fileupload/下载。该组件简单易用,可实现一次上传一个或多个文件,并可限制文件大小。
-下载后解压zip包,将commons-fileupload-1.1.1.jar,和commons-io-1.2.jar复制到tomcat的webapps\你的webapp\web-inf\lib\下,如果目录不存在请自建目录。
新建一个servlet: fileupload.java用于文件上传:

package com.drp.util.servlet;

import java.io.ioexception;
import java.io.printwriter;

import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

import org.apache.commons.fileupload.*;
import java.util.*;
import java.util.regex.*;
import java.io.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.diskfileitemfactory;

public class fileupload extends httpservlet {

 private string uploadpath = ""; // 用于存放上传文件的目录
 
 private file temppath = new file("d:\\tomcat 5.5\\webapps\\drp1.2\\tempimages\\"); // 用于存放临时文件的目录

 public void dopost(httpservletrequest req, httpservletresponse res)
   throws servletexception, ioexception {
  res.setcontenttype("text/html; charset=gb18030");
  printwriter out = res.getwriter();
  system.out.println(req.getcontentlength());
  system.out.println(req.getcontenttype());
  diskfileitemfactory factory = new diskfileitemfactory();
  // maximum size that will be stored in memory
  //允许设置内存中存储数据的门限,单位:字节
  factory.setsizethreshold(4096);
  // the location for saving data that is larger than getsizethreshold()
 
 //如果文件大小大于sizethreshold,则保存到临时目录
  factory.setrepository(new file("d:\\tomcat 5.5\\webapps\\drp1.2\\tempimages"));

  servletfileupload upload = new servletfileupload(factory);
  // maximum size before a fileuploadexception will be thrown
 
 //最大上传文件,单位:字节
  upload.setsizemax(1000000);
  try {
   list fileitems = upload.parserequest(req);
   // assume we know there are two files. the first file is a small
   // text file, the second is unknown and is written to a file on
   // the server
   iterator iter = fileitems.iterator();

   // 正则匹配,过滤路径取文件名
   string regexp = ". \\\\(. )$";

   // 过滤掉的文件类型
   string[] errortype = { ".exe", ".com", ".cgi", ".asp" };
   pattern p = pattern.compile(regexp);
   string itemno = "";//文件存放路径
   while (iter.hasnext()) {
    fileitem item = (fileitem) iter.next();
    
  
  // 忽略其他不是文件域的所有表单信息
    if (!item.isformfield()) {
     string name = item.getname();
     long size = item.getsize();
     if ((name == null || name.equals("")) && size == 0)
      continue;
     matcher m = p.matcher(name);
     boolean result = m.find();
     if (result) {
      for (int temp = 0; temp < errortype.length; temp ) {
       if (m.group(1).endswith(errortype[temp])) {
        throw new ioexception(name ": wrong type");
       }
      }
      try {

       // 保存上传的文件到指定的目录

       // 在下文中上传文件至数据库时,将对这里改写
         item.write(new file("d:\\" m.group(1)));

                                                          out.print(name "  " size "
");


      } catch (exception e) {
       out.println(e);
      }

     } else {
      throw new ioexception("fail to upload");
     }
    }
   }
  } catch (ioexception e) {
   out.println(e);
  } catch (fileuploadexception e) {
   out.println(e);
  }
 }

 public void init() throws servletexception {
  this.uploadpath = this.getservletconfig().getinitparameter("upload_path");//
的到web.xml中的配置文件用于保存上传文件,也可以在已开始定义的时候初始化,不过这样可以通过改动配置文件来改动存放路径,不用该代码,增加了灵活性。
 }
}

web.xml中相应的配置如下:

 
        fileupload
        com.drp.util.servlet.fileupload//注意路径

  
 upload_path
 d:\\tomcat 5.5\\webapps\\drp1.2\\images\\item\\//存放地址  

 

 

    fileupload
    >/servlet/fileupload

 

对应的请求文件:



 
    index.html

   
   

   

 

 
   //注意action地址,还有enctype要写成multipart/form-data,和method="post"   

       

       

       

       

       

                        文件1:

       

                        文件2:

       

                        文件3:

       

       

       

       

      

 

第二个组件smartupload
=================smartupload=================================================
smartupload.zip 下载地址: http://dev2dev.bea.com.cn/bbs/servlet/d2dservlet/download/121-20468-118409-1176/smartupload.zip
解压后有6个主要文件:servletupload.java,smartfile.java,smartfiles.java,smartrequest.java,smartupload.java,  smartuploadexception.java 将其放在你的javabeen目录下,注意每个文件的包名和你的存放位置是否相否,
  由于不能传中文,根据网站上的介绍对其中的smartupload.java做修改添加toutf8string(string s)方法:
  改动后smartupload.java的全部代码如下:红色代表改动的部分:
package com.drp.upload;//写你自己的包名

import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.vector;
import javax.servlet.servletconfig;
import javax.servlet.servletcontext;
import javax.servlet.servletexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
import javax.servlet.jsp.jspwriter;
import javax.servlet.jsp.pagecontext;

public class smartupload
{
    protected byte m_binarray[];
    protected httpservletrequest m_request;
    protected httpservletresponse m_response;
    protected servletcontext m_application;
    private int m_totalbytes;
    private int m_currentindex;
    private int m_startdata;
    private int m_enddata;
    private string m_boundary;
    private long m_totalmaxfilesize;
    private long m_maxfilesize;
    private vector m_deniedfileslist;
    private vector m_allowedfileslist;
    private boolean m_denyphysicalpath;

    //private boolean m_forcephysicalpath;
    private string m_contentdisposition;
    public static final int save_auto = 0;
    public static final int save_virtual = 1;
    public static final int save_physical = 2;
    private smartfiles m_files;
    private smartrequest m_formrequest;

    public smartupload()
    {
        m_totalbytes = 0;
        m_currentindex = 0;
        m_startdata = 0;
        m_enddata = 0;
        m_boundary = ""; //new string();
        m_totalmaxfilesize = 0l;
        m_maxfilesize = 0l;
        m_deniedfileslist = new vector();
        m_allowedfileslist = new vector();
        m_denyphysicalpath = false;
        //m_forcephysicalpath = false;
        m_contentdisposition = ""; //new string();
        m_files = new smartfiles();
        m_formrequest = new smartrequest();
    }

    /**
     * @deprecated method init is deprecated
     */
    public final void init(servletconfig servletconfig) throws servletexception
    {
        m_application = servletconfig.getservletcontext();
    }

    /**
     * @deprecated method service is deprecated
     */
    public void service(httpservletrequest httpservletrequest,httpservletresponse httpservletresponse) throws

servletexception,ioexception
    {
        m_request = httpservletrequest;
        m_response = httpservletresponse;
    }

    public final void initialize(servletconfig servletconfig,httpservletrequest

httpservletrequest,httpservletresponse httpservletresponse) throws servletexception
    {
        m_application = servletconfig.getservletcontext();
        m_request = httpservletrequest;
        m_response = httpservletresponse;
    }

    public final void initialize(pagecontext pagecontext) throws servletexception
    {
        m_application = pagecontext.getservletcontext();
        m_request = (httpservletrequest)pagecontext.getrequest();
        m_response = (httpservletresponse)pagecontext.getresponse();
    }

    /**
     * @deprecated method initialize is deprecated
     */
    public final void initialize(servletcontext servletcontext,httpsession httpsession,httpservletrequest

httpservletrequest,httpservletresponse httpservletresponse,jspwriter jspwriter) throws servletexception
    {
        m_application = servletcontext;
        m_request = httpservletrequest;
        m_response = httpservletresponse;
    }

    public void upload() throws servletexception,ioexception,smartuploadexception
    {
        int i = 0;
        //boolean flag = false;
        boolean flag1 = false;
        //boolean flag2 = false;
        long l = 0l;
        //string s = "";//new string();
        //string s2 = "";//new string();
        string s4 = ""; //new string();
        string s5 = ""; //new string();
        string s6 = ""; //new string();
        string s7 = ""; //new string();
        string s8 = ""; //new string();
        string s9 = ""; //new string();
        string s10 = ""; //new string();
        m_totalbytes = m_request.getcontentlength();
        m_binarray = new byte[m_totalbytes];
        int j;
        for(;i < m_totalbytes;i = j)
        {
            try
            {
                m_request.getinputstream();
                j = m_request.getinputstream().read(m_binarray,i,m_totalbytes - i);
            }
            catch(exception exception)
            {
                throw new smartuploadexception("unable to upload.");
            }
        }

        for(;!flag1 && m_currentindex < m_totalbytes;m_currentindex )
        {
            if(m_binarray[m_currentindex] == 13)
            {
                flag1 = true;
            }
            else
            {
                m_boundary = m_boundary (char)m_binarray[m_currentindex];

            }
        }
        if(m_currentindex == 1)
        {
            return;
        }
        for(m_currentindex ;m_currentindex < m_totalbytes;m_currentindex = m_currentindex 2)
        {
            string s1 = getdataheader();
            m_currentindex = m_currentindex 2;
            boolean flag3 = s1.indexof("filename") > 0;
            string s3 = getdatafieldvalue(s1,"name");
            if(flag3)
            {
                s6 = getdatafieldvalue(s1,"filename");
                s4 = getfilename(s6);
                s5 = getfileext(s4);
                s7 = getcontenttype(s1);
                s8 = getcontentdisp(s1);
                s9 = gettypemime(s7);
                s10 = getsubtypemime(s7);
            }
            getdatasection();
            if(flag3 && s4.length() > 0)
            {
                if(m_deniedfileslist.contains(s5))
                {
                    throw new securityexception("the extension of the file is denied to be uploaded (1015).");
                }
                if(!m_allowedfileslist.isempty() && !m_allowedfileslist.contains(s5))
                {
                    throw new securityexception("the extension of the file is not allowed to be uploaded

(1010).");
                }
                if(m_maxfilesize > 0l && (long)((m_enddata - m_startdata) 1) > m_maxfilesize)
                {
                    throw new securityexception("size exceeded for this file : " s4 " (1105).");
                }
                l = (m_enddata - m_startdata) 1;
                if(m_totalmaxfilesize > 0l && l > m_totalmaxfilesize)
                {
                    throw new securityexception("total file size exceeded (1110).");
                }
            }
            if(flag3)
            {
                smartfile file = new smartfile();
                file.setparent(this);
                file.setfieldname(s3);
                file.setfilename(s4);
                file.setfileext(s5);
                file.setfilepathname(s6);
                file.setismissing(s6.length() == 0);
                file.setcontenttype(s7);
                file.setcontentdisp(s8);
                file.settypemime(s9);
                file.setsubtypemime(s10);
                if(s7.indexof("application/x-macbinary") > 0)
                {
                    m_startdata = m_startdata 128;
                }
                file.setsize((m_enddata - m_startdata) 1);
                file.setstartdata(m_startdata);
                file.setenddata(m_enddata);
                m_files.addfile(file);
            }
            else
            {
                string s11 = new string(m_binarray,m_startdata,(m_enddata - m_startdata) 1);
                m_formrequest.putparameter(s3,s11);
            }
            if((char)m_binarray[m_currentindex 1] == '-')
            {
                break;
            }
        }
    }

    public int save(string s) throws servletexception,ioexception,smartuploadexception
    {
        return save(s,0);
    }

    public int save(string s,int i) throws servletexception,ioexception,smartuploadexception
    {
        int j = 0;
        if(s == null)
        {
            s = m_application.getrealpath("/");
            //system.out.println("s == null,m_application.getrealpath:" s);
        }
        if(s.indexof("/") != -1)
        {
            if(s.charat(s.length() - 1) != '/')
            {
                s = s "/";
                //system.out.println("m_application.getrealpath::" s);
            }
        }
        else
        {
            if(s.charat(s.length() - 1) != '\\')
            {
                s = s "\\";
                //system.out.println("m_application.getrealpath" s);
            }
        }
        //system.out.println("m_application.getrealpath:::" s);
        filenames = new string[m_files.getcount()];
        for(int k = 0;k < m_files.getcount();k )
        {
            if(!m_files.getfile(k).ismissing())
            {
                //system.out.println("s m_files.getfile(k).getfilename():" s m_files.getfile

(k).getfilename());
                m_files.getfile(k).saveas(s m_files.getfile(k).getfilename(),i);
                filenames[j] = s m_files.getfile(k).getfilename();
                j ;
            }
        }

        return j;
    }

    //add
    private string[] filenames;
    public string[] getfilenames()
    {
        //method may expose internal representation by returning array
        //returning an array value stored in one of the object's fields exposes the internal representation of

the object.? for classes shared by other untrusted classes, this could potentially be a security issue.?

returning a new copy of the array is better approach in many situations.
        string[] vfilenames = new string[filenames.length];
        system.arraycopy(filenames,0,vfilenames,0,filenames.length);
        return vfilenames;
    }

    public int getsize()
    {
        return m_totalbytes;
    }

    public byte getbinarydata(int i)
    {
        byte byte0;
        try
        {
            byte0 = m_binarray[i];
        }
        catch(exception exception)
        {
            throw new arrayindexoutofboundsexception("index out of range (1005).");
        }
        return byte0;
    }

    public smartfiles getfiles()
    {
        return m_files;
    }

    public smartrequest getrequest()
    {
        return m_formrequest;
    }

    public void downloadfile(string s) throws servletexception,ioexception,smartuploadexception
    {
        downloadfile(s,null,null);
    }

 

    public void downloadfile(string s,string s1) throws

servletexception,ioexception,smartuploadexception,smartuploadexception
    {
        downloadfile(s,s1,null);
    }

    public void downloadfile(string s,string s1,string s2) throws

servletexception,ioexception,smartuploadexception
    {
        downloadfile(s,s1,s2,65000);
    }

   public void downloadfile(string s, string s1, string s2, int i)
 throws servletexception, ioexception, smartuploadexception
    {
 if(s == null)
     throw new illegalargumentexception("file '" s
     "' not found (1040).");
 if(s.equals(""))
     throw new illegalargumentexception("file '" s
     "' not found (1040).");
 if(!isvirtual(s) && m_denyphysicalpath)
     throw new securityexception("physical path is denied (1035).");
 if(isvirtual(s))
     s = m_application.getrealpath(s);
 java.io.file file = new java.io.file(s);
 fileinputstream fileinputstream = new fileinputstream(file);
 long l = file.length();
 boolean flag = false;
 int k = 0;
 byte abyte0[] = new byte[i];
 if(s1 == null)
     m_response.setcontenttype("application/x-msdownload");
 else
 if(s1.length() == 0)
     m_response.setcontenttype("application/x-msdownload");
 else
     m_response.setcontenttype(s1);
 m_response.setcontentlength((int)l);
 m_contentdisposition = m_contentdisposition != null ?
 m_contentdisposition : "attachment;";
 if(s2 == null)
     m_response.setheader("content-disposition",
     m_contentdisposition " filename="
     toutf8string(getfilename(s)));
 else
 if(s2.length() == 0)
     m_response.setheader("content-disposition",
     m_contentdisposition);
 else
     m_response.setheader("content-disposition", 

    
m_contentdisposition " filename=" toutf8string(s2));

 while((long)k < l)
 {
     int j = fileinputstream.read(abyte0, 0, i);
     k = j;
     m_response.getoutputstream().write(abyte0, 0, j);
 }
 fileinputstream.close();
    }

 public static string toutf8string(string s) {
 stringbuffer sb = new stringbuffer();
 for (int i=0;i      char c = s.charat(i);
     if (c >= 0 && c <= 255) {
  sb.append(c);
     } else {
  byte[] b;
  try {
      b = character.tostring(c).getbytes("utf-8");
  } catch (exception ex) {
      system.out.println(ex);
      b = new byte[0];
  }
  for (int j = 0; j < b.length; j ) {
      int k = b[j];
      if (k < 0) k = 256;
      sb.append("%" integer.tohexstring(k).
      touppercase());
  }
     }
 }
 return sb.tostring();
    }

    public void downloadfield(resultset resultset,string s,string s1,string s2) throws

servletexception,ioexception,sqlexception
    {
        if(resultset == null)
        {
            throw new illegalargumentexception("the recordset cannot be null (1045).");
        }
        if(s == null)
        {
            throw new illegalargumentexception("the columnname cannot be null (1050).");
        }
        if(s.length() == 0)
        {
            throw new illegalargumentexception("the columnname cannot be empty (1055).");
        }
        byte abyte0[] = resultset.getbytes(s);
        if(s1 == null)
        {
            m_response.setcontenttype("application/x-msdownload");
        }
        else
        {
            if(s1.length() == 0)
            {
                m_response.setcontenttype("application/x-msdownload");
            }
            else
            {
                m_response.setcontenttype(s1);
            }
        }
        m_response.setcontentlength(abyte0.length);
        if(s2 == null)
        {
            m_response.setheader("content-disposition","attachment;");
        }
        else
        {
            if(s2.length() == 0)
            {
                m_response.setheader("content-disposition","attachment;");
            }
            else
            {
                m_response.setheader("content-disposition","attachment; filename=" s2);
            }
        }
        m_response.getoutputstream().write(abyte0,0,abyte0.length);
    }

 


    public void fieldtofile(resultset resultset,string s,string s1) throws

servletexception,ioexception,smartuploadexception,sqlexception
    {
        try
        {
            if(m_application.getrealpath(s1) != null)
            {
                s1 = m_application.getrealpath(s1);
            }
            inputstream inputstream = resultset.getbinarystream(s);
            fileoutputstream fileoutputstream = new fileoutputstream(s1);
            int i;
            while((i = inputstream.read()) != -1)
            {
                fileoutputstream.write(i);
            }
            fileoutputstream.close();
        }
        catch(exception exception)
        {
            throw new smartuploadexception("unable to save file from the database (1020).");
        }
    }

    private string getdatafieldvalue(string s,string s1)
    {
        string s2 = ""; // = new string();
        string s3 = ""; // = new string();
        int i = 0;
        //boolean flag = false;
        //boolean flag1 = false;
        //boolean flag2 = false;
        s2 = s1 "=" '"';
        i = s.indexof(s2);
        if(i > 0)
        {
            int j = i s2.length();
            int k = j;
            s2 = "\"";
            int l = s.indexof(s2,j);
            if(k > 0 && l > 0)
            {
                s3 = s.substring(k,l);
            }
        }
        return s3;
    }

    private string getfileext(string s)
    {
        string s1; // = new string();
        int i = 0;
        int j = 0;
        if(s == null)
        {
            return null;
        }
        i = s.lastindexof('.') 1;
        j = s.length();
        s1 = s.substring(i,j);
        if(s.lastindexof('.') > 0)
        {
            return s1;
        }
        else
        {
            return "";
        }
    }

    private string getcontenttype(string s)
    {
        string s1 = ""; // = new string();
        string s2 = ""; // = new string();
        int i = 0;
        //boolean flag = false;
        s1 = "content-type:";
        i = s.indexof(s1) s1.length();
        if(i != -1)
        {
            int j = s.length();
            s2 = s.substring(i,j);
        }
        return s2;
    }

    private string gettypemime(string s)
    {
        //string s1 = new string();
        int i = 0;
        i = s.indexof("/");
        if(i != -1)
        {
            return s.substring(1,i);
        }
        else
        {
            return s;
        }
    }

    private string getsubtypemime(string s)
    {
        //string s1 = new string();
        //boolean flag = false;
        int i = 0;
        i = s.indexof("/") 1;
        if(i != -1)
        {
            int j = s.length();
            return s.substring(i,j);
        }
        else
        {
            return s;
        }
    }

    private string getcontentdisp(string s)
    {
        //string s1 = new string();
        string s1 = "";
        int i = 0;
        int j = 0;
        i = s.indexof(":") 1;
        j = s.indexof(";");
        s1 = s.substring(i,j);
        return s1;
    }

    private void getdatasection()
    {
        //boolean flag = false;
        //string s = "";
        //string s = new string();
        int i = m_currentindex;
        int j = 0;
        int k = m_boundary.length();
        m_startdata = m_currentindex;
        m_enddata = 0;
        while(i < m_totalbytes)
        {
            if(m_binarray[i] == (byte)m_boundary.charat(j))
            {
                if(j == k - 1)
                {
                    m_enddata = ((i - k) 1) - 3;
                    break;
                }
                i ;
                j ;
            }
            else
            {
                i ;
                j = 0;
            }
        }
        m_currentindex = m_enddata k 3;
    }

    private string getdataheader()
    {
        //boolean flag = false;
        int i = m_currentindex;
        int j = 0;
        for(boolean flag1 = false;!flag1;)
        {
            if(m_binarray[m_currentindex] == 13 && m_binarray[m_currentindex 2] == 13)
            {
                flag1 = true;
                j = m_currentindex - 1;
                m_currentindex = m_currentindex 2;
            }
            else
            {
                m_currentindex ;
            }
        }

        string s = new string(m_binarray,i,(j - i) 1);
        return s;
    }

    private string getfilename(string s)
    {
        //string s1 = ""; // = new string();
        //string s2 = ""; // = new string();
        //boolean flag = false;
        //boolean flag1 = false;
        //boolean flag2 = false;
        int i = 0;
        i = s.lastindexof('/');
        if(i != -1)
        {
            return s.substring(i 1,s.length());
        }
        i = s.lastindexof('\\');
        if(i != -1)
        {
            return s.substring(i 1,s.length());
        }
        else
        {
            return s;
        }
    }

    public void setdeniedfileslist(string s) throws servletexception,ioexception,sqlexception
    {
        //string s1 = "";
        if(s != null)
        {
            string s2 = "";
            for(int i = 0;i < s.length();i )
            {
                if(s.charat(i) == ',')
                {
                    if(!m_deniedfileslist.contains(s2))
                    {
                        m_deniedfileslist.addelement(s2);
                    }
                    s2 = "";
                }
                else
                {
                    s2 = s2 s.charat(i);
                }
            }

            //if(s2 != "")
            if(!s2.equals(""))
            {
                m_deniedfileslist.addelement(s2);
            }
        }
        else
        {
            m_deniedfileslist = null;
        }
    }

    public void setallowedfileslist(string s)
    {
        //string s1 = "";
        if(s != null)
        {
            string s2 = "";
            for(int i = 0;i < s.length();i )
            {
                if(s.charat(i) == ',')
                {
                    if(!m_allowedfileslist.contains(s2))
                    {
                        m_allowedfileslist.addelement(s2);
                    }
                    s2 = "";
                }
                else
                {
                    s2 = s2 s.charat(i);
                }
            }
            //if(s2 != "")
            if(!s2.equals(""))
            {
                m_allowedfileslist.addelement(s2);
            }
        }
        else
        {
            m_allowedfileslist = null;
        }
    }

    public void setdenyphysicalpath(boolean flag)
    {
        m_denyphysicalpath = flag;
    }

    public void setforcephysicalpath(boolean flag)
    {
        //m_forcephysicalpath = flag;
    }

    public void setcontentdisposition(string s)
    {
        m_contentdisposition = s;
    }

    public void settotalmaxfilesize(long l)
    {
        m_totalmaxfilesize = l;
    }

    public void setmaxfilesize(long l)
    {
        m_maxfilesize = l;
    }

    protected string getphysicalpath(string s,int i) throws ioexception
    {
        string s1 = ""; //new string();
        string s2 = ""; //new string();
        string s3 = ""; //new string();
        boolean flag = false;
        s3 = system.getproperty("file.separator");
        if(s == null)
        {
            throw new illegalargumentexception("there is no specified destination file (1140).");
        }
        if(s.equals(""))
        {
            throw new illegalargumentexception("there is no specified destination file (1140).");
        }
        if(s.lastindexof("\\") >= 0)
        {
            s1 = s.substring(0,s.lastindexof("\\"));
            s2 = s.substring(s.lastindexof("\\") 1);
        }
        if(s.lastindexof("/") >= 0)
        {
            s1 = s.substring(0,s.lastindexof("/"));
            s2 = s.substring(s.lastindexof("/") 1);
        }
        s1 = s1.length() != 0 ? s1 : "/";
        java.io.file file = new java.io.file(s1);
        if(file.exists())
        {
            flag = true;
        }
        if(i == 0)
        {
            if(isvirtual(s1))
            {
                s1 = m_application.getrealpath(s1);
                if(s1.endswith(s3))
                {
                    s1 = s1 s2;
                }
                else
                {
                    s1 = s1 s3 s2;
                }
                return s1;
            }
            if(flag)
            {
                if(m_denyphysicalpath)
                {
                    throw new illegalargumentexception("physical path is denied (1125).");
                }
                else
                {
                    return s;
                }
            }
            else
            {
                throw new illegalargumentexception("this path does not exist (1135).");
            }
        }
        if(i == 1)
        {
            if(isvirtual(s1))
            {
                s1 = m_application.getrealpath(s1);
                if(s1.endswith(s3))
                {
                    s1 = s1 s2;
                }
                else
                {
                    s1 = s1 s3 s2;
                }
                return s1;
            }
            if(flag)
            {
                throw new illegalargumentexception("the path is not a virtual path.");
            }
            else
            {
                throw new illegalargumentexception("this path does not exist (1135).");
            }
        }
        if(i == 2)
        {
            if(flag)
            {
                if(m_denyphysicalpath)
                {
                    throw new illegalargumentexception("physical path is denied (1125).");
                }
                else
                {
                    return s;
                }
            }
            if(isvirtual(s1))
            {
                throw new illegalargumentexception("the path is not a physical path.");
            }
            else
            {
                throw new illegalargumentexception("this path does not exist (1135).");
            }
        }
        else
        {
            return null;
        }
    }

    public void uploadinfile(string s) throws ioexception,smartuploadexception
    {
        //boolean flag = false;
        int i = 0;
        int j = 0;
        if(s == null)
        {
            throw new illegalargumentexception("there is no specified destination file (1025).");
        }
        if(s.length() == 0)
        {
            throw new illegalargumentexception("there is no specified destination file (1025).");
        }
        if(!isvirtual(s) && m_denyphysicalpath)
        {
            throw new securityexception("physical path is denied (1035).");
        }
        i = m_request.getcontentlength();
        m_binarray = new byte[i];
        int k;
        for(;j < i;j = k)
        {
            try
            {
                k = m_request.getinputstream().read(m_binarray,j,i - j);
            }
            catch(exception exception)
            {
                throw new smartuploadexception("unable to upload.");
            }
        }

        if(isvirtual(s))
        {
            s = m_application.getrealpath(s);
        }
        try
        {
            java.io.file file = new java.io.file(s);
            fileoutputstream fileoutputstream = new fileoutputstream(file);
            fileoutputstream.write(m_binarray);
            fileoutputstream.close();
        }
        catch(exception exception1)
        {
            throw new smartuploadexception("the form cannot be saved in the specified file (1030).");
        }
    }

    private boolean isvirtual(string s)
    {
        if(m_application.getrealpath(s) != null)
        {
            java.io.file file = new java.io.file(m_application.getrealpath(s));
            return file.exists();
        }
        else
        {
            return false;
        }
    }


}

下面是上传的应用:
首先是一个请求页面:
upload.html




文件上传


 


上传资源


enctype="multipart/form-data">
 
   
     
   
     
   
 
1、
         
       

         
       




然后是处理页面:do_upload.jsp

<%@ page contenttype="text/html; charset=gb2312" language="java"
import="java.util.*,com.drp.upload.*" errorpage="" %>//注意包名


文件上传处理页面


<%
 // 新建一个smartupload对象
 smartupload su = new smartupload();
 // 上传初始化
 su.initialize(pagecontext);
 // 设定上传限制
 // 1.限制每个上传文件的最大长度。
 // su.setmaxfilesize(10000);
 // 2.限制总上传数据的长度。
 // su.settotalmaxfilesize(20000);
 // 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
 // su.setallowedfileslist("doc,txt");
 // 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat,
 //jsp,htm,html扩展名的文件和没有扩展名的文件。
 // su.setdeniedfileslist("exe,bat,jsp,htm,html");
 // 上传文件
 su.upload();
 // 将上传文件全部保存到指定目录
 int count = su.save("/upload");
 out.println(count "个文件上传成功!
");
 
 

 // 逐一提取上传文件信息,同时可保存文件。
 for (int i=0;i  {
  smartfile file = su.getfiles().getfile(i);
  
  // 若文件不存在则继续
  if (file.ismissing()) continue;

  // 显示当前文件信息
  out.println("

");
  out.println("");
  out.println("");
  out.println("");
  out.println("");
  out.println("");
  out.println("
表单项名(fieldname)"
   file.getfieldname() "
文件长度(size)"
  file.getsize() "
文件名(filename)"
   file.getfilename() "
文件扩展名(fileext)"
   file.getfileext() "
文件全名(filepathname)"
   file.getfilepathname() "

");

  // 将文件另存
  // file.saveas("/upload/" myfile.getfilename());
  // 另存到以web应用程序的根目录为文件根目录的目录下
  // file.saveas("/upload/" myfile.getfilename(),
    //su.save_virtual);
  // 存到操作系统的根目录为文件根目录的目录下
  // file.saveas("c:\\temp\\" myfile.getfilename(),
  //su.save_physical);

 }
%>

下载:
请求页面:download.html



下载




 
   
     
   
   
     
   
   
     
   
 
文件下载
选择文件:
         
     

       
       
     




处理页面:do_download.jsp
<%@ page language="java" contenttype="text/html; charset=gbk" import="java.util.*,com.drp.upload.*"
  pageencoding="gbk"%>
<% request.setcharacterencoding("gbk"); %>


insert title here

<%
response.setcharacterencoding("gbk");
string finame = request.getparameter("file");
  // 新建一个smartupload对象
 smartupload su = new smartupload();
  // 初始化
 su.initialize(pagecontext);
  // 设定contentdisposition为null以禁止浏览器自动打开文件,
  //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为
  //doc时,浏览器将自动用word打开它。扩展名为pdf时,
  //浏览器将用acrobat打开。
 su.setcontentdisposition(null);
  // 下载文件
 su.downloadfile(finame);

%>

转载于:https://www.cnblogs.com/kentyshang/archive/2008/07/02/1234216.html

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的jsp上传组件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图