如何从servlet在文件系统中提供图像文件?从servlet中的文件系统提供静态图像文件?
3
A
回答
2
看一看:
Example Depot: Returning an Image in a Servlet
链接断了。 Wayback机器复制下面插入:
// This method is called by the servlet container to process a GET request.
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Get the absolute path of the image
ServletContext sc = getServletContext();
String filename = sc.getRealPath("image.gif");
// Get the MIME type of the image
String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
sc.log("Could not get MIME type of "+filename);
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
// Set content type
resp.setContentType(mimeType);
// Set content size
File file = new File(filename);
resp.setContentLength((int)file.length());
// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}
0
那么这是怎样的一个耻辱的是Servlet规范并没有明确的方式做到这一点,除非图像位于Web应用程序目录下。 Servlet容器通常不会建议他们专有的方法来做到这一点。显然,容器必须这样做才能提供文件,为什么它不公开功能?为什么不是HttpServletResponse.sendFile(File)
?
最好的办法是创建符号链接,以便您的文件显示在webapp目录下。
相关问题
- 1. 关于从Servlet中提供静态文件的问题
- 2. 网络文件系统存储静态图像/文件
- 3. Django,提供静态文件
- 4. Django提供静态文件
- 5. NGINX提供静态文件
- 6. Sinatra提供静态文件
- 7. 404当试图从文件系统提供文件
- 8. 从servlet过滤器提供静态文件
- 9. 从文件夹中提供静态文件里面的wwwroot
- 10. 从外部库提供静态文件
- 11. 无法从cherrypy提供静态文件
- 12. 从appfog提供静态文件
- 13. Django提供在CSS文件中定义的静态图像
- 14. 在groovy文件系统中的文件提供url和存储图像
- 15. 如何在节点中提供静态配置文件图像
- 16. 从文件系统提供上传文件的问题
- 17. 在OpenShift中提供Django静态文件
- 18. 在Meteor.js中提供静态文件
- 19. 如何提供静态文件动态?
- 20. 如何使用Web Servlet提供静态HTML文件?
- 21. Heroku从本地文件系统提供文件
- 22. nancyfx - 从父文件夹提供静态文件
- 23. Python Eve从给定文件夹提供静态文件
- 24. Nginx的提供静态大文件
- 25. Nodejs任何文件系统提供商
- 26. 提供静态文件与泽西2
- 27. 用symfony3提供远程静态文件
- 28. 无法提供静态内容文件
- 29. Node.js +用RESTIFY提供静态文件
- 30. 无法提供静态文件,Django 1.6
什么是您的应用程序服务器?一些提供了一个干净的方式来定义一个Web应用程序发布静态内容,例如weblogic:http://blogs.oracle.com/middleware/2010/06/publish_static_content_to_weblogic.html – RealHowTo 2011-02-05 00:05:04
和Tomcat:http://stackoverflow.com/questions/1502841/reliable-data-serving/2662603#2662603 – BalusC 2011-02-05 00:26:58