通过html表单将图像插入数据库

时间:2012-09-06 21:13:39

标签: mysql jsp java-ee-6

我想通过此Servlet使用HTML表单<input type="file" name="file" />将图像插入MySQL,因此我使用此代码。

if (!isMultipart) {
    System.out.println("File Not Uploaded");
} else {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = null;

    try {
        items = upload.parseRequest(request);
        System.out.println("items: "+items);
    } catch (FileUploadException e) {
        e.printStackTrace();
    }

    Iterator itr = items.iterator();

    while (itr.hasNext()) {
        FileItem item = (FileItem) itr.next();

        if (item.isFormField()){
            String name = item.getFieldName();
            System.out.println("name: "+name);
            String value = item.getString();
            System.out.println("value: "+value);
        } else {
            try {
                String itemName = item.getName();
                Random generator = new Random();
                int r = Math.abs(generator.nextInt());

                String reg = "[.*]";
                String replacingtext = "";
                System.out.println("Text before replacing is:-" + itemName);
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(itemName);
                StringBuffer buffer = new StringBuffer();

                while (matcher.find()) {
                    matcher.appendReplacement(buffer, replacingtext);
                }

                int IndexOf = itemName.indexOf("."); 
                String domainName = itemName.substring(IndexOf);
                System.out.println("domainName: "+domainName);

                String finalimage = buffer.toString()+"_" + r + domainName;
                System.out.println("Final Image===" + finalimage);

                File savedFile = new File("/home/abdo/NetBeansProjects/Gestion_des_taches/web/"+"img/"+finalimage);
                item.write(savedFile);
                out.println("<html>");
                out.println("<body>");
                out.println("<table><tr><td>");
                out.println("<img src=images/" + finalimage + ">");
                out.println("</td></tr></table>");

                Connection conn = null;
                String url = "jdbc:mysql://localhost/";
                String dbName = "actors";
                String driver = "com.mysql.jdbc.Driver";
                String username = "root"; 
                String userPassword = "root";
                String strQuery = null;
                String strQuery1 = null;
                String imgLen="";

                try {
                    System.out.println("itemName::::: "+itemName);
                    Class.forName(driver).newInstance();
                    conn = DriverManager.getConnection(url+dbName,username,userPassword);
                    Statement st = conn.createStatement();
                    strQuery = "insert into pictures set image='" + finalimage + "'"; 
                    int rs = st.executeUpdate(strQuery);
                    System.out.println("Query Executed Successfully++++++++++++++");
                    out.println("image inserted successfully");
                    out.println("</body>");
                    out.println("</html>");                     
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } finally {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

当我使用

out.println("<img src=images/" + finalimage + ">");

它工作正常。我的问题是如何通过MySQL请求select image from pictures;显示图像?

以下是pictures表的输出:

mysql> select * from pictures;

+----+----------------------+ 
| id | image                |
+----+----------------------+
| 42 | Photo_1339972050.jpg |
| 43 | Photo_168423959.jpg  |
| 44 | Photo_859939969.jpg  |
| 45 | Photo_1696305644.jpg |
| 46 | Photo_910632756.jpg  |
| 47 | Photo_560808853.jpg  |
+----+----------------------+
6 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:0)

您需要一个额外的servlet来下载实际的图像数据。检查BalusC的FileServlet