📄 commonsmultipartrequesthandler.java.svn-base
字号:
/** * <p> Returns the path to the temporary directory to be used for uploaded * files which are written to disk. The directory used is determined from * the first of the following to be non-empty. <ol> <li>A temp dir * explicitly defined either using the <code>tempDir</code> servlet init * param, or the <code>tempDir</code> attribute of the <controller> * element in the Struts config file.</li> <li>The container-specified * temp dir, obtained from the <code>javax.servlet.context.tempdir</code> * servlet context attribute.</li> <li>The temp dir specified by the * <code>java.io.tmpdir</code> system property.</li> (/ol> </p> * * @param mc The module config instance for which the path should be * determined. * @return The path to the directory to be used to store uploaded files. */ 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; } /** * <p> Adds a regular text parameter to the set of text parameters for * this request and also to the list of all parameters. Handles the case * of multiple values for the same parameter by using an array for the * parameter value. </p> * * @param request The request in which the parameter was specified. * @param item The file item for the parameter to add. */ protected void addTextParameter(HttpServletRequest request, FileItem item) { String name = item.getFieldName(); String value = null; boolean haveValue = false; String encoding = null; if (item instanceof DiskFileItem) { encoding = ((DiskFileItem)item).getCharSet(); if (log.isDebugEnabled()) { log.debug("DiskFileItem.getCharSet=[" + encoding + "]"); } } if (encoding == null) { encoding = request.getCharacterEncoding(); if (log.isDebugEnabled()) { log.debug("request.getCharacterEncoding=[" + encoding + "]"); } } 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); } /** * <p> Adds a file parameter to the set of file parameters for this * request and also to the list of all parameters. </p> * * @param item The file item for the parameter to add. */ protected void addFileParameter(FileItem item) { FormFile formFile = new CommonsFormFile(item); String name = item.getFieldName(); if (elementsFile.containsKey(name)) { Object o = elementsFile.get(name); if (o instanceof List) { ((List)o).add(formFile); } else { List list = new ArrayList(); list.add((FormFile)o); list.add(formFile); elementsFile.put(name, list); elementsAll.put(name, list); } } else { elementsFile.put(name, formFile); elementsAll.put(name, formFile); } } // ---------------------------------------------------------- Inner Classes /** * <p> This class implements the Struts <code>FormFile</code> interface by * wrapping the Commons FileUpload <code>FileItem</code> interface. This * implementation is <i>read-only</i>; any attempt to modify an instance * of this class will result in an <code>UnsupportedOperationException</code>. * </p> */ static class CommonsFormFile implements FormFile, Serializable { /** * <p> The <code>FileItem</code> instance wrapped by this object. * </p> */ FileItem fileItem; /** * Constructs an instance of this class which wraps the supplied file * item. </p> * * @param fileItem The Commons file item to be wrapped. */ public CommonsFormFile(FileItem fileItem) { this.fileItem = fileItem; } /** * <p> Returns the content type for this file. </p> * * @return A String representing content type. */ public String getContentType() { return fileItem.getContentType(); } /** * <p> Sets the content type for this file. <p> NOTE: This method is * not supported in this implementation. </p> * * @param contentType A string representing the content type. */ public void setContentType(String contentType) { throw new UnsupportedOperationException( "The setContentType() method is not supported."); } /** * <p> Returns the size, in bytes, of this file. </p> * * @return The size of the file, in bytes. * @deprecated */ public int getFileSize() { long size = fileItem.getSize(); if (size > Integer.MAX_VALUE) { throw new IllegalStateException("Size is greater than 2 GB; use getFileLength()"); } return (int) size; } /** * <p> Sets the size, in bytes, for this file. <p> NOTE: This method * is not supported in this implementation. </p> * * @param filesize The size of the file, in bytes. * @deprecated */ public void setFileSize(int filesize) { throw new UnsupportedOperationException( "The setFileSize() method is not supported."); } /** * <p> Returns the length of this file. </p> * * @return The length of the file, in bytes. * @throws IllegalStateException if size is greater than 2GB */ public long getFileLength() { return fileItem.getSize(); } /** * <p> Sets the length, in bytes, for this file. <p> NOTE: This method * is not supported in this implementation. </p> * * @param fileLength The length of the file, in bytes. */ public void setFileLength(long fileLength) { throw new UnsupportedOperationException( "The setFileLength() method is not supported."); } /** * <p> Returns the (client-side) file name for this file. </p> * * @return The client-size file name. */ public String getFileName() { return getBaseFileName(fileItem.getName()); } /** * <p> Sets the (client-side) file name for this file. <p> NOTE: This * method is not supported in this implementation. </p> * * @param fileName The client-side name for the file. */ public void setFileName(String fileName) { throw new UnsupportedOperationException( "The setFileName() method is not supported."); } /** * <p> Returns the data for this file as a byte array. Note that this * may result in excessive memory usage for large uploads. The use of * the {@link #getInputStream() getInputStream} method is encouraged * as an alternative. </p> * * @return An array of bytes representing the data contained in this * form file. * @throws FileNotFoundException If some sort of file representation * cannot be found for the FormFile * @throws IOException If there is some sort of IOException */ public byte[] getFileData() throws FileNotFoundException, IOException { return fileItem.get(); } /** * <p> Get an InputStream that represents this file. This is the * preferred method of getting file data. </p> * * @throws FileNotFoundException If some sort of file representation * cannot be found for the FormFile * @throws IOException If there is some sort of IOException */ public InputStream getInputStream() throws FileNotFoundException, IOException { return fileItem.getInputStream(); } /** * <p> Destroy all content for this form file. Implementations should * remove any temporary files or any temporary file data stored * somewhere </p> */ public void destroy() { fileItem.delete(); } /** * <p> Returns the base file name from the supplied file path. On the * surface, this would appear to be a trivial task. Apparently, * however, some Linux JDKs do not implement <code>File.getName()</code> * correctly for Windows paths, so we attempt to take care of that * here. </p> * * @param filePath The full path to the file. * @return The base file name, from the end of the path. */ protected String getBaseFileName(String filePath) { // First, ask the JDK for the base file name. String fileName = new File(filePath).getName(); // Now check for a Windows file name parsed incorrectly. int colonIndex = fileName.indexOf(":"); if (colonIndex == -1) { // Check for a Windows SMB file path. colonIndex = fileName.indexOf("\\\\"); } int backslashIndex = fileName.lastIndexOf("\\"); if ((colonIndex > -1) && (backslashIndex > -1)) { // Consider this filename to be a full Windows path, and parse it // accordingly to retrieve just the base file name. fileName = fileName.substring(backslashIndex + 1); } return fileName; } /** * <p> Returns the (client-side) file name for this file. </p> * * @return The client-size file name. */ public String toString() { return getFileName(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -