zl程序教程

您现在的位置是:首页 >  后端

当前栏目

字符串 java字符串编码转换处理类详解编程语言

JAVA转换编码编程语言 详解 处理 字符串
2023-06-13 09:20:29 时间

该字符串处理类包括将ISO-8859-1编码的字符串转换成GBK编码 、对输入的字符串进行一次编码转换,防止SQL注入和验证URL地址是否存在的方法。

字符串处理类(编码转化、SQL注入、URL)

import java.net.HttpURLConnection; 

import java.net.URL; 

public class StringUtils { 

 public String toGBK(String strvalue) { 

 try { 

 if (strvalue == null) { //当变量strvalue为null时 

 return ""; //将返回空的字符串 

 } else { 

 //将字符串转换为GBK编码 

 strvalue = new String(strvalue.getBytes("ISO-8859-1"), "GBK"); 

 return strvalue; //返回转换后的输入变量strvalue 

 } catch (Exception e) { 

 return ""; 

 // 对输入的字符串进行一次编码转换,防止SQL注入 

 public String StringtoSql(String str) { 

 if (str == null) { //当变量str为null时 

 return ""; //返回空的字符串 

 } else { 

 try { 

 //将号转换化为空格 

 str = str.trim().replace(/, (char) 32); } catch (Exception e) { 

 return ""; 

 return str; 

 //验证URL地址是否存在 

 public int isURLExist(String url){ 

 int rtn=0; 

 try { 

 URL u = new URL(url); 

 HttpURLConnection urlconn = (HttpURLConnection) u.openConnection(); 

 int state = urlconn.getResponseCode(); 

 if (state == 200) { //表示URL地址存在 

 //String succ = urlconn.getURL().toString(); 

 rtn=1; 

 } else { //表示URL地址不存在 

 rtn=0; 

 } catch (Exception e) { 

 rtn=0; 

 return rtn; 

}

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10458.html

cjava