欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

common-upload上传文件功能封装类分享

程序员文章站 2024-02-14 16:27:28
文件屬性bean复制代码 代码如下:package com.onsafe.util.upload; /** * 文件屬性bean * @author...

文件屬性bean

复制代码 代码如下:

package com.onsafe.util.upload;

/**
 * 文件屬性bean
 * @author lushuifa
 */
public class filebean {
 private string filename;// 文件名
 private string filecontenttype;// 上传文件的数据类型
 private long filesize;// 文件的大小;单位byte
 private string extname;// 文件扩展名的大小
 private string fieldname;
 private string filepath;
 private string filenote;//文件说明
 private string newfilename;//新文件名

 public string getfilename() {
  return filename;
 }

 public void setfilename(string filename) {
  this.filename = filename;
 }

 public string getfilecontenttype() {
  return filecontenttype;
 }

 public void setfilecontenttype(string filecontenttype) {
  this.filecontenttype = filecontenttype;
 }

 public long getfilesize() {
  return filesize;
 }

 public void setfilesize(long filesize) {
  this.filesize = filesize;
 }

 public string getextname() {
  return extname;
 }

 public void setextname(string extname) {
  this.extname = extname;
 }

 public string getfilepath() {
  return filepath;
 }

 public void setfilepath(string filepath) {
  this.filepath = filepath;
 }

 public string getfieldname() {
  return fieldname;
 }

 public void setfieldname(string fieldname) {
  this.fieldname = fieldname;
 }

 public string getfilenote() {
  return filenote;
 }

 public void setfilenote(string filenote) {
  this.filenote = filenote;
 }

 public string getnewfilename() {
  return newfilename;
 }

 public void setnewfilename(string newfilename) {
  this.newfilename = newfilename;
 }
}

上传工具类

复制代码 代码如下:

package com.onsafe.util.upload;

import java.io.file;
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.date;
import java.util.hashmap;
import java.util.list;
import java.util.map;

import javax.servlet.http.httpservletrequest;

import org.apache.commons.fileupload.fileitem;
import org.apache.commons.fileupload.disk.diskfileitemfactory;
import org.apache.commons.fileupload.servlet.servletfileupload;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;

import com.onsafe.util.chinesespelling;

/**
 * 上传工具类
 * @author administrator
 *
 */
public class uploadtools {

 protected final static log log = logfactory.getlog(uploadtools.class);

 private httpservletrequest request = null;
 private string savepath = "";
 private list<filebean> filebeanlist = null;
 //上传表单的其它信息,除file类型的input
 private map<string,string> formdatas = null;
 private string uploadstatus = "";
 //文件单个最大大小
 private long maxsize;
 //文件允许的格式为
 private string allowexts;
 //文件不允许的格式为
 private string unallowexts;

