UEditor文件上传辅助类

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.Arrays;import java.util.HashMap;import java.util.Iterator;import java.util.Random;import javax.servlet.http.HttpServletRequest;import org.apache.commons.fileupload.FileItemIterator;import org.apache.commons.fileupload.FileItemStream;import org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException;import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.commons.fileupload.util.Streams;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.ApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;/** * UEditor文件上传辅助类 * */{Logger logger = LoggerFactory.getLogger(Uploader.class);// 输出文件地址private String url = “”;// 上传文件名private String fileName = “”;// 状态private String state = “”;// 文件类型private String type = “”;// 原始文件名private String originalName = “”;// 文件大小private String size = “”;private HttpServletRequest request = null;private String title = “”;ApplicationContext application;// 保存路径@SuppressWarnings(“unused”)private String savePath = “upload”;// 文件允许格式private String[] allowFiles = { “.rar”, “.doc”, “.docx”, “.zip”, “.pdf”, “.txt”, “.swf”, “.wmv”, “.gif”, “.png”,”.jpg”, “.jpeg”, “.bmp” };maxSize = 10000;private HashMap<String, String> errorInfo = new HashMap<String, String>();public Uploader(HttpServletRequest request) {this.request = request;HashMap<String, String> tmp = this.errorInfo;tmp.put(“SUCCESS”, “SUCCESS”); // 默认成功tmp.put(“NOFILE”, “未包含文件上传域”);tmp.put(“TYPE”, “不允许的文件格式”);tmp.put(“SIZE”, “文件大小超出限制”);tmp.put(“ENTYPE”, “请求类型ENTYPE错误”);tmp.put(“REQUEST”, “上传请求异常”);tmp.put(“IO”, “IO异常”);tmp.put(“DIR”, “目录创建失败”);tmp.put(“UNKNOWN”, “未知错误”);application = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());}() throws Exception {ConfigUtil util = (ConfigUtil) application.getBean(“configUtil”);boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);if (!isMultipart) {this.state = this.errorInfo.get(“NOFILE”);return;}DiskFileItemFactory dff = new DiskFileItemFactory();String savePath = this.getFolder(util.getAbsUploadPath());dff.setRepository(new File(savePath));try {ServletFileUpload sfu = new ServletFileUpload(dff);sfu.setSizeMax(this.maxSize * 1024);sfu.setHeaderEncoding(“utf-8”);FileItemIterator fii = sfu.getItemIterator(this.request);while (fii.hasNext()) {FileItemStream fis = fii.next();if (!fis.isFormField()) {this.originalName = fis.getName().substring(fis.getName().lastIndexOf(System.getProperty(“file.separator”)) + 1);if (!this.checkFileType(this.originalName)) {this.state = this.errorInfo.get(“TYPE”);continue;}this.fileName = this.getName(this.originalName);this.type = this.getFileExt(this.fileName);this.url = savePath + this.fileName;BufferedInputStream in = new BufferedInputStream(fis.openStream());FileOutputStream out = new FileOutputStream(new File(url));BufferedOutputStream output = new BufferedOutputStream(out);Streams.copy(in, output, true);this.url = util.getResourceContext() + fileName;this.state = this.errorInfo.get(“SUCCESS”);// UE中只会处理单张上传,完成后即退出break;} else {String fname = fis.getFieldName();// 只处理title,,其余表单请自行处理if (!fname.equals(“pictitle”)) {continue;}BufferedInputStream in = new BufferedInputStream(fis.openStream());BufferedReader reader = new BufferedReader(new InputStreamReader(in));StringBuffer result = new StringBuffer();while (reader.ready()) {result.append((char) reader.read());}this.title = new String(result.toString().getBytes(), “utf-8”);reader.close();}}} catch (SizeLimitExceededException e) {logger.error(“SizeLimitExceededException”,e);this.state = this.errorInfo.get(“SIZE”);} catch (InvalidContentTypeException e) {logger.error(“InvalidContentTypeException”,e);this.state = this.errorInfo.get(“ENTYPE”);} catch (FileUploadException e) {logger.error(“FileUploadException”,e);this.state = this.errorInfo.get(“REQUEST”);} catch (Exception e) {logger.error(“Exception”,e);this.state = this.errorInfo.get(“UNKNOWN”);}}/*** 接受并保存以base64格式上传的文件** @param fieldName*//*public void uploadBase64(String fieldName) {String savePath = this.getFolder(this.savePath);String base64Data = this.request.getParameter(fieldName);this.fileName = this.getName(“test.png”);this.url = savePath + this.fileName;//BASE64Decoder decoder = new BASE64Decoder();try {File outFile = new File(this.getPhysicalPath(this.url));OutputStream ro = new FileOutputStream(outFile);byte[] b = decoder.decodeBuffer(base64Data);for (int i = 0; i < b.length; ++i) {if (b[i] < 0) {b[i] += 256;}}ro.write(b);ro.flush();ro.close();this.state = this.errorInfo.get(“SUCCESS”);} catch (Exception e) {logger.error(“Exception”,e);this.state = this.errorInfo.get(“IO”);}}*//*** 文件类型判断** @param fileName* @return*/(String fileName) {Iterator<String> type = Arrays.asList(this.allowFiles).iterator();while (type.hasNext()) {String ext = type.next();if (fileName.toLowerCase().endsWith(ext)) {return true;}}return false;}/*** 获取文件扩展名** @return string*/private String getFileExt(String fileName) {return fileName.substring(fileName.lastIndexOf(“.”));}/*** 依据原始文件名生成新文件名** @return*/private String getName(String fileName) {Random random = new Random();return this.fileName = “” + random.nextInt(10000) + System.currentTimeMillis() + this.getFileExt(fileName);}/*** 根据字符串创建本地目录 并按照日期建立子目录返回** @param path* @return*/private String getFolder(String path) {// 5SimpleDateFormat formater = new SimpleDateFormat(“yyyyMMdd”);File dir = new File(path);if (!dir.exists()) {try {boolean makeDirresult = dir.mkdirs();System.out.println(“makeDirresult:” + makeDirresult);} catch (Exception e) {logger.error(“Exception”,e);this.state = this.errorInfo.get(“DIR”);return “”;}}return path;}/*** 根据传入的虚拟路径获取物理路径** @param path* @return*/@SuppressWarnings(“unused”)private String getPhysicalPath(String path) {String servletPath = this.request.getServletPath();String realPath = this.request.getSession().getServletContext().getRealPath(servletPath);return new File(realPath).getParent() + “/” + path;}(String savePath) {this.savePath = savePath;}(String[] allowFiles) {this.allowFiles = allowFiles;}(int size) {this.maxSize = size;}public String getSize() {return this.size;}public String getUrl() {return this.url;}public String getFileName() {return this.fileName;}public String getState() {return this.state;}public String getTitle() {return this.title;}public String getType() {return this.type;}public String getOriginalName() {return this.originalName;}}

而在于当时的那份心情。可是旅行的彼时那刻我的心情一直是好的吗?

UEditor文件上传辅助类

相关文章:

你感兴趣的文章:

标签云: