我正在使用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>
with:
<bean id="multipartResolver"
class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>
我还尝试了Apache的CommonSmultipartresolver,但问题是相同的。
我得到的是"应用程序/文本平台:格式化",而不是"应用程序/PDF"。我用几个PDF文件(来自不同来源)进行了测试。当我尝试上传其他文件类型(例如Word Document)时,它可以按预期工作(对于Word文档,我获得"应用程序/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),之后:"
在.svg文件中也有相同的问题。
通过修改 Mozilla Firefox mimetypes.rdf 文件中的文件解决了它。(上面的thx到piotrs链接)
对于其他有类似问题的人。这是由于我的PHP脚本中错误设置的内容类型而引起的。首次测试脚本(通过PHP间接传递SVG文件)时,我在标题()中设置了另一个内容类型。
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认为它看到了"新"文件/filetype-并将其添加到其配置中。
要解决此问题,您只需要删除mimetypes.rdf中的错误XML部分,当然也重新启动FF。