IDEA插件 FindBugs-IDEA异常积累

IDEA插件 FindBugs-IDEA异常积累

1. DM_DEFAULT_ENCODING

Found a call to a method which will perform a byte to String (or String to byte) conversion, and will assume that the default platform encoding is suitable. This will cause the application behaviour to vary between platforms. Use an alternative API and specify a charset name or Charset object explicitly.

对将执行字节到String(或String to byte)转换的方法的调用,并假设默认平台编码是合适的。这将导致应用程序行为在平台之间变化。使用备用API并显式指定charset名称或Charset对象。

1.1 Found reliance on default encoding : new java.io.InputStreamReader(InputStream)

new BufferedReader(new InputStreamReader(connection.getInputStream()));

 修改为:

 InputStreamReader fileData = new InputStreamReader(file ,"utf-8");

1.2 Found reliance on default encoding : new java.io.PrintWriter(OutputStream)

out = new PrintWriter(conn.getOutputStream());

 修改为:

 out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "utf-8"));

1.3 Found reliance on default encoding : String.getBytes()

fileName = new String(req.getParameter("fileName").getBytes(), "UTF-8");

修改为

fileName = new String(req.getParameter("fileName").getBytes("UTF-8"), "UTF-8");

1.4 Found reliance on default encoding: java.io.ByteArrayOutputStream.toString()

logger.info("RECV STR: " + baos.toString());

修改为

logger.info("RECV STR: " + baos.toString("utf-8"));

1.5 Found reliance on default encoding : new java.io.FileWriter(File)

new FileWriter(f).append(baos.toString("UTF-8")).close();

修改为

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(ftrue))); 

out.write(baos.toString("UTF-8")); 

out.close();

BufferedWriter bw= new BufferedWriter( new OutputStreamWriter(new FileOutputStream(filePath, true), "utf-8"));

1.6 Found reliance on default encoding: new java.io.FileReader(String)

FileReader in = new FileReader(file);

改为

BufferedReader reader = new BufferedReader(new InputStreamReader(newFileInputStream(file), "UTF-8")); 

1.7 Found reliance on default encoding: new String(byte[]) 

 //content为byte[]

System.out.print(new String(content));   

修改为:

System.out.println(new String(content,"utf-8"));


版权声明:本文为qq_39615245原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。