過濾特殊字元java
1. java怎麼限制不可錄入特殊符號
打開MyEclipse工具,並編寫一個類「Test」
編寫過濾方法,方法的代御斗扒碼如下:
public static String encoding(String src) {
if (src == null)
return "";
StringBuilder result = new StringBuilder();
if (src != null) {
src = src.trim();
for (int pos = 0; pos <銷薯 src.length(); pos++) {
switch (src.charAt(pos)) {
case '\"':
result.append(""");
break;
case '<':
result.append("<");
break;
case '>':
result.append("鎮昌>");
break;
case '\'':
result.append("'");
break;
/*case '&':
result.append("&");
break;*/
case '%':
result.append("&pc;");
break;
case '_':
result.append("&ul;");
break;
case '#':
result.append("&shap;");
break;
case '?':
result.append("&ques;");
break;
default:
result.append(src.charAt(pos));
break;
}
}
}
return result.toString();
}
編寫main方法,mian方法如下:
/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
String s="%>我是特殊";
System.out.println(t.encoding(s));
}
4
運行結果:&pc;>我是特殊
2. jsp\java 如何編寫過濾器過濾特殊字元
package com.jing.common;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class IllegalCharacterFilter implements Filter {
private String[] characterParams = null;
private boolean OK=true;
public void init(FilterConfig config) throws ServletException {
// if(config.getInitParameter("characterParams").length()<1)
// OK=false;
// else
// this.characterParams = config.getInitParameter("characterParams").split(",");
System.out.println("初粗褲始化");
}
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest servletrequest = (HttpServletRequest) request;
HttpServletResponse servletresponse = (HttpServletResponse) response;
String param = "";
String paramValue = "";
//設置請求編碼格式
servletresponse.setContentType("text/html");
servletresponse.setCharacterEncoding("UTF-8");
servletrequest.setCharacterEncoding("UTF-8");
java.util.Enumeration params = request.getParameterNames();
//循環讀取參數
while (params.hasMoreElements()){
param = (String) params.nextElement(); //獲取請求中的基棗參數
String[] values = servletrequest.getParameterValues(param);//獲得每個參數對應的值
for (int i = 0; i < values.length; i++) {
paramValue = values[i];
//轉換目標字元變成對象字元,可以多個。後搏凳拆期擴展特殊字元庫用於管理
paramValue = paramValue.replaceAll("'","");
paramValue = paramValue.replaceAll("@","");
paramValue = paramValue.replaceAll("胡錦濤","***");
//這里還可以增加,如領導人 自動轉義成****,可以從資料庫中讀取非法關鍵字。
values[i] = paramValue;
}
//把轉義後的參數重新放回request中
request.setAttribute(param, paramValue);
}
//繼續向下 執行請求,如果有其他過濾器則執行過濾器
arg2.doFilter(request, response);
}
public void destroy() {
// TODO Auto-generated method stub
}
}
3. JAVA中如何過濾字元串裡面特殊字元
zenkill的回答應該是對的吧。
難道你的錯誤類似下面這樣?
錯誤原因已經註明。
class outoffunction
{
public int i;
i=100; //方法外面不應該對數據進行操作。
public static void main()
{
System.out.print(i);
}
}
4. java io 讀入文件 怎麼過濾特殊字元
可考慮將文件裝換為流來處理,或者將文禪帶件讀取到的字元串轉換為byte數組來處理
以byte單位,
根據byte的范圍來確認是否是特殊字賣滲符,替換為空賀配蘆字元。
或者使用正則匹配文件讀取到的字元串,替換為空字元。
5. jsp\java 如何編寫過濾器過濾特殊字元
package com.jing.common;x0dx0aimport java.io.IOException;x0dx0aimport java.io.PrintWriter;x0dx0aimport javax.servlet.Filter;x0dx0aimport javax.servlet.FilterChain;x0dx0aimport javax.servlet.FilterConfig;x0dx0aimport javax.servlet.ServletException;x0dx0aimport javax.servlet.ServletRequest;x0dx0aimport javax.servlet.ServletResponse;x0dx0aimport javax.servlet.http.HttpServletRequest;x0dx0aimport javax.servlet.http.HttpServletResponse;x0dx0ax0dx0apublic class IllegalCharacterFilter implements Filter {x0dx0a private String[] characterParams = null;x0dx0a private boolean OK=true;x0dx0a x0dx0a public void init(FilterConfig config) throws ServletException {x0dx0a x0dx0a// if(config.getInitParameter("characterParams").length()<1)x0dx0a// OK=false;x0dx0a// elsex0dx0a/飢態/ this.characterParams = config.getInitParameter("characterParams").split(",");x0dx0a System.out.println("初始化");x0dx0a }x0dx0ax0dx0a@SuppressWarnings("unchecked")x0dx0a public void doFilter(ServletRequest request, ServletResponse response,x0dx0a FilterChain arg2) throws IOException, ServletException {x0dx0a HttpServletRequest servletrequest = (HttpServletRequest) request;x0dx0a HttpServletResponse servletresponse = (HttpServletResponse) response;x0dx0a String param = "";x0dx0a String paramValue = "";x0dx0a x0dx0a /爛如源/設置請求編碼格式x0dx0a servletresponse.setContentType("text/html");x0dx0a servletresponse.setCharacterEncoding("UTF-8");x0dx0a servletrequest.setCharacterEncoding("UTF-8");x0dx0a java.util.Enumeration params = request.getParameterNames();x0dx0a //循環讀取參數x0dx0a while (params.hasMoreElements()){x0dx0a param = (String) params.nextElement(); /橡鄭/獲取請求中的參數x0dx0a String[] values = servletrequest.getParameterValues(param);//獲得每個參數對應的值x0dx0a x0dx0a for (int i = 0; i < values.length; i++) {x0dx0a x0dx0a paramValue = values[i];x0dx0a x0dx0a //轉換目標字元變成對象字元,可以多個。後期擴展特殊字元庫用於管理x0dx0a paramValue = paramValue.replaceAll("'",""); x0dx0a paramValue = paramValue.replaceAll("@","");x0dx0a paramValue = paramValue.replaceAll("胡錦濤","***");x0dx0a x0dx0a //這里還可以增加,如領導人 自動轉義成****,可以從資料庫中讀取非法關鍵字。x0dx0a values[i] = paramValue;x0dx0a x0dx0a }x0dx0a x0dx0a //把轉義後的參數重新放回request中x0dx0a request.setAttribute(param, paramValue);x0dx0a }x0dx0a //繼續向下 執行請求,如果有其他過濾器則執行過濾器x0dx0a arg2.doFilter(request, response);x0dx0a }x0dx0a x0dx0a public void destroy() {x0dx0a // TODO Auto-generated method stubx0dx0a }x0dx0a}
6. java 處理特殊字元
首先你的存放的txt文件的編碼格式要支持哪個特殊字元(utf-8一般都支持),然後就寫就是正常io的讀寫了
7. JAVA特殊字元過濾方法
public static String StringFilter(String str) throws PatternSyntaxException {
// 只允許字母和數字
// String regEx = "[^a-zA-Z0-9]";
// 清除掉所有特殊字元
String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】『;:」「』。,、?]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}