【问题标题】:Incorrect content type for PDF file with Firefox使用 Firefox 的 PDF 文件的内容类型不正确
【发布时间】:2012-10-27 16:11:15
【问题描述】:

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

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"
}

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

<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,但问题是一样的。

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

我打算将内容类型(和文件名)存储到数据库中,以便以后检索和下载文件。像这样调用 setContentType 时,内容类型不正确会导致异常:

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),在 :"" 之后

【问题讨论】:

标签: java spring spring-mvc firefox mime-types


【解决方案1】:

.svg 文件也有同样的问题。

通过修改profile folder中的Mozilla Firefox mimeTypes.rdf文件解决了这个问题。 (感谢上面的 Piotrs 链接)

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

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

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

  <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 认为它看到了“新”文件/文件类型 - 并将其添加到其配置中。

要解决这个问题,你当然只需要删除mimeTypes.rdf中错误的XML部分并重启FF。

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2015-05-22
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多