如何從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. NGINX提供靜態文件
- 4. Sinatra提供靜態文件
- 5. 從servlet過濾器提供靜態文件
- 6. 從文件夾中提供靜態文件裏面的wwwroot
- 7. 無法從cherrypy提供靜態文件
- 8. Django提供在CSS文件中定義的靜態圖像
- 9. 在Meteor.js中提供靜態文件
- 10. 如何提供靜態文件動態?
- 11. 如何使用Web Servlet提供靜態HTML文件?
- 12. Heroku從本地文件系統提供文件
- 13. Apache不會提供Django靜態文件
- 14. IIS8拒絕提供靜態.wsdl文件
- 15. Django 1.7 - 提供靜態文件
- 16. 無法提供靜態文件,Django 1.6
- 17. 提供靜態文件與澤西2
- 18. Nginx沒有提供Django靜態文件
- 19. Django和提供靜態文件
- 20. Django未提供靜態文件
- 21. 用Ruby Espresso提供靜態文件
- 22. NGINX爲Django App提供靜態文件
- 23. Django和Apache不提供靜態文件
- 24. asp.net,如何提供靜態文件
- 25. 蟒蛇燒瓶 - 提供靜態文件
- 26. 用Django提供靜態文件
- 27. golang錯誤提供靜態文件
- 28. django用nginx提供靜態文件
- 29. django:通過nginx提供靜態文件
- 30. 在使用Joomla時從靜態文件的文件夾中提供404頁面
什麼是您的應用程序服務器?一些提供了一個乾淨的方式來定義一個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