/** * Append a query string to an URL. This adds a '?' or a '&' or nothing, as needed. * * @param urlString existing URL string * @param queryString query string, or null * @return resulting URL */ public static String appendQueryString(String urlString, String queryString) { if (org.apache.commons.lang.StringUtils.isBlank(queryString)) { return urlString; } else { final StringBuilder updatedActionStringBuilder = new StringBuilder(urlString); updatedActionStringBuilder.append((urlString.indexOf('?') == -1) ? '?' : '&'); updatedActionStringBuilder.append(queryString); return updatedActionStringBuilder.toString(); } }
/** * 下载文件的时候会使用的方法,把硬盘里的数据读取出来,然后再解析成 byte[]数组,以便下面再往客户端去写数据 * * @return * @throws IOException */ public byte[] getFileContent(String filePath) throws IOException { StringBuilder builder = new StringBuilder(); FileReader fr = new FileReader(filePath); BufferedReader br = new BufferedReader(fr); String readLineStr = null; while ((readLineStr = br.readLine()) != null) { builder.append(readLineStr); } String fileContent = builder.toString(); return Base64.decode(fileContent); }
/** * Encode a Human Readable Resource Identifier to a URI. Leading and trailing spaces are removed * first. * * <p>NOTE: See more recent W3C note: http://www.w3.org/TR/2008/NOTE-leiri-20081103/ * * @param uriString URI to encode * @param processSpace whether to process the space character or leave it unchanged * @return encoded URI, or null if uriString was null */ public static String encodeHRRI(String uriString, boolean processSpace) { if (uriString == null) return null; // Note that the XML Schema spec says "Spaces are, in principle, allowed in the ·lexical space· // of anyURI, // however, their use is highly discouraged (unless they are encoded by %20).". // We assume that we never want leading or trailing spaces. You can use %20 if you really want // this. uriString = uriString.trim(); // We try below to follow the "Human Readable Resource Identifiers" RFC, in draft as of // 2007-06-06. // * the control characters #x0 to #x1F and #x7F to #x9F // * space #x20 // * the delimiters "<" #x3C, ">" #x3E, and """ #x22 // * the unwise characters "{" #x7B, "}" #x7D, "|" #x7C, "\" #x5C, "^" #x5E, and "`" #x60 final StringBuilder sb = new StringBuilder(uriString.length() * 2); for (int i = 0; i < uriString.length(); i++) { final char currentChar = uriString.charAt(i); if (currentChar >= 0 && (currentChar <= 0x1f || (processSpace && currentChar == 0x20) || currentChar == 0x22 || currentChar == 0x3c || currentChar == 0x3e || currentChar == 0x5c || currentChar == 0x5e || currentChar == 0x60 || (currentChar >= 0x7b && currentChar <= 0x7d) || (currentChar >= 0x7f && currentChar <= 0x9f))) { sb.append('%'); sb.append(NumberUtils.toHexString((byte) currentChar).toUpperCase()); } else { sb.append(currentChar); } } return sb.toString(); }
/** * 返回FileRepositoryDTO类 * * @return * @throws IOException s * @see FileRepositoryDTO */ public FileRepositoryDTO getFileContentWithFileRepository( String filePath, FileRepository fileRepository) throws IOException { StringBuilder builder = new StringBuilder(); filePath = Config.UPLOAD_FILE_PATH + "/" + filePath + "/" + fileRepository.getId() + ".xzsoft"; FileReader fr = new FileReader(filePath); BufferedReader br = new BufferedReader(fr); String readLineStr = null; while ((readLineStr = br.readLine()) != null) { builder.append(readLineStr); } String fileContent = builder.toString(); FileRepositoryDTO dto = new FileRepositoryDTO(); BeanMapper.copy(fileRepository, dto); dto.setContent(Base64.decode(fileContent)); return dto; }
/** Encode a query string. The input Map contains names indexing Object[]. */ public static String encodeQueryString(Map parameters) { final StringBuilder sb = new StringBuilder(100); boolean first = true; try { for (Object o : parameters.keySet()) { final String name = (String) o; final Object[] values = (Object[]) parameters.get(name); for (final Object currentValue : values) { if (currentValue instanceof String) { if (!first) sb.append('&'); sb.append(URLEncoder.encode(name, NetUtils.STANDARD_PARAMETER_ENCODING)); sb.append('='); sb.append( URLEncoder.encode((String) currentValue, NetUtils.STANDARD_PARAMETER_ENCODING)); first = false; } } } } catch (UnsupportedEncodingException e) { // Should not happen as we are using a required encoding throw new OXFException(e); } return sb.toString(); }
/** Combine a path info and a parameters map to form a path info with a query string. */ public static String pathInfoParametersToPathInfoQueryString(String pathInfo, Map parameters) throws IOException { final StringBuilder redirectURL = new StringBuilder(pathInfo); if (parameters != null) { boolean first = true; for (Object o : parameters.keySet()) { final String name = (String) o; final Object[] values = (Object[]) parameters.get(name); for (final Object currentValue : values) { if (currentValue instanceof String) { redirectURL.append(first ? "?" : "&"); redirectURL.append(URLEncoder.encode(name, NetUtils.STANDARD_PARAMETER_ENCODING)); redirectURL.append("="); redirectURL.append( URLEncoder.encode((String) currentValue, NetUtils.STANDARD_PARAMETER_ENCODING)); first = false; } } } } return redirectURL.toString(); }