こんにちは、私はここにあるこの非常に役立つtutに従ってみました:
http://balusc.blogspot.co.uk/2007/04/imageservlet.html
チュートリアルに従って、データベースから画像を取得し、jsp ページでユーザーに表示する画像サーブレットを作成しようとしています。私の知る限り、コードは問題ないように見えますが、コンソールに 500 内部エラーが表示され、サーバー コンソールに Image 変数が null であることが示されています。
Image image = dataManager.getPhotos(homeId);
以下は私の完全なコードです:
@WebServlet(name = "ImageServlet", urlPatterns = {"/Image"})
public class ImageServlet extends HttpServlet {
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
private static DatabaseConnector dataManager;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Get ID from request.
String homeId = request.getParameter("id");
// Check if ID is supplied to the request.
if (homeId == null) {
// Do your thing if the ID is not supplied to the request.
// Throw an exception, or send 404, or show default/warning image, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
try {
// Lookup Image by ImageId in database.
// Do your "SELECT * FROM Image WHERE ImageID" thing.
Image image = dataManager.getPhotos(homeId);
// Check if image is actually retrieved from database.
if (image == null) {
// Do your thing if the image does not exist in database.
// Throw an exception, or send 404, or show default/warning image, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Init servlet response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("image/png");
response.setHeader("Content-Length", String.valueOf(image.getLength()));
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getHomeID() + "\"");
// Prepare streams.
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams.
input = new BufferedInputStream(image.getContent(), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
// Gently close streams.
close(output);
close(input);
}
} catch (IllegalArgumentException ex) {
Logger.getLogger(ImageServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(ImageServlet.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ImageServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
// Do your thing with the exception. Print it, log it or mail it.
e.printStackTrace();
}
}
}
以下の My Database Connector は、以下のデータを処理および取得します。
public Image getPhotos(String homeID) throws
IllegalArgumentException, SQLException, ClassNotFoundException {
createConnection();
Image img = new Image();
ResultSet rs = null;
PreparedStatement preparedStatement = null;
String strQuery = "SELECT * "
+ "FROM home_photo "
+ "WHERE home_photo.home_id = ?";
try {
preparedStatement = conn.prepareStatement(strQuery);
preparedStatement.setString(1, homeID);
rs = preparedStatement.executeQuery();
while (rs.next()) {
img.setHomeID(rs.getInt("home_id"));
img.setPhotoID(rs.getInt("photo_id"));
img.setContent(rs.getBinaryStream("photo"));
img.setDescription(rs.getString("description"));
img.setLength(rs.getInt("length"));
img.setContentType(rs.getString("type"));
}
} catch (SQLException e) {
throw new SQLException(e);
}
closeConnection();
return img;
}
imgとnullを返す場所がわかりませんか?
助けていただければ幸いです。