1.编写SQL,拼接处需要的图片的url
MySQL中使用concat函数来拼接
2.编写Java代码
2.1先写一个downloadPicture函数
private void downloadPicture(String urlList) {
URL url = null;
int imageNumber = 0;
try {
url = new URL(urlList);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
String imageName = "图片存储位置"+urlList.substring(urlList.lastIndexOf("/"));
FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
byte[] context=output.toByteArray();
fileOutputStream.write(output.toByteArray());
dataInputStream.close();
fileOutputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
2.2引进junit包,写一个test函数,执行downloadPicture并下载图片
@Test
public void test(){
ArrayList<String> list = new ArrayList<>();
list.add("由SQL得到的url");
for(String str:list){
downloadPicture(str);
}
}