HttpServletRequest:如何确定所请求资产的Content-Type [英] HttpServletRequest: How to determine Content-Type of requested asset

查看:222
本文介绍了HttpServletRequest:如何确定所请求资产的Content-Type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jetty编写一个快速而又静态的静态文件服务器.我需要为每个响应设置Content-Type:

I am writing a quick-and-dirty static file server using Jetty. I need to set the Content-Type for each response:

HttpServletResponse.setContentType("content/type");

对于给定的文件请求,如何可靠地确定要设置的内容类型?是检查文件扩展名的问题,还是有更完善的方法? (例如,如果没有文件扩展名,或者扩展名不反映内容类型怎么办?)

For a given file request, how do I reliably determine what content type to set? Is it a question of examining the file extension, or is there a more sophisticated method? (E.g what if there is no file extension, or the extension does not reflect the content type?)

感谢理查德

推荐答案

您难道不可以只检查请求标头Content-Type并为此设置响应吗?

Can't you just check the request header Content-Type and set the response to that?

我不知道您打算支持哪种类型的客户端,但是如果它们是浏览器,我想您应该会满意的.如果您控制客户,则最好还是将标头发送给您.

I don't know the kind of clients you are planning to support but if they are browsers I guess you should be fine with that. If you control the clients, it's a good practice that they send you that header anyways.

祝你好运!

这篇关于HttpServletRequest:如何确定所请求资产的Content-Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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