Common-upload Upload file Function encapsulation class sharing _java

Source: Internet
Author: User

File-Belongs Bean

Copy Code code as follows:

Package com.onsafe.util.upload;

/**
* File-Nature Bean
* @author Lushuifa
*/
public class Filebean {
Private String filename;//file name
Private String filecontenttype;//The data type of the uploaded file
The size of the private long filesize;//file; unit byte
Private String extname;//The size of the file name extension
Private String FieldName;
Private String FilePath;
Private String filenote;//File description
Private String newfilename;//New file name

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;
}
}

Upload Tool class

Copy Code code as follows:

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;

/**
* Upload Tool class
* @author Administrator
*
*/
public class Uploadtools {

Protected final Static log = Logfactory.getlog (Uploadtools.class);

Private HttpServletRequest request = null;
Private String Savepath = "";
Private list<filebean> filebeanlist = null;
Additional information for uploading forms, except for file type input
Private map<string,string> Formdatas = null;
Private String Uploadstatus = "";
File single Maximum size
Private long maxSize;
The format allowed for the file is
Private String allowexts;
File is not allowed in a format that is
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 ());//Upload file Resolution
Form Element Collection
list<fileitem> formfieldlist = new arraylist<fileitem> ();
File Element Collection
list<fileitem> filefieldlist = new arraylist<fileitem> ();
if (Items.size () >0) {
To load form elements and file elements into a different collection
for (Fileitem Fileitem:items) {
if (Fileitem.isformfield ()) {
Formfieldlist.add (Fileitem);
} else {
Filefieldlist.add (Fileitem);
}
}

/**
* Decompose form elements
*/
Initialize File Upload Property bean
Filebean Filebean = null;
for (Fileitem fileitem:formfieldlist) {
System.out.println ("Form parameter name:" + fileitem.getfieldname () + ", form parameter value:" + 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 ("The True Path of preservation is:" +savepath);


/**
* Explode file elements
*/
for (Fileitem fileitem:filefieldlist) {
Determine if a file is selected
if (fileitem.getname ()!= null &&!fileitem.getname (). Equals ("")) {
Filename
String fileName = Fileitem.getname ();
IE and FireFox under the GetName () the value of different
IE gets the full path of the file
if (Filename.contains ("\")) {
Intercepts the file name after the full path
FileName = filename.substring (Filename.lastindexof ("\") + 1);
}
File size
Long fileSize = Fileitem.getsize ();
Check extension
String extname = filename.substring (Filename.lastindexof (".") + 1). toLowerCase ();
File attribute Bean
Filebean = new Filebean ();
Filebean.setfilename (FileName);
Filebean.setextname (Extname);
Filebean.setfilesize (fileSize);
SimpleDateFormat df = new SimpleDateFormat ("YyyyMMdd");
name to Pinyin
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 ("New file path:" +newfilename);
File UploadedFile = new file (Savepath, newfilename);
Fileitem.write (UploadedFile);
Get the true physical path of the root directory
Save the file on the server's physical disk
System.out.println ("Upload file Size:" + fileitem.getsize ());
System.out.println ("Type of File uploaded:" + Fileitem.getcontenttype ());
System.out.println ("Upload file name:" + fileName);
Filebeanlist.add (Filebean);
Uploadstatus = "Upload success";
} else {
Uploadstatus = "No files selected!" ";
}
}
}
catch (Exception e) {
E.printstacktrace ();
Uploadstatus = "Upload file failed!" ";
}
}


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;
}
}

Use the tool class

Copy Code code as follows:

Instantiate Upload Tool class
Uploadtools uploadtools = new Uploadtools ();
Uploadtools.setsavepath (Savepath);
Uploadtools.setrequest (wu.request);
Uploadtools.uploadfiles ();
map<string, string> FDS = Uploadtools.getformdatas ();
List<filebean> filebeanlist =uploadtools.getfilebeanlist ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.