一、前言
关于apache的fulcrum-upload源码包中基于其org.apache.commons.fileupload.servlet.ServletFileUpload文件上传实现类,定义org.apache.fulcrum.upload.UploadService、org.apache.fulcrum.upload.DefaultUploadService上传文件服务,详情参见代码示例。
二、代码示例
1.UploadService上传服务定义接口
package org.apache.fulcrum.upload;@b@@b@import java.util.List;@b@import javax.servlet.http.HttpServletRequest;@b@import org.apache.avalon.framework.service.ServiceException;@b@import org.apache.commons.fileupload.FileItem;@b@import org.apache.commons.fileupload.FileItemIterator;@b@@b@public abstract interface UploadService@b@{@b@ public static final String ROLE = UploadService.class.getName();@b@ public static final String CONTENT_TYPE = "Content-type";@b@ public static final String CONTENT_DISPOSITION = "Content-disposition";@b@ public static final String MULTIPART = "multipart";@b@ public static final String FORM_DATA = "form-data";@b@ public static final String MIXED = "mixed";@b@ public static final String MULTIPART_FORM_DATA = "multipart/form-data";@b@ public static final String MULTIPART_MIXED = "multipart/mixed";@b@ public static final String REPOSITORY_PARAMETER = "path";@b@ public static final String REPOSITORY_KEY = "repository";@b@ public static final String REPOSITORY_DEFAULT = ".";@b@ public static final String SIZE_MAX_KEY = "sizeMax";@b@ public static final int SIZE_MAX_DEFAULT = 1048576;@b@ public static final String SIZE_THRESHOLD_KEY = "sizeThreshold";@b@ public static final int SIZE_THRESHOLD_DEFAULT = 10240;@b@ public static final String HEADER_ENCODING_KEY = "headerEncoding";@b@ public static final String HEADER_ENCODING_DEFAULT = "ISO-8859-1";@b@@b@ public abstract List<FileItem> parseRequest(HttpServletRequest paramHttpServletRequest)@b@ throws ServiceException;@b@@b@ public abstract List<FileItem> parseRequest(HttpServletRequest paramHttpServletRequest, String paramString)@b@ throws ServiceException;@b@@b@ public abstract List<FileItem> parseRequest(HttpServletRequest paramHttpServletRequest, int paramInt1, int paramInt2, String paramString)@b@ throws ServiceException;@b@@b@ public abstract FileItemIterator getItemIterator(HttpServletRequest paramHttpServletRequest)@b@ throws ServiceException;@b@@b@ public abstract long getSizeMax();@b@@b@ public abstract long getSizeThreshold();@b@@b@ public abstract String getRepository();@b@@b@ public abstract String getHeaderEncoding();@b@@b@ public abstract boolean isMultipart(HttpServletRequest paramHttpServletRequest);@b@}
2.DefaultUploadService实现类
package org.apache.fulcrum.upload;@b@@b@import java.io.File;@b@import java.io.IOException;@b@import java.util.List;@b@import javax.servlet.http.HttpServletRequest;@b@import org.apache.avalon.framework.activity.Initializable;@b@import org.apache.avalon.framework.configuration.Configurable;@b@import org.apache.avalon.framework.configuration.Configuration;@b@import org.apache.avalon.framework.context.Context;@b@import org.apache.avalon.framework.context.ContextException;@b@import org.apache.avalon.framework.context.Contextualizable;@b@import org.apache.avalon.framework.logger.AbstractLogEnabled;@b@import org.apache.avalon.framework.logger.Logger;@b@import org.apache.avalon.framework.service.ServiceException;@b@import org.apache.commons.fileupload.FileItem;@b@import org.apache.commons.fileupload.FileItemIterator;@b@import org.apache.commons.fileupload.FileUploadException;@b@import org.apache.commons.fileupload.disk.DiskFileItemFactory;@b@import org.apache.commons.fileupload.servlet.ServletFileUpload;@b@@b@public class DefaultUploadService extends AbstractLogEnabled@b@ implements UploadService, Initializable, Configurable, Contextualizable@b@{@b@ private DiskFileItemFactory itemFactory;@b@ private int sizeThreshold;@b@ private int sizeMax;@b@ private String repositoryPath;@b@ private String headerEncoding;@b@ private String applicationRoot;@b@@b@ public long getSizeMax()@b@ {@b@ return this.sizeMax;@b@ }@b@@b@ public long getSizeThreshold()@b@ {@b@ return this.itemFactory.getSizeThreshold();@b@ }@b@@b@ public String getRepository()@b@ {@b@ return this.itemFactory.getRepository().getAbsolutePath();@b@ }@b@@b@ public String getHeaderEncoding()@b@ {@b@ return this.headerEncoding;@b@ }@b@@b@ public List<FileItem> parseRequest(HttpServletRequest req)@b@ throws ServiceException@b@ {@b@ return parseRequest(req, this.sizeMax, this.itemFactory);@b@ }@b@@b@ public List<FileItem> parseRequest(HttpServletRequest req, String path)@b@ throws ServiceException@b@ {@b@ return parseRequest(req, this.sizeThreshold, this.sizeMax, path);@b@ }@b@@b@ public List<FileItem> parseRequest(HttpServletRequest req, int sizeThreshold, int sizeMax, String path)@b@ throws ServiceException@b@ {@b@ return parseRequest(req, sizeMax, new DiskFileItemFactory(sizeThreshold, new File(path)));@b@ }@b@@b@ protected List<FileItem> parseRequest(HttpServletRequest req, int sizeMax, DiskFileItemFactory factory)@b@ throws ServiceException@b@ {@b@ ServletFileUpload fileUpload;@b@ try@b@ {@b@ fileUpload = new ServletFileUpload(factory);@b@ fileUpload.setSizeMax(sizeMax);@b@ fileUpload.setHeaderEncoding(this.headerEncoding);@b@ return fileUpload.parseRequest(req);@b@ }@b@ catch (FileUploadException e)@b@ {@b@ throw new ServiceException(Contextualizable.ROLE, e.getMessage(), e);@b@ }@b@ }@b@@b@ public FileItemIterator getItemIterator(HttpServletRequest req)@b@ throws ServiceException@b@ {@b@ ServletFileUpload upload = new ServletFileUpload();@b@ try@b@ {@b@ return upload.getItemIterator(req);@b@ }@b@ catch (FileUploadException e)@b@ {@b@ throw new ServiceException(Contextualizable.ROLE, e.getMessage(), e);@b@ }@b@ catch (IOException e)@b@ {@b@ throw new ServiceException(Contextualizable.ROLE, e.getMessage(), e);@b@ }@b@ }@b@@b@ public boolean isMultipart(HttpServletRequest req)@b@ {@b@ return ServletFileUpload.isMultipartContent(req);@b@ }@b@@b@ private String getRealPath(String path)@b@ {@b@ String absolutePath = null;@b@ if (this.applicationRoot == null)@b@ {@b@ absolutePath = new File(path).getAbsolutePath();@b@ }@b@ else@b@ {@b@ absolutePath = new File(this.applicationRoot, path).getAbsolutePath();@b@ }@b@@b@ return absolutePath;@b@ }@b@@b@ public void configure(Configuration conf)@b@ {@b@ this.repositoryPath = conf.getAttribute("repository", ".");@b@@b@ this.headerEncoding = conf.getAttribute("headerEncoding", "ISO-8859-1");@b@@b@ this.sizeMax = conf.getAttributeAsInteger("sizeMax", 1048576);@b@@b@ this.sizeThreshold = conf.getAttributeAsInteger("sizeThreshold", 10240);@b@ }@b@@b@ public void initialize()@b@ throws Exception@b@ {@b@ String testPath = getRealPath(this.repositoryPath);@b@ File testDir = new File(testPath);@b@ if (testDir.exists())@b@ {@b@ this.repositoryPath = testPath;@b@ }@b@@b@ getLogger().debug("Upload Service: REPOSITORY_KEY => " + this.repositoryPath);@b@@b@ this.itemFactory = new DiskFileItemFactory(this.sizeThreshold, new File(this.repositoryPath));@b@ }@b@@b@ public void contextualize(Context context)@b@ throws ContextException@b@ {@b@ this.applicationRoot = context.get("urn:avalon:home").toString();@b@ }@b@}