List of usage examples for org.apache.commons.fileupload ParameterParser ParameterParser
public ParameterParser()
From source file:com.oneis.appserver.FileUploads.java
/** * Determine whether an HTTP request contains file uploads. If so, returns a * FileUploads object initialised for decoding the stream. * * @return FileUploads object to use to decode the streamed data. */// w w w . ja va2 s . c om static public FileUploads createIfUploads(HttpServletRequest request) { String contentType = request.getHeader("Content-Type"); if (contentType == null) { return null; } // Set up a parser for the various values ParameterParser paramParser = new ParameterParser(); paramParser.setLowerCaseNames(true); // Decode content type Map contentTypeDecoded = paramParser.parse(contentType, PARAMPARSER_SEPERATORS); String boundaryStr = null; if (!contentTypeDecoded.containsKey("multipart/form-data") || (boundaryStr = (String) contentTypeDecoded.get("boundary")) == null) { // Wasn't a file upload return null; } byte[] boundary = null; try { boundary = boundaryStr.getBytes("ISO-8859-1"); } catch (java.io.UnsupportedEncodingException e) { throw new RuntimeException("Expected charset ISO-8859-1 not installed"); } if (boundaryStr == null || boundary.length < 2) { return null; } // Looks good... create an object to signal success return new FileUploads(boundary); }
From source file:at.gv.egiz.bku.binding.XWWWFormUrlInputDecoder.java
@Override public void setContentType(String contentType) { ParameterParser pp = new ParameterParser(); pp.setLowerCaseNames(true);/*from w ww. ja v a2s . com*/ Map<String, String> params = pp.parse(contentType, new char[] { ':', ';' }); if (!params.containsKey(CONTENT_TYPE)) { throw new IllegalArgumentException("not a url encoded content type specification: " + contentType); } }
From source file:at.gv.egiz.bku.binding.HttpUtil.java
/** * Extracts charset from a content type header. * /*from w w w . j av a 2s . c om*/ * @param contentType * @param replaceNullWithDefault * if true the method return the default charset if not set * @return charset String or null if not present */ public static String getCharset(String contentType, boolean replaceNullWithDefault) { ParameterParser pf = new ParameterParser(); pf.setLowerCaseNames(true); Map<?, ?> map = pf.parse(contentType, SEPARATOR); String retVal = (String) map.get(CHAR_SET); if ((retVal == null) && (replaceNullWithDefault)) { if (map.containsKey(APPLICATION_URL_ENCODED)) { // default charset for url encoded data return "UTF-8"; } retVal = getDefaultCharset(); } return retVal; }
From source file:com.gae.MemoryFileItem.java
@SuppressWarnings("unchecked") public String getCharSet() { ParameterParser parser = new ParameterParser(); parser.setLowerCaseNames(true);/*from w ww . ja v a 2s.c o m*/ // Parameter parser can handle null input Map params = parser.parse(getContentType(), ';'); return (String) params.get("charset"); }
From source file:com.alibaba.citrus.service.upload.impl.cfu.ServletFileUpload.java
private String getFileName(String pContentDisposition) { String fileName = null;//from w ww. j av a 2s .co m if (pContentDisposition != null) { String cdl = pContentDisposition.toLowerCase(); if (cdl.startsWith(FORM_DATA) || cdl.startsWith(ATTACHMENT)) { ParameterParser parser = new ParameterParser(); parser.setLowerCaseNames(true); // Parameter parser can handle null input @SuppressWarnings("unchecked") Map<String, String> params = parser.parse(pContentDisposition, ';'); // Flashfilename fname ? for (String key : getFileNameKey()) { fileName = trimToNull(params.get(key)); if (fileName != null) { break; } } } } return fileName; }
From source file:com.aspectran.web.support.multipart.inmemory.MemoryFileItem.java
/** * Returns the content charset passed by the agent or {@code null} if not defined. * * @return the content charset passed by the agent or {@code null} if not defined *//*from ww w . j a v a2 s . c o m*/ @SuppressWarnings("unchecked") public String getCharSet() { ParameterParser parser = new ParameterParser(); parser.setLowerCaseNames(true); // Parameter parser can handle null input Map<String, String> params = parser.parse(getContentType(), ';'); return params.get("charset"); }
From source file:cn.clxy.studio.common.web.multipart.GFileItem.java
/** * Returns the content charset passed by the agent or <code>null</code> if not defined. * * @return The content charset passed by the agent or <code>null</code> if not defined. */// w ww .j a v a2 s . c o m public String getCharSet() { ParameterParser parser = new ParameterParser(); parser.setLowerCaseNames(true); // Parameter parser can handle null input Map<String, String> params = parser.parse(getContentType(), ';'); return params.get("charset"); }
From source file:act.data.ApacheMultipartParser.java
/** * Retrieves the boundary from the <code>Content-type</code> header. * * @param contentType The value of the content type header from which to extract the * boundary value.//w ww . j ava 2s.c om * @return The boundary, as a byte array. */ private byte[] getBoundary(String contentType) { ParameterParser parser = new ParameterParser(); parser.setLowerCaseNames(true); // Parameter parser can handle null input Map params = parser.parse(contentType, ';'); String boundaryStr = (String) params.get("boundary"); if (boundaryStr == null) { return null; } byte[] boundary; try { boundary = boundaryStr.getBytes("ISO-8859-1"); } catch (UnsupportedEncodingException e) { boundary = boundaryStr.getBytes(); } return boundary; }
From source file:act.data.ApacheMultipartParser.java
/** * Retrieves the file name from the <code>Content-disposition</code> * header.//from ww w. ja va2 s. co m * * @param headers A <code>Map</code> containing the HTTP request headers. * @return The file name for the current <code>encapsulation</code>. */ private String getFileName(Map /* String, String */ headers) { String fileName = null; String cd = getHeader(headers, CONTENT_DISPOSITION); if (cd != null) { String cdl = cd.toLowerCase(); if (cdl.startsWith(FORM_DATA) || cdl.startsWith(ATTACHMENT)) { ParameterParser parser = new ParameterParser(); parser.setLowerCaseNames(true); // Parameter parser can handle null input Map params = parser.parse(cd, ';'); if (params.containsKey("filename")) { fileName = (String) params.get("filename"); if (fileName != null) { fileName = fileName.trim(); // IE7 returning fullpath name (#300920) if (fileName.indexOf('\\') != -1) { fileName = fileName.substring(fileName.lastIndexOf('\\') + 1); } } else { // Even if there is no value, the parameter is present, // so we return an empty file name rather than no file // name. fileName = ""; } } } } return fileName; }
From source file:com.oneis.appserver.FileUploads.java
/** * Handle the incoming stream, processing files. *///from w ww .j a v a2 s . co m public void performUploads(HttpServletRequest request) throws IOException, UserReportableFileUploadException { instructionsRequired = false; // Need a parser for parameters ParameterParser paramParser = new ParameterParser(); paramParser.setLowerCaseNames(true); // Thread ID is used for temporary filenames long threadId = Thread.currentThread().getId(); int fileId = 0; InputStream requestBody = request.getInputStream(); MultipartStream multipart = new MultipartStream(requestBody, boundary); multipart.setHeaderEncoding("UTF-8"); boolean nextPart = multipart.skipPreamble(); while (nextPart) { String headerPart = multipart.readHeaders(); // Parse headers, splitting out the bits we're interested in String name = null; String filename = null; Map<String, String> itemHeaders = HeaderParser.parse(headerPart, true /* keys to lower case */); String mimeType = itemHeaders.get("content-type"); String disposition = itemHeaders.get("content-disposition"); if (disposition != null) { Map disp = paramParser.parse(disposition, PARAMPARSER_SEPERATORS); name = (String) disp.get("name"); filename = (String) disp.get("filename"); } // Set a default MIME type if none is given (Safari may omit it) if (mimeType == null) { mimeType = "application/octet-stream"; } // Remove the path prefix from IE (before the length check) if (filename != null) { int slash1 = filename.lastIndexOf('/'); int slash2 = filename.lastIndexOf('\\'); int slash = (slash1 > slash2) ? slash1 : slash2; if (slash != -1) { filename = filename.substring(slash + 1); } } boolean isFile = (filename != null && filename.length() > 0); if (isFile) { // File - did the app server give instructions about it? Upload upload = files.get(name); if (upload == null) { // Looks like a file, but the app server didn't say it should be. Give up. throw new UserReportableFileUploadException( "A file was uploaded, but it was not expected by the application. Field name: '" + name + "'"); } String dir = upload.getSaveDirectory(); if (dir == null) { // Ooops. throw new IOException("app server didn't specify dir"); } // Generate a temporary filename File outFile = null; do { outFile = new File(String.format("%1$s/u%2$d.%3$d", dir, threadId, fileId++)); } while (!outFile.createNewFile()); OutputStream outStream = new FileOutputStream(outFile); // Decorate with a digest? MessageDigest digest = null; if (upload.getDigestName() != null) { try { digest = MessageDigest.getInstance(upload.getDigestName()); } catch (java.security.NoSuchAlgorithmException e) { digest = null; } if (digest != null) { outStream = new DigestOutputStream(outStream, digest); } } // Decorate with a decompressor? String filterName = upload.getFilterName(); if (filterName != null && filterName.equals("inflate")) { outStream = new InflaterOutputStream(outStream); } multipart.readBodyData(outStream); outStream.close(); String digestAsString = null; if (digest != null) { String.format("%1$s_digest", name); digestAsString = StringUtils.bytesToHex(digest.digest()); } upload.setUploadDetails(outFile.getPath(), digestAsString, mimeType, filename, outFile.length()); } else { // Normal field - just absorb a few k max of it, turn it into a field ByteArrayOutputStream value = new ByteArrayOutputStream(); // TODO: Limit request size as a whole, not on a per-parameter basis. multipart.readBodyData(new LimitedFilterOutputStream(value, MAX_TEXT_PARAMETER_LENGTH)); params.put(name, value.toString("UTF-8")); } nextPart = multipart.readBoundary(); } }