Documented beans
Copy the code code as follows:
package com.onsafe.util.upload;
/**
* Documentary bean
* @author lushuifa
*/
public class FileBean {
private String fileName;// file name
private String fileContentType;//Data type of uploaded file
private long fileSize;//The size of the file; unit byte
private String extName;//The size of the file 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 tools
Copy the 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 tools
* @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;
//Upload other information of the form, except file type input
private Map<String,String> formDatas = null;
private String uploadStatus = "";
//Maximum size of a single file
private long maxSize;
//The allowed format of the file is
private String allowExts;
//The format not allowed for the file 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 parsing
//Collection of form elements
List<FileItem> formFieldList = new ArrayList<FileItem>();
//File element collection
List<FileItem> fileFieldList = new ArrayList<FileItem>();
if (items.size()>0) {
//Load form elements and file elements into different collections
for (FileItem fileItem : items) {
if (fileItem.isFormField()) {
formFieldList.add(fileItem);
} else {
fileFieldList.add(fileItem);
}
}
/**
* Decompose form elements
*/
//Initialize file upload attribute 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 real path saved is: "+savePath);
/**
* Decompose file elements
*/
for (FileItem fileItem : fileFieldList) {
//First determine whether a file is selected in the file field
if (fileItem.getName() != null && !fileItem.getName().equals("")) {
//file name
String fileName = fileItem.getName();
//The values obtained by getName() under IE and FireFox are different
// What you get under IE is the full path of the file
if(fileName.contains("//")) {
//Intercept the file name after obtaining 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");
//Convert file 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 real physical path corresponding to the root directory
//Save the file on the server's physical disk
System.out.println("Size of uploaded file:" + fileItem.getSize());
System.out.println("Type of uploaded file:" + fileItem.getContentType());
System.out.println("Name of uploaded file:" + fileName);
fileBeanList.add(fileBean);
uploadStatus = "Upload successful";
} else {
uploadStatus = "No file selected!";
}
}
}
} catch (Exception e) {
e.printStackTrace();
uploadStatus = "Failed to upload file!";
}
}
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 tools
Copy the code code as follows:
// Instantiate the 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();