簡體   English   中英

從服務器獲取圖像並保存到MySQL DB

[英]Getting image from server and saving to MySQL DB

我從服務器獲取圖像作為InputStream,然后將其保存到mySQL數據庫。 當我使用Thread.sleep(5000);時,它可以工作Thread.sleep(5000); 但是,如果我不使用它,則不會將任何圖片保存到數據庫,或者僅保存一張圖片,一半或更少。 所以我知道程序需要時間將圖像寫入數據庫,但是需要多少時間? 這是一個問題,我想確切地知道它何時將圖像寫入數據庫,並且可以從下一個圖像開始。 下面是我的代碼:

        ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {

            int ID = rs.getInt(1);
            String myName = rs.getString(2);

            try {

                String myCommand = "take picture and save /mydir/mydir2/mydir3" + myName + ".png";
                telnet.sendCommand(myCommand); // Here taking a picture via telnet
                // Thread.sleep(5000);// If I uncomment this line it works

                String sqlCommand = "UPDATE my_table SET Picture = ? WHERE ID ='" + ID +"';";
                PreparedStatement statement = conn.prepareStatement(sqlCommand);

                String ftpUrl = "ftp://"+server_IP+"/mydir/mydir2/mydir3" + myName + ".png;type=i";

                URL url = new URL(ftpUrl);
                URLConnection connUrl = url.openConnection();

                //Thread.sleep(5000); // If I uncomment this line, it works too.

                InputStream inputStreamTelnet = connUrl.getInputStream();

                statement.setBlob(1, inputStreamTelnet);
                int row = statement.executeUpdate();
                if (row > 0) {
                    System.out.println("A picture was inserted into DB.");
                    System.out.println("Value of row(s) : " + row);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        } // End of while

我希望將Waiting(sleep)放在InputStream inputStreamTelnet = connUrl.getInputStream(); 但是當我在這條線之后進入睡眠狀態時,它不起作用。 它僅在睡眠之前起作用。 有人可以向我解釋為什么,我想避免使用Thread.sleep(5000); 而是想等待確切的時間或根本不等待,這會使程序更快,並且在某些情況下,保存圖片可能會花費5秒鍾以上,或者保存圖片不會花費時間,但是會打開url連接。 當我取消注釋其中一條程序時,代碼上有2條睡眠行(程序將圖像成功保存到mysql DB)。 我還在服務器上驗證了圖像的存在,但最后我沒有在mysql DB中看到它們。

更新:我刪除了try塊和telnet東西,現在它可以工作而無需等待,但是我真的需要telnet東西...

更新2:檢查我的telnet類后,發現我忘了對單行進行更改了...現在可以正常使用了!

h,我在JDK 1.7.0_67 / PostgreSQL 9.2上測試了我的代碼,它運行良好:

public class ImageLoader {
    private static final int START_IMAGE_ID = 1;
    private static final int END_IMAGE_ID = 1000;

    private static final String IMAGE_URL = "http://savepic.net/%d.jpg";

    public static void main(String[] args) throws SQLException, IOException {
        Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "username", "password");
        PreparedStatement imageStatement = connection.prepareStatement("INSERT INTO public.image VALUES(?, ?)");

        for (int i = START_IMAGE_ID; i <= END_IMAGE_ID; i++) {
            String imageUrl = String.format(IMAGE_URL, i);

            URL url = new URL(imageUrl);
            URLConnection urlConnection = url.openConnection();

            imageStatement.setLong(1, i);
            imageStatement.setBytes(2, read(urlConnection.getInputStream()));

            int count = imageStatement.executeUpdate();
            if (count != 1) {
                throw new IllegalStateException("Image with ID = " + i + " not inserted");
            } else {
                System.out.println("Image (" + imageUrl + ") saved to database");
            }
        }

        imageStatement.close();
        connection.close();
    }

    private static byte[] read(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1 << 15); // assume image average size ~ 32 Kbytes
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

        byte[] buffer = new byte[1 << 10];

        int read = -1;
        while ((read = bufferedInputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, read);
        }

        return byteArrayOutputStream.toByteArray();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM