JSP的CSV导出(多语言 无乱码)

今天Support的时候遇到个烦人的问题,CSV出力数据的时候泰文乱码。找了半天才发现错误出在使用FileWriter读写临时文件的时候不能设置“utf-8”编码。所以就此改用OutputStreamWriter。

 

在Java中,如果我们用java.io.FileReader或者java.io.FileWriter来读写文件的话,固然我们可以通过 java.io.BufferedReader和java.io.BufferedWriter来提高效率,但是我们会发现在FileReader和 FileWriter中我们只能获取编码方式,而不能设置。这样,在FileReader和FileWriter中的编码设置只能服从于一些更为底层的设置,那么在读写多种语言编码的文件时就很容易出现乱码。

另外一种方法就是采用java.io.FileInputStream/java.io.InputStreamReader和 java.io.FileOutputStream/java.io.OutputStreamWriter来解决这个问题。在 InputStreamReader和OutputStreamWriter中,可以通过指定编码方式来完成UTF-8文件的读写。

比如:
java.io.BufferedWriter writer = null;
java.io.FileOutputStream writerStream = new java.io.FileOutputStream(filename);   
writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(writerStream, "UTF-8")); 
// do something
// writing file
writer.close(); 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/socrates/archive/2008/07/27/2721018.aspx

 

修改以后试验了多种文字均未出现问题:) 所以记录一下以备不时之需。

 

<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><%@ include file="../../../webadmin.jsp" %><%@ page import="jp.co.exxaxon.bean.ActionHistoryBean"%><%@ page import="jp.co.exxaxon.bean.DownloadCSVFormBean"%><%@ page import="java.io.BufferedOutputStream" %><%@ page import="java.io.BufferedInputStream" %><%@ page import="java.io.FileOutputStream" %><%@ page import="java.io.File" %><%@ page import ="java.util.Date"%><%@ page import="java.io.BufferedWriter"%><%@ page import="jp.co.exxaxon.logic.ContentDBLogic"%><% DownloadCSVFormBean ah_bean = (DownloadCSVFormBean)session.getAttribute("INPUT_FORM_DATA"); String country = ah_bean.getCountry(); String from_day = ah_bean.getFrom_day(); String from_month = ah_bean.getFrom_month(); String from_year = ah_bean.getFrom_year(); String to_day = ah_bean.getTo_day(); String to_month = ah_bean.getTo_month(); String to_year = ah_bean.getTo_year(); if(!"".equals(from_day)&&!"".equals(from_month)&&!"".equals(from_year)&& !"".equals(to_day)&&!"".equals(to_month)&&!"".equals(to_year)){ //----edit by EXXaXon0 20090714 S------- String f_date = from_year+"/"+from_month+"/"+from_day; String t_date = to_year+"/"+to_month+"/"+to_day; ArrayList cols = new ArrayList(); cols.add("update_time"); cols.add("update_time"); String clause = ""; if("all".equals(country)){ clause = "where ### <='"+t_date+" 23:59:59' and ### >='"+f_date+" 00:00:00'"; }else{ cols.add("country"); clause = "where ### <='"+t_date+" 23:59:59' and ### >='"+f_date+" 00:00:00' and ### ='"+country+"'"; } //----edit by EXXaXon0 20090714 E------- ContentDBLogic db_logic = new ContentDBLogic(db); ArrayList result = db_logic.selectWithCustomClause(ActionHistoryBean.TABLE_NAME,cols,clause); try { // Create temp file. File temp = File.createTempFile("pattern", ".suffix"); // Delete temp file when program exits. temp.deleteOnExit(); // Write to temp file //----edit by EXXaXon0 20090806 S------- FileOutputStream writerStream = new FileOutputStream(temp); BufferedWriter fout = new BufferedWriter(new java.io.OutputStreamWriter(writerStream, "UTF-8")); //----edit by EXXaXon0 20090806 E------- StringBuffer content = new StringBuffer(1024); // printout title content.append("Date Arrange:").append(f_date).append("-").append(t_date); fout.write(content.toString().replaceAll("/n","").replaceAll("n","").replaceAll("/r","").replaceAll("r","")); fout.newLine(); content. //append("ID").append(","). append("Content_ID").append(","). append("Content_Title").append(","). append("Content_Group").append(","). append("User_Group").append(","). append("User_Name").append(","). append("Action_Type").append(","). append("Update_Time").append(","). append("Country"); fout.write(content.toString().replaceAll("/n","").replaceAll("n","").replaceAll("/r","").replaceAll("r","")); fout.newLine(); if(result == null || result.isEmpty()){ }else{ for(int i = 0 ;i

你可能感兴趣的:(JAVA)