| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「画像出力Servlet」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
 
1行目: 1行目:
==画像出力Servlet==
+
==[[画像出力Servlet]]==
[[Java]] | {{category コード片}}
+
[[Java]] | [[Category:コード片]]
  
 
=====呼出元=====
 
=====呼出元=====
7行目: 7行目:
 
   <servlet>
 
   <servlet>
 
     <servlet-name>imageresoruce</servlet-name>
 
     <servlet-name>imageresoruce</servlet-name>
     <servlet-class>rc.ImageResourceServlet</servlet-class>
+
     <servlet-class>rc.Image[[R]]esourceServlet</servlet-class>
 
     <load-on-startup>1</load-on-startup>
 
     <load-on-startup>1</load-on-startup>
 
   </servlet>
 
   </servlet>
26行目: 26行目:
 
  import javax.servlet.ServletException;
 
  import javax.servlet.ServletException;
 
  import javax.servlet.http.HttpServlet;
 
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
+
  import javax.servlet.http.HttpServlet[[R]]equest;
  import javax.servlet.http.HttpServletResponse;
+
  import javax.servlet.http.HttpServlet[[R]]esponse;
 
   
 
   
 
  @SuppressWarnings("serial")
 
  @SuppressWarnings("serial")
  public class ImageResourceServlet extends HttpServlet {
+
  public class Image[[R]]esourceServlet extends HttpServlet {
 
   private String basedir = "c:\\work\\img\\";
 
   private String basedir = "c:\\work\\img\\";
 
    
 
    
 
   @Override
 
   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
   protected void doGet(HttpServlet[[R]]equest request, HttpServlet[[R]]esponse response) throws ServletException, IOException {
 
      
 
      
 
     byte[] buf = new byte[1024];
 
     byte[] buf = new byte[1024];
42行目: 42行目:
 
   
 
   
 
     // 動的ファイルのファイル名を設定
 
     // 動的ファイルのファイル名を設定
     response.setHeader("Cache-Control", "public"); // IE対策
+
     response.set[[Header]]("Cache-Control", "public"); // IE対策
 
     response.setContentType("image/jpeg");
 
     response.setContentType("image/jpeg");
     response.setHeader("Content-Disposition", "inline; filename=\"" + f.getName() + "\"");
+
     response.set[[Header]]("Content-Disposition", "inline; filename=\"" + f.getName() + "\"");
 
   
 
   
 
     FileInputStream r = new FileInputStream(f);
 
     FileInputStream r = new FileInputStream(f);

2020年2月16日 (日) 04:20時点における最新版

画像出力Servlet

Java |

呼出元
<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();
  }
}