我使用下面的代码让用户在我的网页上上传csv文件。csv文件包含如下信息:
12345,account,password,ABC,Tom,0
12346,account,password,ABC,Jerry,0
12347,account,password,ABC,Mary,0
doPost.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletFileUpload upload = new ServletFileUpload();
upload.setHeaderEncoding("UTF-8");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
String type = "";
String mode = "";
String name = "";
String remark = "";
String id = "";
Enumeration params = request.getParameterNames();
while (params.hasMoreElements())
{
String param = (String) params.nextElement();
if (param.equals("type")) type = request.getParameter(param);
if (param.equals("mode")) mode = request.getParameter(param);
if (param.equals("name")) name = request.getParameter(param);
if (param.equals("remark")) remark = request.getParameter(param);
if (param.equals("id")) id = request.getParameter(param);
}
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext())
{
FileItemStream item = iterator.next();
if (!item.isFormField())
{
InputStream stream = item.openStream();
//try print stream
BufferedReader lesen = new BufferedReader(new InputStreamReader(stream));
String line = lesen.readLine();
while(line!=null)
{
System.out.println("stream: "+line);
line = lesen.readLine();
}
if (type.equals("csv"))
{
List<BaseModel> devices = CsvParser.csv2ListBaseModel(stream);
}
}
}
}
system.out.println会打印出csv文件中不正确的内容:
Stream: 嚜?2345,account,password,ABC,Tom,0
Stream: 12346,account,password,ABC,Jerry,0
Stream: 12347,account,password,ABC,Mary,0
而CsvParser.csv2ListBaseModel(stream)也会返回不正确的内容。
import dk.lindhardt.gwt.geie.server.CSV2TableLayout;
import dk.lindhardt.gwt.geie.shared.Cell;
import dk.lindhardt.gwt.geie.shared.TableLayout;
public class CsvParser
{
public static List<BaseModel> csv2ListBaseModel(InputStream stream)
{
CSV2TableLayout csv2TableLayout = new CSV2TableLayout(stream);
TableLayout tableLayout = csv2TableLayout.build();
List<Cell> cells = tableLayout.getCells();
List<BaseModel> devices = new ArrayList<BaseModel>();
BaseModel device = null;
for (int row = 0; row < tableLayout.rows(); row++)
{
device = new BaseModel();
for (int column = 0; column < tableLayout.columns(); column++)
{
String value = null;
try
{
value = (String) tableLayout.getCell(row, column).getValue();
} catch (NullPointerException npe)
{
//
}
device.set(column + "", value);
}
devices.add(device);
}
return devices;
}
}
最后,当我将设备存储到数据库中时,第一行的第一个字(12345)将变成?12345csv文件为UTF-8编码。任何建议都很感激。由于
这个隐藏的字节称为BOM,用于标识unicode文件的字节顺序。
无论如何,你可以从你的字符串中删除它,例如:
yourString = yourString.replace("uFEFF", "");
另外,如果你在SO或Google中搜索关于从输出中删除bom的信息,你会发现很多资源:
http://www.javapractices.com/topic/TopicAction.do?Id=257读取UTF-8 - BOM标记