Firefox的PDF文件的内容类型不正确 [英] Incorrect content type for PDF file with Firefox

查看:71
本文介绍了Firefox的PDF文件的内容类型不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring 3.2.0.M2,并且正在尝试上传pdf文件.

I am using Spring 3.2.0.M2 and I am trying to upload a pdf file.

JSP文件:

<form method="POST" action="upload" enctype="multipart/form-data>
  <input type="file" name="file" />
</form>

控制器:

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleUpload(@RequestParam MultipartFile file) {
    System.out.println(file.getContentType());
    //prints "application/text-plain:formatted"
}

我正在使用标准的多部分解析器:

I am using the Standard multipart resolver :

<servlet>
  <servlet-name>servlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  <multipart-config />
</servlet>

与:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>

我还尝试了Apache的CommonsMultipartResolver,但问题是相同的.

I also tried with CommonsMultipartResolver from Apache but the problem is the same.

我得到的是"application/text-plain:formatted",而不是"application/pdf".我测试了几个pdf文件(来自不同来源).当我尝试上传其他文件类型(例如Word文档)时,它可以按预期工作(对于Word文档,我会收到"application/msword").

What I am getting is "application/text-plain:formatted" instead of "application/pdf". I tested with several pdf file (from different sources). When I try to upload another file type (e.g. Word document), it works as expected (for a Word document, I get "application/msword").

我打算将内容类型(和文件名)存储到数据库中,以便以后检索和下载文件.拥有不正确的内容类型会在调用setContentType时引发异常:

I intend to store the content type (and the file name) into a database for later retrieval and download of the file. Having an incorrect content type is then causing an exception when calling setContentType like this :

public void downloadResource(@RequestParam("resId") Long resourceId, HttpServletResponse response) {
    // get resource with its id.
    response.setContentType(resource.getContentType());
    //throws org.apache.tomcat.util.http.parser.TokenMgrError
}

异常消息是:

第1行第23列出现词法错误,在:"之后出现::"(58)

Lexical error at line 1, column 23. Encountered: ":" (58), after : ""

推荐答案

.svg文件存在相同的问题.

Had the same problem with a .svg file.

通过修改 Mozilla Firefox ="a href =" http://kb.mozillazine.org/MimeTypes.rdf"rel =" nofollow> mimeTypes.rdf 来解决个人资料文件夹中的a> 文件.(向上述Piotrs链接致谢)

Solved it by modifying the Mozilla Firefox mimeTypes.rdf file in the profile folder. (thx to Piotrs Link above)

对于其他有类似问题的人.这是由于我的PHP脚本中错误设置了Content-Type引起的.当第一次测试脚本(它通过php间接传送了一个svg文件)时,我在header()中设置了另一个Content-Type.

For other people having a similar problem. This was caused due to a mistakenly set Content-Type in my PHP script. When first testing the script (it delivered a svg file indirectly via php) i had another Content-Type set in the header().

header("Content-Type: image/svg+xml"); //should have been
header("Content-Type: application/vnd.android.package-archive"); //was actually set

现在,此设置否决了firefox mimeTypes.rdf文件中的内容类型设置:

This now overruled the content type setting in the firefox mimeTypes.rdf file:

  <RDF:Description RDF:about="urn:mimetype:application/vnd.android.package-archive"
                   NC:value="application/vnd.android.package-archive"
                   NC:editable="true"
                   NC:fileExtensions="svg"
                   NC:description="Scalable Vector Graphics">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:application/vnd.android.package-archive"/>
  </RDF:Description>

可能是因为FF认为它看到的是新"文件/文件类型-并将其添加到配置中.

Probably because FF thinks its seeing a "new" file/filetype - and adding it to its config.

要解决此问题,您只需删除mimeTypes.rdf中错误的XML部分,然后重新启动FF.

To fix this, you only need to delete the wrong XML section in the mimeTypes.rdf and restart FF of course.

这篇关于Firefox的PDF文件的内容类型不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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