* Moved getSubHeader() method from FileUpload to MimePart and made it public static
* Check charset in MimePart.getText() * Increase default uploadLimit in FileUpload to 4 MB
This commit is contained in:
		
							parent
							
								
									c55a192d5d
								
							
						
					
					
						commit
						710dd12e17
					
				
					 2 changed files with 45 additions and 31 deletions
				
			
		|  | @ -31,7 +31,7 @@ public class FileUpload { | |||
|      * Creates a new FileUpload object. | ||||
|      */ | ||||
|     public FileUpload() { | ||||
|         maxKbytes = 1024; | ||||
|         maxKbytes = 4096; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|  | @ -67,7 +67,7 @@ public class FileUpload { | |||
|               throws Exception { | ||||
|         parts = new Hashtable(); | ||||
| 
 | ||||
|         String boundary = getSubHeader(contentType, "boundary"); | ||||
|         String boundary = MimePart.getSubHeader(contentType, "boundary"); | ||||
| 
 | ||||
|         if (boundary == null) { | ||||
|             throw new MimeParserException("Error parsing MIME input stream."); | ||||
|  | @ -107,8 +107,8 @@ public class FileUpload { | |||
| 
 | ||||
|             String type = headers.getValue("Content-Type"); | ||||
|             String disposition = headers.getValue("Content-Disposition"); | ||||
|             String name = getSubHeader(disposition, "name"); | ||||
|             String filename = getSubHeader(disposition, "filename"); | ||||
|             String name = MimePart.getSubHeader(disposition, "name"); | ||||
|             String filename = MimePart.getSubHeader(disposition, "filename"); | ||||
| 
 | ||||
|             if (filename != null) { | ||||
|                 int sep = filename.lastIndexOf("\\"); | ||||
|  | @ -133,28 +133,4 @@ public class FileUpload { | |||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     private String getSubHeader(String header, String subHeaderName) { | ||||
|         if (header == null) { | ||||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|         String retval = null; | ||||
|         StringTokenizer headerTokenizer = new StringTokenizer(header, ";"); | ||||
| 
 | ||||
|         while (headerTokenizer.hasMoreTokens()) { | ||||
|             String token = headerTokenizer.nextToken().trim(); | ||||
|             int i = token.indexOf("="); | ||||
| 
 | ||||
|             if (i > 0) { | ||||
|                 String hname = token.substring(0, i).trim(); | ||||
| 
 | ||||
|                 if (hname.equalsIgnoreCase(subHeaderName)) { | ||||
|                     retval = token.substring(i + 1).replace('"', ' ').trim(); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return retval; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -18,6 +18,7 @@ package helma.util; | |||
| 
 | ||||
| import java.io.*; | ||||
| import java.util.Date; | ||||
| import java.util.StringTokenizer; | ||||
| 
 | ||||
| /** | ||||
|  * This represents a MIME part of a HTTP file upload | ||||
|  | @ -87,9 +88,17 @@ public class MimePart implements Serializable { | |||
|      */ | ||||
|     public String getText() { | ||||
|         if ((contentType == null) || contentType.startsWith("text/") | ||||
|                                   || contentType.equals("application/text")) { | ||||
|             // FIXME: check for encoding | ||||
|             return new String(content); | ||||
|                                   || contentType.startsWith("application/text")) { | ||||
|             String charset = getSubHeader(contentType, "charset"); | ||||
|             if (charset != null) { | ||||
|                 try { | ||||
|                     return new String(content, charset); | ||||
|                 } catch (UnsupportedEncodingException uee) { | ||||
|                     return new String(content); | ||||
|                 } | ||||
|             } else { | ||||
|                 return new String(content); | ||||
|             } | ||||
|         } else { | ||||
|             return null; | ||||
|         } | ||||
|  | @ -151,4 +160,33 @@ public class MimePart implements Serializable { | |||
|             return null; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      *  Get a sub-header from a header, e.g. the charset from | ||||
|      *  <code>Content-Type: text/plain; charset="UTF-8"</code> | ||||
|      */ | ||||
|     public static String getSubHeader(String header, String subHeaderName) { | ||||
|         if (header == null) { | ||||
|             return null; | ||||
|         } | ||||
| 
 | ||||
|         StringTokenizer headerTokenizer = new StringTokenizer(header, ";"); | ||||
| 
 | ||||
|         while (headerTokenizer.hasMoreTokens()) { | ||||
|             String token = headerTokenizer.nextToken().trim(); | ||||
|             int i = token.indexOf("="); | ||||
| 
 | ||||
|             if (i > 0) { | ||||
|                 String hname = token.substring(0, i).trim(); | ||||
| 
 | ||||
|                 if (hname.equalsIgnoreCase(subHeaderName)) { | ||||
|                     String value = token.substring(i + 1); | ||||
|                     return value.replace('"', ' ').trim(); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return null; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue