使用Stripes / Tomcat对流式CSV进行错误编码 [英] Bad encoding of streamed CSV with Stripes / Tomcat

查看:335
本文介绍了使用Stripes / Tomcat对流式CSV进行错误编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我试图流式传输CSV文件。我设置编码为Windows-1252,但它似乎仍然流作为UTF-8文件。

  final String encoding = windows-1252; 
exportResolution = new StreamingResolution(builder.getContentType()+; charset =+ encoding.toLowerCase()){
@Override
public void stream(HttpServletResponse response)throws Exception {
//设置响应头
response.setHeader(Cache-control,private,max-age = 0);
response.setCharacterEncoding(encoding);
OutputStream os = response.getOutputStream();
writeExportStream(os,builder);
}
} .setFilename(filename);

writeExportStream只是将内容流传输到输出流(使用分页和db调用,需要一些时间) / p>

它在本地(jetty插件)+ dev(tomcat)中不起作用,不能使用firefox / chrome



我没有测试,但工作的人告诉我,当我们不流传输内容,但我们写一个文件,在加载了所有的对象,我们想要的数据库后,它的工作更好。



任何人都知道发生了什么?感谢



Btw我的标题:

  HTTP / 1.1 200 OK 
Content-Language:fr-FR
Content-Type:text / csv; charset = windows-1252
Content-Disposition:attachment; filename =export_rshop_01-02-11.csv
Cache-Control:private,max-age = 0
传输编码:chunked
服务器:Jetty(6.1.14)

我想要的文件可以在windows-1252中导入excel,但我不能,它只是在utf8打开,而我的标题是windows-1252

解决方案

问题出在 writeExportStream(os,builder); 我们不能看到它正在执行什么编码操作,但我猜它正在编写UTF-8数据。






输出操作需要执行两个编码任务:


  1. 告诉客户端响应文本的编码(通过头) li>
  2. 使用匹配的编码(例如通过作者)将数据写入客户端

1正确完成。步骤2可能是错误的根源。



如果您使用提供的作者,它会以相应的响应编码对字符数据进行编码。



如果预编码数据是通过原始字节流写入的( getOutputStream()),您需要确保此过程使用相同的编码。


Actually i'm trying to stream a CSV file. I set the encoding to windows-1252 but it seems it is still streamed as UTF-8 file.

   final String encoding = "windows-1252";
   exportResolution = new StreamingResolution(builder.getContentType() + ";charset=" + encoding.toLowerCase()) {
        @Override
        public void stream(HttpServletResponse response) throws Exception {
            // Set response headers
            response.setHeader("Cache-control", "private, max-age=0");
            response.setCharacterEncoding(encoding);
            OutputStream os = response.getOutputStream();
            writeExportStream(os,builder);
        }
    }.setFilename(filename);

writeExportStream just streams the content to the outputstream (with pagination and db calls, it takes some time)

It doesn't work in local (jetty plugin) + dev (tomcat) Neither with firefox / chrome

I've not tested but people at work told me that it works better when we don't stream the content but we write the file in one time after having loaded all the objets we want from db.

Anybody know what is happening? Thanks

Btw my headers:

HTTP/1.1 200 OK
Content-Language: fr-FR
Content-Type: text/csv;charset=windows-1252
Content-Disposition: attachment;filename="export_rshop_01-02-11.csv"
Cache-Control: private, max-age=0
Transfer-Encoding: chunked
Server: Jetty(6.1.14)

I want the file to be able to be imported in excel in windows-1252 but i can't, it just open in utf8 while my header is windows-1252

解决方案

The problem lies in the writeExportStream(os,builder); method. We can't see what encoding operations it is performing, but I'm guessing it is writing UTF-8 data.


The output operation needs to perform two encoding tasks:

  1. Tell the client what encoding the response text is in (via the headers)
  2. Encode the data writen to the client in a matching encoding (e.g. via a writer)

Step 1 is being done correctly. Step 2 is probably the source of the error.

If you use the provided writer, it will encode character data in the appropriate response encoding.

If pre-encoded data is written via the raw byte stream (getOutputStream()), you need to make sure this process uses the same encoding.

这篇关于使用Stripes / Tomcat对流式CSV进行错误编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