0

<input type="file" name="file" />このサーブレット経由でHTML フォームを使用して 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)
4

1 に答える 1

0

実際の画像データをダウンロードするには、追加のサーブレットが必要になります。BalusC のFileServletを確認してください。

于 2012-09-07T08:35:21.197 に答える