Java 从服务器获取图像并保存到MySQL数据库
我从服务器获取一个图像作为InputStream,然后将其保存到mySQL数据库。当我使用Java 从服务器获取图像并保存到MySQL数据库,java,mysql,url,ftp,inputstream,Java,Mysql,Url,Ftp,Inputstream,我从服务器获取一个图像作为InputStream,然后将其保存到mySQL数据库。当我使用Thread.sleep(5000)时,它可以工作。但是如果我不使用它,则不会将任何图片保存到数据库中,或者只保存一张图片和一半或更少。所以我知道程序需要时间将图像写入数据库,但需要多少时间?这就是问题所在,我想确切地知道它何时将图像写入数据库,并可以从下一个图像开始。下面是我的代码: ResultSet rs = stmt.executeQuery(query); whil
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
我希望将等待(睡眠)放在InputStreamInputStreamTelnet=connUrl.getInputStream()之后但是当我把睡眠放在这行后面时,它就不起作用了。它只有在睡觉之前才起作用。有人能解释一下为什么我不想使用Thread.sleep(5000)
相反,您希望等待准确的时间或根本不等待,这将使程序更快。此外,可能存在保存图片可能需要5秒钟以上的情况,或者可能保存图片不需要时间,但需要打开url连接。当我取消对其中一行的注释时,代码上有两行睡眠行,程序运行正常(成功地将图像保存到mysql数据库)。我还在服务器上验证了这些图像是否存在,但最终我在mysql数据库中看不到它们
更新:我删除了try块和telnet的东西,现在不用等待就可以工作了,但我真的需要telnet的东西
更新2:在检查我的telnet类后,发现我忘了将我所做的更改应用于单行。。。现在它不用等待就能工作了 嗯,我在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();
}
}
公共类ImageLoader{
私有静态最终整数开始\图像\ ID=1;
私有静态最终int END_IMAGE_ID=1000;
私有静态最终字符串图像\u URL=”http://savepic.net/%d.jpg";
公共静态void main(字符串[]args)引发SQLException、IOException{
Connection Connection=DriverManager.getConnection(“jdbc:postgresql://localhost:5432/test“,”用户名“,”密码“);
PreparedStatement imageStatement=connection.prepareStatement(“插入到public.image值(?)”;
对于(int i=START\u IMAGE\u ID;i Hm,可能是您应该将整个图像读入字节数组,然后再读入该语句。setBlob(1,imageArray)?@IvanBabanin我试过了,但没用,您能给出代码示例吗?它还希望我使用语句。setBytes(1,imageArray);
是的,对不起。当然是挫折。我在下面添加了答案和示例代码。你用“插入到”来做,但我用“更新”来做,但它对我仍然不起作用,这可能是原因吗?我必须用“更新”来做,因为我需要“在哪里”子句。只是为了测试,我将语句改为INSERT INTO,但它也不起作用,当我睡眠5秒时,测试再次起作用。可能是因为我使用的是MYSQL和Java 1.8吗?你使用的是哪个版本的MYSQL和JDBC驱动程序?我想这不是因为MYSQL版本,而是因为MYSQL:'5.5.37-0ubuntu0.12.04.1'和JDBC:MYSQL-connector-Java-5。1.7Hm,我在MySQL上测试了我的代码:'5.5.40-o+wheezy1'/JDBC:'MySQL-connector-java-5.1.34',效果很好。