JPEGファイルを読みこんで返すJSP
2009/01/15
java
jsp
えーと、Javaです。
JSPを使って任意のJPEGファイル読み込み適当に返します。
web.xmlです。
mappingで拡張子がjpg,jpegであるものに適用します。
<servlet>
<servlet-name>read_image</servlet-name>
<jsp-file>/read_image.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>read_image</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>read_image</servlet-name>
<url-pattern>*.jpeg</url-pattern>
</servlet-mapping>
<servlet-name>read_image</servlet-name>
<jsp-file>/read_image.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>read_image</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>read_image</servlet-name>
<url-pattern>*.jpeg</url-pattern>
</servlet-mapping>
JSPです。
結構イージーにつくってます。
<%@ page language="java"
contentType="image/jpeg"%><%
String root = "/";
String s = request.getRequestURI();
System.out.println("jpeg request:[" + request.getRequestURI() + "] imagepath:[" + s + "]");
java.io.BufferedInputStream input = new java.io.BufferedInputStream(
new java.io.FileInputStream(root + s));
java.io.BufferedOutputStream output = new java.io.BufferedOutputStream(response
.getOutputStream());
byte[] buf = new byte[8192];
int len = -1;
while ((len = input.read(buf)) != -1) {
output.write(buf, 0, len);
output.flush();
}
input.close();
output.close();
%>
contentType="image/jpeg"%><%
String root = "/";
String s = request.getRequestURI();
System.out.println("jpeg request:[" + request.getRequestURI() + "] imagepath:[" + s + "]");
java.io.BufferedInputStream input = new java.io.BufferedInputStream(
new java.io.FileInputStream(root + s));
java.io.BufferedOutputStream output = new java.io.BufferedOutputStream(response
.getOutputStream());
byte[] buf = new byte[8192];
int len = -1;
while ((len = input.read(buf)) != -1) {
output.write(buf, 0, len);
output.flush();
}
input.close();
output.close();
%>
: