1.特殊字符问题 CSV上传文件因为是BOOM表头 所以存在特殊字符/uFEFF 如果用作敏感词过滤 包含特殊字符的内容无法进行过滤 //去除空格 str.replace(" ", “”); //csv特殊字符处理上传csv str.replace(“\uFEFF”, “”);
2.导入时乱码问题 判断是否乱码 /** * 判断字符串是否是乱码 * * @param strName 字符串 * @return 是否是乱码 / public static boolean isMessyCode(String strName) { Pattern p = Pattern.compile(Constant.IS_MESSY_CODE); Matcher m = p.matcher(strName); String after = m.replaceAll(""); String temp = after.replaceAll("\p{P}", “”); char[] ch = temp.trim().toCharArray(); float chLength = ch.length; float count = 0; for (int i = 0; i < ch.length; i++) { char c = ch[i]; if (!Character.isLetterOrDigit?) { if (!isChinese?) { count = count + 1; } } } float result = count / chLength; if (result > 0.4) { return true; } else { return false; } } /* * 判断字符是否是中文 * * @param c 字符 * @return 是否是中文 */ public static boolean isChinese(char c) { Character.UnicodeBlock ub = Character.UnicodeBlock.of?; if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { return true; } return false; }
3.导出时乱码问题 用下面这种方法导出 private final static String CODE_FORMART =“GB2312”;
/**
* 导出 (流)
* @param strs
* @param response
* @param fileName
*/
public static void write(List<String> strs, HttpServletResponse response, String fileName) {
response.setContentType("application/csv;charset =" + CODE_FORMART);
fileName = fileName + Constant.CSV;
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
PrintWriter out = null;
try {
out = response.getWriter();
writeCSV(out,strs);
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
out = null;
if (out != null) {
out.close();
out = null;
}
}
}
private static void writeCSV(PrintWriter bos,List csvList){
Iterator it=csvList.iterator();
while(it.hasNext()){
bos.println( it.next() );
}
}
- 导出时报response.setContentType(“application/msword;charset=GB2312”);错误解决
返回时不设置返回值即可解决
|