服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

用javazoom的uploadbean扩展request的功能


  在servlet中,upload上传是我们常用的操作之一。然而在javax.servlet包中,javax.servlet.http.httpservletrequest对于form中的enctype
  ="multipart/form-data"的内容的处理支持却极有限,我们不得不手工编写读取和分离stream中的内容的代码。这使我们对封装完好的multipartformdatarequest提供支持的包充满了期待。其实常用的javazoom的uploadbean中,就已经提供了这个良好的工具。另外,uploadbean还包括了上传后的存储机制,如保存到数据库、文件、zip文件等。如果你不需要做定制的话,尽管可以使用他们。当然定制上传后的处理很简单也很实用。
  javazoom的包中共包含了7个class文件:
  uploadbean.class
  
  uploadparameters.class
  
  uploadlistener.class
  
  uploadfile.class
  
  uploadexception.class
  
  archiver.class
  
  multipartformdatarequest.class
  
  其中,我们最感兴趣的是multipartformdatarequest.class。正如其名,它使我们可以象处理一般的form那样处理enctype="multipart/form-data"的form的内容。
  
  在mutipartformdatarequest中,共有5种public方法:
  
  getparameternames(name:string):enumeration
  
  getparametervalue(name:string):string
  
  getparametervalues(name:string):string[]
  
  getfiles():hashtable
  
  ismultipartformdata(req:httpservletrequest):boolean
  
  和2种public构造函数:
  
  multipartformdatarequest(req:httpservletrequest,maxcontentlength:int)
  
  multipartformdatarequest(req:httpservletrequest)
  
  所有的对mutipart的内容的分析,在构造函数中由 com.oreilly.servlet.multipart包中的分析器完成。我们无需关心原先最头疼的对stream内容的分析的工作,只需要象下面的代码那样:
  
  if (multipartformdatarequest.ismultipartformdata(request)){//如果是multipart类型的request
  
  multipartformdatarequest mrequest = new multipartformdatarequest(request);
  
  }
  
  就得到了mutipartformdatarequest的实例。
  
  如果想得到上传的内容:
  
  hashtable files = mrequest.getfiles();//得到所有的上传的文件
  
  if(files!=null && !files.isempty()){
  
  uploadfile file=(uploadfile)files.get(name);// name:string 文件输入框的名称
  
  // file:uploadfile 文件
  
  }
  
  在这里,uploadfile file中包含了我们感兴趣的上传的文件的所有的信息:
  
  getdata():byte[] 文件的内容
  
  getfilesize():long 文件的长度
  
  getcontenttype():string 文件的编码
  
  getfilename():string 文件的名称
  
  
  这些信息足够我们进行一般所想要的操作了。
  
  正如uploadbean的名称所言,uploadbean提供对某些常用上传操作的封装。
  
  在uploadbean中,setstoremodel(storemodel:int)提供了对上传文件以下几种形式的保存
  
  0 保存在内存中
  
  1 保存在指定目录中
  
  2 保存在数据库中
  
  3 保存在zip文件中
  
  4 保存在tagzip文件中
  
  5 序列化形式保存
  
  6 xml形式保存
  
  不过以上每一种形式的保存,都需要符合uploadbean中所指的一些规则。如保存在数据库中,uploadbean是把数据库纯粹作为一种存储手段,关键字都是用timestamp生成的;保存在目录中,不能指定3层以上目录。
  
  如我们需要把上传文件用自己的方式保存,可以采用重新编写保存或者直接重载uploadbean实现。
  
  从byte[]data=file.getdata();中,我们得到文件的字节数组;很容易通过构造新的流,进行文件的输入输出,存储到数据库,保存在各种形式的载体中。
  
  
  实例1:上传文件并且存放到数据库中
  
  数据库表格:testtable
  
  lsh char(10) pk,
  
  nr clob(1000000)
  
  
  //数据库基本操作
  
  package upload.example1;
  
  import java.sql.*;
  
  import javax.sq1.*;
  
  import javax.naming.*;
  
  public class dbops(){
  
  private datasource ds;
  
  private connection conn;
  
  private string env="java:comp/env/jdbc/testdb";
  public string getenv(){return env;}
  public void setenv(string env){this.env=env;}
  public datasource getdatasource(){//取得数据源(连结池)
  try{
  
  context ictx=new initalcontext();
  
  ds=(datasource)ictx.lookup(env);
  
  }catch(exception ignore){
  
  //some debug codes
  
  }
  return ds;
  }
  public connection getconnection(){//取得数据库连结
  if(ds==null)ds=getdatasource();
  
  try{
  
  if(conn==null)conn=ds.getconnection();
  
  }catch(exception ignore){
  
  //some debug codes
  
  }
  return conn;
  }
  }
  //存储
  package upload.example1;
  
  import java.sql.*;
  
  public class dbstore(){
  
  public static string isfileexist="select count(*) from testtable where lsh=?";
  
  public static string insertfile="insert into testtable (lsh)values(?)";
  
  public static string updatefile="update testtable set nr=? where lsh=?";
  
  connection conn;
  
  dbops dbops;
  
  protected boolean isfileexist(string lsh)throws sqlexception{
  
  preparedstatement pst=conn.preparestatement(isfileexist);
  
  pst.setstring(1,lsh);
  
  resultset rs=pst.executequery();
  
  int count;
  
  for(count=0;rs.next();count=rs.getint(1));
  
  pst.close();
  
  if(count<=0)return false;
  
  else return true;
  
  }
  
  protected void insertfile(string lsh)throws sqlexception{
  
  preparedstatement pst=conn.preparestatement(insertfile);
  
  pst.setstring(1,lsh);
  
  pst.executeupdate();
  
  pst.close();
  
  }
  
  protected void updatefile(string lsh,byte[]data)throws sqlexception{
  
  preparedstatement pst=conn.preparestatement(updatefile);
  
  pst.setbytes(1,data);
  
  pst.setstring(2,lsh);
  
  pst.executeupdate();
  
  pst.close();
  
  }
  
  public void savefile(string lsh,byte[]data)throws sqlexception{
  
  if(lsh==null || data==null ||lsh.trim().length()==0)return;
  
  
  if(dbops==null)dbops=new dbops();
  
  if(conn==null)conn=dbops.getconnection();
  
  
  if(!isfileexist(lsh)){
  
  insertfile(lsh);
  
  }
  
  updatefile(lsh,data);
  
  
  conn.close();
  
  conn=null;
  
  }
  
  }
  
  //servlet 片断(servlet 中应包含javazoom.upload.*;java.util.*;)
  
  dbstore dbs=new dbstore();
  
  if (multipartformdatarequest.ismultipartformdata(request))
  
  {
  
  multipartformdatarequest mrequest = new multipartformdatarequest(request);
  
  if(mrequest.getparameter("submit")!=null){
  
  string lsh=mrequest.getparameter("lsh");
  
  hashtable files = mrequest.getfiles();
  
  uploadfile file = files.get("ufile");
  
  byte[]data=file.getdata();
  
  try{
  
  dbs.savefile(lsh,data);
  
  }catch(exception e){
  
  //debugcodes
  
  }
  }
  }
  //submit html 片断
  

  

  

  

  

  实际运行以上代码时,注意dbops的env参数需要和实际的服务器配置的连结池参数一致,并且需要有upload.jar 和 cos.jar这两个包。如果是在webshpere 4上,请把他们拷贝到项目的webapplication/web-inf/lib下。如果是resin上,把他们拷贝到项目的web-inf/lib下。

扫描关注微信公众号