如何在 servlet 上设置内容类型 [英] How to set the content type on the servlet
问题描述
我正在使用一个简单的 servlet,它将数据库中的文档内容作为字节数组发回.我想设置一个内容 type 以便在通过 doGet() 调用检索时具有适当的扩展名.
我确实将文档的类型作为元数据存储在数据库中(例如 png、gif、png、xls、docx ...).
- 我应该设置什么内容类型才能保留文件扩展名?
- 下载的文件名为doc",如何在 servlet 上为正在下载的数据设置文件名.
我应该设置什么内容类型才能保留文件扩展名?
使用ServletContext#getMimeType()
根据文件名获取mime类型.
String mimeType = getServletContext().getMimeType(filename);
servletcontainer 通常已经在它自己的 web.xml
中提供了默认的 MIME 类型映射.如果你想覆盖或添加一些其他的,那么把它作为新的 mime 映射放在 webapp 的 web.xml
中.例如
<extension>docx</extension><mime-type>application/vnd.openxmlformats-officedocument.wordprocessingml.document</mime-type></mime-mapping><mime-mapping><扩展名>xlsx</扩展名><mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type></mime-mapping>
最后将其设置为Content-Type
响应头:
response.setContentType(mimeType);
<小时><块引用>
下载的文件名为doc",如何在 servlet 上为正在下载的数据设置文件名.
将其添加到 servlet URL,因为某些浏览器(如 MSIE)会忽略内容处置的 filename
属性.
下载文件名.ext
如果servlet映射到/download/*
的URL模式,则可以通过如下方式获取
String filename = request.getPathInfo().substring(1);
最后将其设置在 Content-Disposition
标头中,以使普通浏览器满意:
response.setHeader("Content-Disposition", "attachment; filename="" + filename + """);
如果您不在 DB 中存储文件名,而是将 ID 或其他内容存储在数据库中,则将其用作文件名.
<a href="download/${file.id}.${file.ext}">download ${file.id}.${file.ext}</a>
然后在servlet中
String filename = request.getPathInfo().substring(1);String id = filename.split("\.")[0];//根据 id 从 DB 获取.
I am using a simple servlet which sends back document contents from the database as a byte array. I would like to set a content type so that it has an appropriate extension while it is being retrieved via a doGet() call.
I do have the type of the document stored as a metadata in the database (e.g. png, gif, png, xls, docx ...).
- What should I set as the content type so that it retains the file extension?
- The file gets downloaded with a name of "doc", how do I set the filename on the servlet for the data being downloaded.
What should I set as the content type so that it retains the file extension?
Use ServletContext#getMimeType()
to get the mime type based on the file name.
String mimeType = getServletContext().getMimeType(filename);
The servletcontainer usually already provides a default mime type mapping in its own web.xml
. If you want to overridde or add some other, then put it as new mime mappings in webapp's web.xml
. E.g.
<mime-mapping>
<extension>docx</extension>
<mime-type>application/vnd.openxmlformats-officedocument.wordprocessingml.document</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xlsx</extension>
<mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type>
</mime-mapping>
Finally set it as the Content-Type
response header:
response.setContentType(mimeType);
The file gets downloaded with a name of "doc", how do I set the filename on the servlet for the data being downloaded.
Add it to the servlet URL because some browsers like MSIE ignores the filename
attribute of the content disposition.
<a href="download/filename.ext">download filename.ext</a>
If the servlet is mapped on an URL pattern of /download/*
, then you can obtain it as follows
String filename = request.getPathInfo().substring(1);
Finally set it in the Content-Disposition
header as well to make normal browsers happy:
response.setHeader("Content-Disposition", "attachment; filename="" + filename + """);
If you don't store filenames in DB but rather IDs or something, then use it as filename instead.
<a href="download/${file.id}.${file.ext}">download ${file.id}.${file.ext}</a>
And then in the servlet
String filename = request.getPathInfo().substring(1);
String id = filename.split("\.")[0];
// Obtain from DB based on id.
这篇关于如何在 servlet 上设置内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!