You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by de...@struts.apache.org on 2005/03/02 00:07:04 UTC
[Apache Struts Wiki] Updated: StrutsUpload
Date: 2005-03-01T15:07:04
Editor: DakotaJack
Wiki: Apache Struts Wiki
Page: StrutsUpload
URL: http://wiki.apache.org/struts/StrutsUpload
no comment
Change Log:
------------------------------------------------------------------------------
@@ -440,4 +440,282 @@
Jack
+------
+
+The implementation of the !MultimpartRequestHandler in Strust upload is:
+
+{{{
+public class CommonsMultipartRequestHandler implements MultipartRequestHandler {
+ public static final long DEFAULT_SIZE_MAX = 250 * 1024 * 1024;
+ public static final int DEFAULT_SIZE_THRESHOLD = 256 * 1024;
+ protected static Log log = LogFactory.getLog(
+ CommonsMultipartRequestHandler.class);
+ private Hashtable elementsAll;
+ private Hashtable elementsFile;
+ private Hashtable elementsText;
+ private ActionMapping mapping;
+ private ActionServlet servlet;
+
+ public ActionServlet getServlet() {
+ return this.servlet;
+ }
+
+ public void setServlet(ActionServlet servlet) {
+ this.servlet = servlet;
+ }
+
+ public ActionMapping getMapping() {
+ return this.mapping;
+ }
+
+ public void setMapping(ActionMapping mapping) {
+ this.mapping = mapping;
+ }
+
+ public void handleRequest(HttpServletRequest request)
+ throws ServletException {
+ ModuleConfig ac = (ModuleConfig) request.getAttribute(
+ Globals.MODULE_KEY);
+ DiskFileUpload upload = new DiskFileUpload();
+ upload.setHeaderEncoding(request.getCharacterEncoding());
+ upload.setSizeMax(getSizeMax(ac));
+ upload.setSizeThreshold((int) getSizeThreshold(ac));
+ upload.setRepositoryPath(getRepositoryPath(ac));
+ elementsText = new Hashtable();
+ elementsFile = new Hashtable();
+ elementsAll = new Hashtable();
+
+
+ List items = null;
+ try {
+ items = upload.parseRequest(request);
+ } catch (DiskFileUpload.SizeLimitExceededException e) {
+ request.setAttribute(
+ MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED,
+ Boolean.TRUE);
+ return;
+ } catch (FileUploadException e) {
+ log.error("Failed to parse multipart request", e);
+ throw new ServletException(e);
+ }
+
+ Iterator iter = items.iterator();
+ while (iter.hasNext()) {
+ FileItem item = (FileItem) iter.next();
+
+ if (item.isFormField()) {
+ addTextParameter(request, item);
+ } else {
+ addFileParameter(item);
+ }
+ }
+ }
+
+ public Hashtable getTextElements() {
+ return this.elementsText;
+ }
+
+ public Hashtable getFileElements() {
+ return this.elementsFile;
+ }
+
+ public Hashtable getAllElements() {
+ return this.elementsAll;
+ }
+
+ public void rollback() {
+ Iterator iter = elementsFile.values().iterator();
+
+ while (iter.hasNext()) {
+ FormFile formFile = (FormFile) iter.next();
+
+ formFile.destroy();
+ }
+ }
+
+ public void finish() {
+ rollback();
+ }
+
+ protected long getSizeMax(ModuleConfig mc) {
+ return convertSizeToBytes(
+ mc.getControllerConfig().getMaxFileSize(),
+ DEFAULT_SIZE_MAX);
+ }
+
+ protected long getSizeThreshold(ModuleConfig mc) {
+ return convertSizeToBytes(
+ mc.getControllerConfig().getMemFileSize(),
+ DEFAULT_SIZE_THRESHOLD);
+ }
+
+ protected long convertSizeToBytes(String sizeString, long defaultSize) {
+ int multiplier = 1;
+
+ if (sizeString.endsWith("K")) {
+ multiplier = 1024;
+ } else if (sizeString.endsWith("M")) {
+ multiplier = 1024 * 1024;
+ } else if (sizeString.endsWith("G")) {
+ multiplier = 1024 * 1024 * 1024;
+ }
+ if (multiplier != 1) {
+ sizeString = sizeString.substring(0, sizeString.length() - 1);
+ }
+
+ long size = 0;
+ try {
+ size = Long.parseLong(sizeString);
+ } catch (NumberFormatException nfe) {
+ log.warn("Invalid format for file size ('" + sizeString +
+ "'). Using default.");
+ size = defaultSize;
+ multiplier = 1;
+ }
+
+ return (size * multiplier);
+ }
+
+ protected String getRepositoryPath(ModuleConfig mc) {
+
+ // First, look for an explicitly defined temp dir.
+ String tempDir = mc.getControllerConfig().getTempDir();
+
+ // If none, look for a container specified temp dir.
+ if (tempDir == null || tempDir.length() == 0) {
+ if (servlet != null) {
+ ServletContext context = servlet.getServletContext();
+ File tempDirFile = (File) context.getAttribute(
+ "javax.servlet.context.tempdir");
+ tempDir = tempDirFile.getAbsolutePath();
+ }
+
+ // If none, pick up the system temp dir.
+ if (tempDir == null || tempDir.length() == 0) {
+ tempDir = System.getProperty("java.io.tmpdir");
+ }
+ }
+
+ if (log.isTraceEnabled()) {
+ log.trace("File upload temp dir: " + tempDir);
+ }
+
+ return tempDir;
+ }
+
+ protected void addTextParameter(HttpServletRequest request, FileItem item) {
+ String name = item.getFieldName();
+ String value = null;
+ boolean haveValue = false;
+ String encoding = request.getCharacterEncoding();
+
+ if (encoding != null) {
+ try {
+ value = item.getString(encoding);
+ haveValue = true;
+ } catch (Exception e) {
+ // Handled below, since haveValue is false.
+ }
+ }
+ if (!haveValue) {
+ try {
+ value = item.getString("ISO-8859-1");
+ } catch (java.io.UnsupportedEncodingException uee) {
+ value = item.getString();
+ }
+ haveValue = true;
+ }
+
+ if (request instanceof MultipartRequestWrapper) {
+ MultipartRequestWrapper wrapper = (MultipartRequestWrapper) request;
+ wrapper.setParameter(name, value);
+ }
+
+ String[] oldArray = (String[]) elementsText.get(name);
+ String[] newArray;
+
+ if (oldArray != null) {
+ newArray = new String[oldArray.length + 1];
+ System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
+ newArray[oldArray.length] = value;
+ } else {
+ newArray = new String[] { value };
+ }
+
+ elementsText.put(name, newArray);
+ elementsAll.put(name, newArray);
+ }
+
+ protected void addFileParameter(FileItem item) {
+ FormFile formFile = new CommonsFormFile(item);
+
+ elementsFile.put(item.getFieldName(), formFile);
+ elementsAll.put(item.getFieldName(), formFile);
+ }
+
+ static class CommonsFormFile implements FormFile, Serializable {
+ FileItem fileItem;public CommonsFormFile(FileItem fileItem) {
+ this.fileItem = fileItem;
+ }
+
+ public String getContentType() {
+ return fileItem.getContentType();
+ }
+
+ public void setContentType(String contentType) {
+ throw new UnsupportedOperationException(
+ "The setContentType() method is not supported.");
+ }
+
+ public int getFileSize() {
+ return (int)fileItem.getSize();
+ }
+
+ public void setFileSize(int filesize) {
+ throw new UnsupportedOperationException(
+ "The setFileSize() method is not supported.");
+ }
+
+ public String getFileName() {
+ return getBaseFileName(fileItem.getName());
+ }
+
+ public void setFileName(String fileName) {
+ throw new UnsupportedOperationException(
+ "The setFileName() method is not supported.");
+ }
+
+ public byte[] getFileData() throws FileNotFoundException, IOException {
+ return fileItem.get();
+ }
+
+ public InputStream getInputStream() throws FileNotFoundException, IOException {
+ return fileItem.getInputStream();
+ }
+
+ public void destroy() {
+ fileItem.delete();
+ }
+
+ protected String getBaseFileName(String filePath) {
+ String fileName = new File(filePath).getName();
+ int colonIndex = fileName.indexOf(":");
+ if (colonIndex == -1) {
+ colonIndex = fileName.indexOf("\\\\");
+ }
+ int backslashIndex = fileName.lastIndexOf("\\");
+
+ if (colonIndex > -1 && backslashIndex > -1) {
+ fileName = fileName.substring(backslashIndex + 1);
+ }
+ return fileName;
+ }
+
+ public String toString() {
+ return getFileName();
+ }
+ }
+}
+}}}
+
-----
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org