「画像出力Servlet」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==画像出力Servlet== [Java]{{category コード片}} =====呼出元===== <img src="/rscsvr/img.irc?img=01.jpg"> =====Web.xml===== <servlet> <servlet-nam…」) |
|||
1行目: | 1行目: | ||
==画像出力Servlet== | ==画像出力Servlet== | ||
− | [Java]{{category コード片}} | + | [[Java]]{{category コード片}} |
=====呼出元===== | =====呼出元===== | ||
− | + | <img src="/rscsvr/img.irc?img=01.jpg"> | |
=====Web.xml===== | =====Web.xml===== | ||
− | + | <servlet> | |
− | + | <servlet-name>imageresoruce</servlet-name> | |
− | + | <servlet-class>rc.ImageResourceServlet</servlet-class> | |
− | + | <load-on-startup>1</load-on-startup> | |
− | + | </servlet> | |
− | + | <!-- Standard Action Servlet Mapping --> | |
− | + | <servlet-mapping> | |
− | + | <servlet-name>imageresoruce</servlet-name> | |
− | + | <url-pattern>*.irc</url-pattern> | |
− | + | </servlet-mapping> | |
=====Servlet===== | =====Servlet===== | ||
package rc; | package rc; | ||
49行目: | 49行目: | ||
OutputStream o = response.getOutputStream(); | OutputStream o = response.getOutputStream(); | ||
− | while (r.read(buf) | + | while (r.read(buf) > 0) { |
o.write(buf); | o.write(buf); | ||
} | } |
2020年2月15日 (土) 07:59時点における版
画像出力Servlet
呼出元
<img src="/rscsvr/img.irc?img=01.jpg">
Web.xml
<servlet> <servlet-name>imageresoruce</servlet-name> <servlet-class>rc.ImageResourceServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Standard Action Servlet Mapping --> <servlet-mapping> <servlet-name>imageresoruce</servlet-name> <url-pattern>*.irc</url-pattern> </servlet-mapping>
Servlet
package rc; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @SuppressWarnings("serial") public class ImageResourceServlet extends HttpServlet { private String basedir = "c:\\work\\img\\"; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { byte[] buf = new byte[1024]; String img = request.getParameter("img"); File f = new File(basedir + img); // 動的ファイルのファイル名を設定 response.setHeader("Cache-Control", "public"); // IE対策 response.setContentType("image/jpeg"); response.setHeader("Content-Disposition", "inline; filename=\"" + f.getName() + "\""); FileInputStream r = new FileInputStream(f); OutputStream o = response.getOutputStream(); while (r.read(buf) > 0) { o.write(buf); } o.close(); r.close(); } }
© 2006 矢木浩人