📄 commonmethods.java
字号:
}
else{
String url = "jdbc:odbc:"+sDB;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(Exception e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
return null;
}
try {
con = DriverManager.getConnection(url,sUser, sPass);
} catch(Exception ex) {
System.err.println("CommonMethods SQLException1: " + ex.getMessage());
return null;
}
}
return con;
}
//for out a error message in HTML out;
//make html code;
static public String makeHtmlString(String sCode)
{
if(sCode==null)
return "";
StringBuffer sReturn=new StringBuffer (1024);
int nLength = sCode.length ();
char chari;
for( int i=0;i<nLength;i++ ){
chari = sCode.charAt(i);
if( chari<=0x20&&chari!=0xd&&chari!=0xa )
sReturn.append (" ");
if( chari==0xa )
sReturn.append ("<BR>");
if( chari==0xd )
continue;
if( chari=='\"' )
sReturn.append (""");
else if( chari=='&' )
sReturn.append ("&");
else if( chari=='>' )
sReturn.append (">");
else if( chari=='<' )
sReturn.append ("<");
else
sReturn.append (chari);
}
return sReturn.toString ();
}
static public String makeHtmlString1(String sCode)
{
if(sCode==null)
return "";
StringBuffer sReturn=new StringBuffer (1024);
sReturn.append("<p> ");
int nLength = sCode.length ();
char chari;
for( int i=0;i<nLength;i++ ){
chari = sCode.charAt(i);
if( chari<=0x20&&chari!=0xd&&chari!=0xa )
sReturn.append (" ");
if( chari==0xa )
sReturn.append ("<br>");
if( chari==0xd )
continue;
if( chari=='\"' )
sReturn.append (""");
else if( chari=='&' )
sReturn.append ("&");
else if( chari=='>' )
sReturn.append (">");
else if( chari=='<' )
sReturn.append ("<");
else
sReturn.append (chari);
}
sReturn.append("</p>");
return sReturn.toString ();
}
public static String DealWithGBCodeOfRequest(String sGB)
{
try {
byte[] sReguest = sGB.getBytes("8859_1");
if(nUseGBTypeInDB==1)
return new String (sReguest);
else{
System.out.println(sGB);
return sGB;
}
}catch(Exception e){
return sGB;
}
}
static String readIniFile( String sFile,String sKey ){
FileReader freader;
// 将模板文件读到 templateContents 中
try
{
freader = new FileReader(sFile);
}catch(Exception e)
{
GlobalError.setGlobalErr ("can't read init file!");
return null;
}
char[] contents = new char[200000];
int nSize = 0;
try
{
nSize = freader.read(contents,0,200000);
freader.close ();
}catch(Exception e)
{
GlobalError.setGlobalErr ("read init file!");
return null;
}
String templateContents = new String(contents,0,nSize);
int nBegin ,nEnd ;
nBegin = templateContents.indexOf (sKey)+sKey.length ();
if(nBegin==-1){
GlobalError.setGlobalErr ("read key : "+sKey+ " fail!" );
return null;
}
nEnd = templateContents.indexOf("\r\n",nBegin);
if(nEnd == -1)
nEnd = templateContents.length ();
String sPath = templateContents.substring (nBegin,nEnd);
return sPath;
}
//for error message of the global module ;
static int Global_ErrorID = 0;
static int getGlobalErrID(){
return Global_ErrorID;
}
static void setGlobalErrID( int nErrID ){
Global_ErrorID = nErrID;
}
//for get number value from string
//return number value from the string ,
//if fail , return the default value;
static public int GetInt(String sInt,int Default){
try{
return Integer.valueOf (sInt).intValue ();
}
catch(Exception e){
return Default;
}
}
static public double GetDouble(String sDouble,double Default){
try{
return Double.valueOf (sDouble).doubleValue ();
}
catch(Exception e){
return Default;
}
}
//for get parameter from http request
//return string from the request ,
//if fail , return null;
static public String getParameterValue(HttpServletRequest request,String sParam)
{
try{
String sResult = request.getParameter(sParam);
if( sResult!=null ){
return DealWithGBCodeOfRequest(sResult.trim());
}
return null;
}catch(Exception e){
return null;
}
}
//return string from the resultset ,
//if fail , return null;
static public String getFieldValue(ResultSet rs,String sField)
{
try{
String sResult = rs.getString (sField);
if( sResult!=null ){
return sResult.trim();
}
return null;
}catch(Exception e){
return null;
}
}
public static boolean WriteToDisk(byte[] sData , String sFile){
System.out.println (sFile);
if( sFile==null||sFile.length () == 0 ){
return false;
}
File file = new File(sFile);
while( file.exists ()&&!file.isDirectory () ){
sFile = addSomeToFileName(sFile);
file = new File(sFile);
}
try{
FileOutputStream tempFS = new FileOutputStream (sFile);
int nDataLength = sData.length ;
tempFS.write (sData,0,nDataLength);
tempFS.close ();
}
catch( Exception e){
System.out.println ("write error:"+e.toString() );
return false;
}
return true;
}
public static boolean WriteToDisk(byte[] sData , int nLeng , String sFile){
if( sFile==null||sFile.length () == 0 )
return false;
File file = new File(sFile);
while( file.exists ()&&!file.isDirectory () ){
sFile = addSomeToFileName(sFile);
file = new File(sFile);
}
try{
FileOutputStream tempFS = new FileOutputStream (sFile);
tempFS.write (sData,0,nLeng);
tempFS.close ();
}
catch( Exception e){
return false;
}
return true;
}
public static String addSomeToFileName(String sPath)
{
if( sPath==null )
return null;
int nIndex = sPath.lastIndexOf (""+File.separatorChar +"");
if( nIndex==-1 )
return null;
return sPath.substring (0,nIndex)+""+File.separatorChar +"_"+sPath.substring (nIndex+1,sPath.length ());
}
public static String FormatDouble(double value,int nTail)
{
String sV="";
StringBuffer sAdd = new StringBuffer();
sV = Double.toString (value);
if( sV.indexOf ('e')!=-1||sV.indexOf ('E')!=-1 )
return "0.0";
char code;
int nlength=sV.length ();
int nT = 0;
boolean bBeginTail = false;
for( int i=0;i<nlength;i++ ){
code = sV.charAt (i);
if( code=='.' ){
if( sAdd.length ()==0||sAdd.charAt (i-1 )=='-' )
sAdd.append ('0');
bBeginTail = true;
}
sAdd.append (code);
if( bBeginTail==true )
nT++;
if( nT > nTail )
break;
}
if(sAdd.length ()==0)
return "0";
return sAdd.toString ();
}
public static String FormatDoubleString(String sV,int nTail)
{
if(sV==null)
return "";
StringBuffer sAdd = new StringBuffer();
char code;
int nlength=sV.length ();
int nT = 0;
boolean bBeginTail = false;
for( int i=0;i<nlength;i++ ){
code = sV.charAt (i);
if( code=='.' ){
if( sAdd.length ()==0||sAdd.charAt (i-1 )=='-' )
sAdd.append ('0');
bBeginTail = true;
}
sAdd.append (code);
if( bBeginTail==true )
nT++;
if( nT > nTail )
break;
}
if(sAdd.length ()==0)
return "0";
return sAdd.toString ();
}
public static String DealWithSQLParameter(String sInfo)
{
if(sInfo==null)
return "";
StringBuffer sBuff=new StringBuffer ();
int nLeng = sInfo.length ();
char code;
for( int i=0;i<nLeng;i++)
{
code = sInfo.charAt (i);
if(code==0x27)
sBuff.append ((char)0x27);
sBuff.append (code);
}
return sBuff.toString ();
}
public static String sUrl365Mail = "http://www.365do.com/cgi-bin/mail/main.pl";
public static String GetUrl365Mail (){
return sUrl365Mail;
}
static String sMailHost = "http://www.365do.com";
public static String GetMailHost (){
return sMailHost;
}
public static String getUserEmailFile(String user)
{
String sDir = CommonMethods.GetUserEmaildir();
File Dir = new File (sDir);
if(Dir.exists ()==false )
Dir.mkdir ();
sDir = sDir+user;
Dir = new File (sDir);
if(Dir.exists ()==false )
Dir.mkdir ();
sDir = sDir+""+File.separatorChar +""+"EmailInfo";
Dir = new File (sDir);
if(Dir.exists ()==false )
Dir.mkdir ();
return sDir +""+File.separatorChar +"em.txt";
}
public static String ReadFromDisk(String FileName){
if( FileName==null||FileName.length () == 0 )
return null;
File tempFile=new File(FileName);
if( tempFile.exists ()==false || tempFile.canRead ()==false )
return null;
int nDataLength =(int)tempFile.length ();
byte[] sReturn ;
try{
FileInputStream tempFS = new FileInputStream (FileName);
sReturn = new byte[nDataLength];
tempFS.read (sReturn);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -