我正在尝试用阿拉伯语创建CSV文件列,我正在使用资源包从属性文件中获取值,并且它能够在阿拉伯语中获取值,但在创建文件后,列名称显示为 “??????????????”而不是阿拉伯语。请帮我解决这个问题。
我正在使用以下代码:
public static void downloadCSVForReconcilationData(String downloadStr,HttpServletResponse response,String reportName) throws IOException{
//response.setHeader("Content-Disposition", "inline;filename=\""+"BillingTrxReport.csv"+"\"");
response.setContentType ("application/x-csv");
response.setHeader("Content-Disposition","attachment; filename=\""+reportName+".csv"+"\";");
//response.setHeader("application/vnd.ms-excel;charset=Cp1256","attachment; filename=\""+reportName+".csv"+"\";");// khushwant Patidar
response.setContentType("application/vnd.ms-excel;charset=Cp1256");// khushwant Patidar
response.setContentLength((int) downloadStr.length());
try{
OutputStream out = response.getOutputStream();
PrintWriter pw = new PrintWriter(out);
BufferedWriter bufw=new BufferedWriter(pw);
//BufferedOutputStream outBuf = new BufferedOutputStream(out);
bufw.write(downloadStr);
bufw.close();
pw.close();
out.flush();
out.close();
}catch(IOException ioe){
ioe.printStackTrace();
throw ioe;
}
}
答案 0 :(得分:2)
如果您在内容类型中使用CP1256,则在使用java.io.PrintStream创建文件时必须使用相同的内容。
我建议:
OutputStream out = response.getOutputStream();
PrintStream ps = new PrintStream( out, true, "CP1256" );
ps.println( downloadStr );
ps.close();