/**
*
*/
package *;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
@SuppressWarnings("unchecked")
public class Util {
/**
* 字符串替换,将 source 中的 oldString 全部换成 newString
*
* @param source 源字符串
* @param oldString 老的字符串
* @param newString 新的字符串
* @return 替换后的字符串
*/
public static String replace(String source, String oldString, String newString) {
StringBuffer output = new StringBuffer();
int lengthOfSource = source.length(); // 源字符串长度
int lengthOfOld = oldString.length(); // 老字符串长度
int posStart = 0; // 开始搜索位置
int pos; // 搜索到老字符串的位置
while ((pos = source.indexOf(oldString, posStart)) >= 0) {
output.append(source.substring(posStart, pos));
output.append(newString);
posStart = pos + lengthOfOld;
}
if (posStart < lengthOfSource) {
output.append(source.substring(posStart));
}
return output.toString();
}
/**
* Convenience method to return a String array as a delimited (e.g. CSV) String. E.g. useful for toString()
* implementations.
*
* @param arr array to display. Elements may be of any type (toString will be called on each element).
* @param delim delimiter to use (probably a ,)
*/
public static String arrayToDelimitedString(Object[] arr, String delim) {
if (arr == null) {
return "null";
} else {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < arr.length; i++) {
if (i > 0) {
sb.append(delim);
}
sb.append(arr[i]);
}
return sb.toString();
}
}
/**
* 将空字符串转成默认字符串
*
* @param str 要转化的字符串
* @param dfValue 默认的字符串
* @return 转化后的字符串
*/
public static String dealNull(String str, String dfValue) {
if (str == null) {
return dfValue;
} else {
return str;
}
}
/**
* 将空字符串转成空字符串
*
* @param str 要转化的字符串
* @return 转化后的字符串
*/
public static String dealNull(String str) {
if (str == null) {
return "";
} else {
return str;
}
}
public static Object dealNull(Object obj) {
if (obj == null) {
return (Object) ("");
} else {
return obj;
}
}
/**
* 取消Html标记
*
* @param str 原始文本
* @return 取消Html标记后的文本
*/
public static String htmlEncode(String str) {
if (str != null) {
str = replace(str, "&", "&");
str = replace(str, "&amp;", "&");
str = replace(str, "&quot;", """);
str = replace(str, "\"", """);
str = replace(str, "&lt;", "<");
str = replace(str, "<", "<");
str = replace(str, "&gt;", ">");
str = replace(str, ">", ">");
str = replace(str, "&nbsp;", " ");
// str = replace(str," "," ");
}
return str;
}
/**
* 功能:格式化当前日期成长整型字符串
*
* @return 返回长整型日期时间字符串
*/
public static String longStringDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
return sdf.format(new Date());
}
/**
* 取文件扩展名
*
* @param fileName 文件名
* @return 文件扩展名
*/
public static String getFileExt(String fileName) {
String value = new String();
int start = 0;
int end = 0;
if (fileName == null)
return null;
start = fileName.lastIndexOf('.') + 1;
end = fileName.length();
value = fileName.substring(start, end);
if (fileName.lastIndexOf('.') > 0)
return value;
else
return "";
}
/**
* 远程下载文件
*
* @param destUrl
* @param fileName
* @return
*/
public static boolean remoteDownload(String destUrl, String fileName) {
boolean succeed = true;
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[8096];
int size = 0;
try {
// 建立链接
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();
// 连接指定的资源
httpUrl.connect();
if (httpUrl.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("连接资源文件地址[" + destUrl + "]失败.");
succeed = false;
}
// 获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
// 建立文件
fos = new FileOutputStream(fileName);
System.out.println("正在获取链接[" + destUrl + "]的内容...\n将其保存为文件[" + fileName + "]");
if (httpUrl.getResponseCode() != 200) {
System.out.println("获取文件链接[" + destUrl + "]失败.");
succeed = false;
}
// 保存文件
while ((size = bis.read(buf)) != -1) {
fos.write(buf, 0, size);
}
System.out.println("资源[" + url + "]下载完毕!!!");
}
catch (IOException e) {
System.out.println("资源[" + url + "]下载失败!!!");
succeed = false;
}
finally {
try {
if (fos != null) {
fos.close();
}
if (bis != null) {
bis.close();
}
}
catch (IOException ex) {
System.out.println("关闭资源数据流失败!!!");
}
httpUrl.disconnect();
}
return succeed;
}
/**
* @param arrVec
* @param arrayName
* @param exclude to filter
* @return
*/
public static String getScriptArray(Vector arrVec, String arrayName, String exclude[]) {
return getScriptArray(arrVec, arrayName, exclude, false, false);
}
/**
* @param arrVec
* @param arrayName
* @param allFlag ALL it shows
* @return
*/
public static String getScriptArray(Vector arrVec, String arrayName, boolean allFlag) {
return getScriptArray(arrVec, arrayName, null, allFlag, false);
}
/**
* @param arrVec
* @param arrayName
* @param exclude to filter
* @param intFlag ----- it shows
* @return
*/
public static String getScriptArray(Vector arrVec, String arrayName, String exclude[], boolean intFlag) {
return getScriptArray(arrVec, arrayName, exclude, false, intFlag);
}
/**
* JSP script
*
* <pre>
* Vector vecUsgeYn = codeMgr.getValues("UseYN");
* Util.getScriptArray(vecUserLevel, "arrUserLevel", true)
* Util.getScriptArray(vecUserLevel, "arrUserLevel", null, true)
* Util.getScriptArray(vecUserLevel, "arrUserLevel", null, false)
* Util.getScriptArray(vecUserLevel, "arrUserLevel", null, true, false)
* </pre>
*
* @param arrVec data interface
* @param arrayName name of script
* @param exclude to filter
* @param allFlag ALL it shows
* @param initFlag ---- it shows
* @return
*/
public static String getScriptArray(Vector arrVec, String arrayName, String exclude[], boolean allFlag,
boolean initFlag) {
StringBuffer sb = new StringBuffer();
sb.append(arrayName + " = new Array ( \r\n");
if (allFlag)
sb.append("\tnew Array('ALL',''),\r\n");
else if (initFlag)
sb.append("\tnew Array('-------------',''),\r\n");
for (int idx = 0; idx < arrVec.size(); idx++) {
String tmpStr[] = (String[]) arrVec.elementAt(idx);
if (exclude != null) {
boolean flag = false;
for (int jdx = 0; jdx < exclude.length; jdx++) {
if (tmpStr[0].equals(exclude[jdx]))
flag = true;
}
if (!flag)
sb.append("\tnew Array(\"" + tmpStr[1] + "\",\"" + tmpStr[0] + "\"),\r\n");
} else {
sb.append("\tnew Array(\"" + tmpStr[1] + "\",\"" + tmpStr[0] + "\"),\r\n");
}
}
return sb.substring(0, sb.length() - 3) + ");\n";
}
}
common
最新推荐文章于 2024-10-02 22:02:29 发布