 public void uploadfiles() {
  savepath = this.getsavepath();
  filebeanlist = new arraylist<filebean>();
  formdatas    = new hashmap<string,string>();
  diskfileitemfactory factory = new diskfileitemfactory();
  servletfileupload upload = new servletfileupload(factory);
  try {
   list<fileitem> items = upload.parserequest(this.getrequest());//上传文件解析
   //表单元素集合
   list<fileitem> formfieldlist = new arraylist<fileitem>();
   //文件元素集合
   list<fileitem> filefieldlist = new arraylist<fileitem>();
   if (items.size()>0) {
    //将表单元素和文件元素装载到不同的集合中
    for (fileitem fileitem : items) {
     if (fileitem.isformfield()) {
      formfieldlist.add(fileitem);
     } else {
      filefieldlist.add(fileitem);
     }
    }

    /**
     * 分解表单元素
     */
    //初始化文件上传属性bean
    filebean filebean = null;
    for (fileitem fileitem : formfieldlist) {
     system.out.println("表单参数名:" + fileitem.getfieldname()+ ",表单参数值:" + fileitem.getstring("utf-8"));
     formdatas.put(fileitem.getfieldname(), fileitem.getstring("utf-8").replace("'", "''"));
     if(fileitem.getfieldname().equals("companyname")){
      savepath = savepath.replace("gongsi", chinesespelling.getspell(fileitem.getstring("utf-8")));
     }

     if(fileitem.getfieldname().tolowercase().contains("colum")){
      savepath = savepath.replace("lanmu", chinesespelling.getspell(fileitem.getstring("utf-8")));
     }

     if(fileitem.getfieldname().equals("articleuuid")){
      savepath = savepath.replace("uuid", fileitem.getstring("utf-8"));
     }
    }
    log.info("保存的真实路径为:"+savepath);

    
    /**
     * 分解文件元素
     */
    for (fileitem fileitem : filefieldlist) {
     //先判断文件件域是否选择了文件
     if (fileitem.getname() != null && !fileitem.getname().equals("")) {
      //文件名
      string filename = fileitem.getname();
      // ie 和 firefox 下的 getname() 得出的值不同
                        // ie 下得到的是文件的完整路径
                        if(filename.contains("\\")) {
                            // 截取得到完整路径后的文件名
                         filename = filename.substring(filename.lastindexof("\\") + 1);
                        }
      //文件大小
      long filesize = fileitem.getsize();
      //检查扩展名
      string extname = filename.substring(filename.lastindexof(".") + 1).tolowercase();
                        //文件属性bean
      filebean = new filebean();
      filebean.setfilename(filename);
      filebean.setextname(extname);
      filebean.setfilesize(filesize);
      simpledateformat df = new simpledateformat("yyyymmdd");
      //文件名转拼音
      string pingyingfilename = chinesespelling.getspell(filename);
      string newfilename = df.format(new date()) + "_" + pingyingfilename;

      filebean.setfilepath(savepath+newfilename);
      filebean.setfieldname(fileitem.getfieldname());
      filebean.setnewfilename(newfilename);
      file f1 = new file(savepath);
      if (!f1.exists()) {
       f1.mkdirs();
      }
      system.out.println("新文件路径:"+newfilename);
      file uploadedfile = new file(savepath, newfilename);
      fileitem.write(uploadedfile);
      // 获取根目录对应的真实物理路径
      // 保存文件在服务器的物理磁盘中
      system.out.println("上传文件的大小:" + fileitem.getsize());
      system.out.println("上传文件的类型:" + fileitem.getcontenttype());
      system.out.println("上传文件的名称:" + filename);
      filebeanlist.add(filebean);
      uploadstatus = "上传成功";
     } else {
      uploadstatus = "没有选择文件!";
     }
    }
   }
  } catch (exception e) {
   e.printstacktrace();
   uploadstatus =  "上传文件失败!";
  }
 }


 public list<filebean> getfilebeanlist() {
  return filebeanlist;
 }

 public void setfilebeanlist(list<filebean> filebeanlist) {
  this.filebeanlist = filebeanlist;
 }

 public httpservletrequest getrequest() {
  return request;
 }

 public void setrequest(httpservletrequest request) {
  this.request = request;
 }

 public string getsavepath() {
  return savepath;
 }

 public void setsavepath(string savepath) {
  this.savepath = savepath;
 }

 public map<string, string> getformdatas() {
  return formdatas;
 }

 public void setformdatas(map<string, string> formdatas) {
  this.formdatas = formdatas;
 }

 public string getuploadstatus() {
  return uploadstatus;
 }

 public void setuploadstatus(string uploadstatus) {
  this.uploadstatus = uploadstatus;
 }

 public long getmaxsize() {
  return maxsize;
 }

 public void setmaxsize(long maxsize) {
  this.maxsize = maxsize;
 }

 public string getallowexts() {
  return allowexts;
 }

 public void setallowexts(string allowexts) {
  this.allowexts = allowexts;
 }

 public string getunallowexts() {
  return unallowexts;
 }

 public void setunallowexts(string unallowexts) {
  this.unallowexts = unallowexts;
 }
}

运用工具类

复制代码 代码如下:

// 实例化上传工具类
uploadtools uploadtools = new uploadtools();
uploadtools.setsavepath(savepath);
uploadtools.setrequest(wu.request);
uploadtools.uploadfiles();
map<string, string> fds = uploadtools.getformdatas();
list<filebean> filebeanlist =uploadtools.getfilebeanlist();