Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring MVC:下载文件的编码错误_Java_Spring_Google Chrome_Spring Mvc_Servlets - Fatal编程技术网

Java Spring MVC:下载文件的编码错误

Java Spring MVC:下载文件的编码错误,java,spring,google-chrome,spring-mvc,servlets,Java,Spring,Google Chrome,Spring Mvc,Servlets,我试图理解使用SpringMVCServlet下载pdf文件时的一种奇怪行为 以下是用于下载文件的控制器代码: @RequestMapping(value = "/handler/{id}", method = RequestMethod.GET) public HttpEntity<byte[]> report(@PathVariable("id") Long id, HttpServletResponse response, HttpServle

我试图理解使用SpringMVCServlet下载pdf文件时的一种奇怪行为

以下是用于下载文件的控制器代码:

@RequestMapping(value = "/handler/{id}", method = RequestMethod.GET)
public HttpEntity<byte[]> report(@PathVariable("id") Long id, 
        HttpServletResponse response,
        HttpServletRequest request) {
    byte[] bytes = service.reportById(id);

    return DownloadUtil.downloadFile(response, "application/pdf",
           "Filename.pdf", bytes);
}

public static HttpEntity<byte[]> downloadFile(
    final HttpServletResponse response, 
    final String contentType, 
    final String fileName, 
    final byte[] item){

    HttpHeaders header = new HttpHeaders();
    header.setContentType(MediaType.valueOf(contentType));
    header.set("Content-Disposition", "inline; filename=\"" + fileName +"\"");
    header.set("Content-Transfer-Encoding", "application/octet-stream");  

    header.setContentLength(item.length);
    return new HttpEntity<byte[]>(item, header);
}   
响应:

GET /path/19649/download HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
Referer: http://path/byDitta
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it,en-US;q=0.8,en;q=0.6
Cookie: JSESSIONID=09CEA1438ACED879CDD96877BB536022; _ga=GA1.1.2013320496.1416898514
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Disposition: inline; filename="file.pdf"
Content-Transfer-Encoding: application/octet-stream
Content-Type: application/pdf;charset=UTF-8
Content-Length: 296750
Date: Tue, 10 Mar 2015 09:39:05 GMT 
GET /path/19649/download HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Referer: /path/19649/download
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it,en-US;q=0.8,en;q=0.6
Cookie: JSESSIONID=09CEA1438ACED879CDD96877BB536022; _ga=GA1.1.2013320496.1416898514
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Disposition: inline; filename="file.pdf"
Content-Transfer-Encoding: application/octet-stream
Content-Type: application/pdf;charset=UTF-8
Content-Length: 296750
Date: Tue, 10 Mar 2015 09:40:29 GMT
当我保存pdf查看器中显示的pdf时,我有以下请求/响应标题:

请求:

GET /path/19649/download HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
Referer: http://path/byDitta
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it,en-US;q=0.8,en;q=0.6
Cookie: JSESSIONID=09CEA1438ACED879CDD96877BB536022; _ga=GA1.1.2013320496.1416898514
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Disposition: inline; filename="file.pdf"
Content-Transfer-Encoding: application/octet-stream
Content-Type: application/pdf;charset=UTF-8
Content-Length: 296750
Date: Tue, 10 Mar 2015 09:39:05 GMT 
GET /path/19649/download HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Referer: /path/19649/download
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it,en-US;q=0.8,en;q=0.6
Cookie: JSESSIONID=09CEA1438ACED879CDD96877BB536022; _ga=GA1.1.2013320496.1416898514
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Disposition: inline; filename="file.pdf"
Content-Transfer-Encoding: application/octet-stream
Content-Type: application/pdf;charset=UTF-8
Content-Length: 296750
Date: Tue, 10 Mar 2015 09:40:29 GMT
响应:

GET /path/19649/download HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
Referer: http://path/byDitta
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it,en-US;q=0.8,en;q=0.6
Cookie: JSESSIONID=09CEA1438ACED879CDD96877BB536022; _ga=GA1.1.2013320496.1416898514
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Disposition: inline; filename="file.pdf"
Content-Transfer-Encoding: application/octet-stream
Content-Type: application/pdf;charset=UTF-8
Content-Length: 296750
Date: Tue, 10 Mar 2015 09:39:05 GMT 
GET /path/19649/download HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Referer: /path/19649/download
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it,en-US;q=0.8,en;q=0.6
Cookie: JSESSIONID=09CEA1438ACED879CDD96877BB536022; _ga=GA1.1.2013320496.1416898514
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Disposition: inline; filename="file.pdf"
Content-Transfer-Encoding: application/octet-stream
Content-Type: application/pdf;charset=UTF-8
Content-Length: 296750
Date: Tue, 10 Mar 2015 09:40:29 GMT
我认为区别在于
accept
request头。它在保存pdf请求中丢失

问题是,保存此文件时,它的编码错误,因此会导致损坏

奇怪的是,我在另一个项目中使用了~相同的代码来做相同的事情,并且它工作正常。所以我想,可能是servlet配置中的某些东西


如何强制正确下载编码?

如果您的pdf文件生成良好,我认为您应该尝试以下方法:

         @RequestMapping(value = "clients/city")
         @ResponseBody
private OutputStream getCity(HttpServletRequest request,HttpServletResponse   response) throws IOException, JRException {
  String path=request.getRealPath("resources/files");
  createFileService.SpravkaCity(path); 
 //      response.setContentType("text/plain");      
 //      response.setHeader("Content-Disposition", "attachment;   filename=reestr.xls");
  File f=new File(path+"/city.pdf");
  response.setContentType("application/pdf");
 //      response.setHeader("Content-Transfer-Encoding", "binary");
  response.setHeader("Content-Length", String.valueOf(f.length()));
  response.setHeader("Content-Disposition", "inline; filename=city.pdf");
  Path p = Paths.get(path+"/city.pdf");
  response.getOutputStream().write(Files.readAllBytes(p));
  return response.getOutputStream();
} 
希望它能对您有所帮助。

来自评论:

比较文件大小,工作pdf为227403字节,另一个为303206字节

这表明数据是正确的。我不明白为什么会这样;大多数情况下,当服务器认为客户机无法处理二进制数据时(如执行AJAX请求时),就会产生这种效果

[编辑]安装一个类似的代理服务器,这样可以查看服务器发送到浏览器的原始数据。试着只创建一个小的PDF文件,使之更容易


使用这些工具,您可以找出谁对数据进行了编码。

您能给我们看一下相应的响应标题吗?特别是检查“编码”和“字符集”之类的东西。我猜这个文件是用gzip压缩的,你需要解压缩它。谢谢@AaronDigulla我已经在我的问题中添加了详细信息。@AaronDigulla我试图解压缩它,但没有成功。比较文件大小,工作pdf为227403字节,另一个为303206字节bytes@gipinani你试过不同的浏览器了吗?您还可以将Tomcat conf/server.xml和JVM选项与另一台服务器的选项进行比较,以查找WRT编码提示
application/pdf;charset=UTF-8
毫无意义,因为PDF是二进制格式<代码>内容传输编码不是标准的HTTP头,
应用程序/octet流
是MIME类型,而不是编码。也许Chrome会被这些问题弄糊涂。谢谢你的回答。从客户端来看,这不会是我的错误,因为我选中了ChromePDF查看器的保存按钮。我也不明白为什么在另一个项目中使用相同的代码。。例如,Firefox工作良好,因为当按下save pdf时,它不会发出另一个请求,而是保存pdf的本地副本。乔姆打了两个电话。一个用于预览,一个用于保存对象。我认为强制下载应该可以解决这个问题,但是在viewerIs Chrome的第二个请求中看到pdf会有不同吗?是的,问题中显示了这一点。也许还不清楚。我给你看第一个请求和第二个请求,在那里我显示了头啊,我明白了!下一步应该是查看服务器发送到浏览器的原始数据。试着创建一个较小的PDF文件,并寻找可以安装在PC上的代理服务器,就像如果线路上的数据错误,开始调试服务器一样。我尝试了各种组合,我遇到了这个问题,但仅适用于IE11。你的回答引起了我的思考,所以我通过在rest调用中添加.pdf解决了这个问题——“myApp/rest/public/v1/downloadPdf.pdf”。难以置信,但它成功了。实际上不应该需要它。