如何使用jsp / Servlet插入和检索图像数据库?

时间:2016-08-30 13:37:50

标签: java mysql jsp servlets

我有一个Form Page index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>
fieldset
{
      width: 70px;
}
</style>
</head>
<body>
<form action="Upload" method="post" enctype="multipart/form-data">
<fieldset>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Select Photo</td>
<td><input type="file" name="photo"></td>
</tr>
<td><input type="submit" value="Upload"></td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>

MyServlet Page Upload.java:

import java.sql.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.DriverManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet(name = "Upload", urlPatterns = {"/Upload"})
@MultipartConfig(maxFileSize = 169999999)   // upload file's size up to 16MB
public class Upload extends HttpServlet 
{ 
private static final long serialVersionUID = 1L;
PrintWriter out;
InputStream inputStream = null; 
int allField = 0;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse      response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try 
{
out = response.getWriter();
String name=request.getParameter("name");
Part filePart = request.getPart("photo");
if (filePart != null) 
{
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
inputStream = filePart.getInputStream();
}
Class.forName("com.mysql.jdbc.Driver");  
Connection    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","root") 
PreparedStatement ps = con.prepareStatement("insert into    PhotoDetails(Name,Images)values(?,?)");
ps.setString(1,name);
ps.setBlob(2,inputStream);
ps.executeUpdate();
out.println("Image Inserted");
}
catch(Exception e)
{
out.println(e);
}   
}
}

我正在使用mysql数据库,这是我的表:

create table PhotoDetails
(
Name varchar(100),
Images blob
) 

填写完所有表格后,当我点击“更新”按钮时,我收到此错误:

HTTP状态500 - Servlet执行引发异常

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:-1)

这是在jsp文件中显示图像的最佳方式。

创建showLogo.jsp 并包括你想要的地方。

<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="java.sql.*,java.io.*,org.apache.struts2.ServletActionContext"%>
<%@ page language="java" import="java.util.*, com.abc.util.dbutil.*,javax.servlet.http.HttpServletRequest" %>
<%

try{
    InputStream sImage;
    ResultSet resultSet = null;
    PreparedStatement pstmt = null;

    DBConnection dbConnection= null;
    dbConnection= new DBConnection();
    Connection con = null;
    con= dbConnection.getConnection();

    ServletInputStream sInIm = null;

      Statement st=con.createStatement();
      String company_id = request.getParameter("company_id");    

      resultSet=st.executeQuery("select logo from company where company_id='" + company_id + "'");

      if(resultSet.next()){
      byte[] bytearray = new byte[1048576];
      int size=0;
      sImage = resultSet.getBinaryStream(1);    
      response.reset();
      response.setContentType("image/jpeg");
      while((size=sImage.read(bytearray))!= -1){
        response.getOutputStream().write(bytearray,0,size);
      }
      response.getOutputStream().flush();
      response.getOutputStream().close();
}
 con.close();
}     
catch(Exception ex){

}
%>

我是这样的。

/company/showLogo.jsp?company_id =&#34; ID =&#34; blah_s&#34;类=&#34;标识小&#34;&GT;

从文件获取图像到FileInputStream

FileInputStream fs = null;
        try {
            fs = new FileInputStream(getUserImage());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


.................


ps.setBinaryStream(8, fs, fs.available());

插入表(徽标)值(?)

这是针对具有blob列的mysql。