根据图片地址下载图片
程序员文章站
2022-06-16 20:06:35
...
public static void saveFp(String url, String savePath,String filename){
HttpGet httpGet = new HttpGet(url);
HttpClient httpClient=new DefaultHttpClient();
try {
HttpResponse response=httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
byte[] byteArray = EntityUtils.toByteArray(entity);
//创建目录
File pathFile = new File(savePath);
if(!pathFile.exists()){
pathFile.mkdir();
}
//创建pdf文件
File pdfFile = new File(savePath+filename+".pdf");
if(!pdfFile.exists()){
pdfFile.createNewFile();
}
// //如果该文件所在目录不存在,则新创建该目录
// if(!file.getParentFile().exists()){
// File file2=new File(savePath);
// file2.mkdir();
// System.out.println(file2.mkdir());
// }
// if(!file.exists()){
// file.createNewFile();
// //创建目录
// //file.mkdir();
// System.out.println(file.createNewFile());
// } else {
// //file.delete();
// }
FileOutputStream fos = new FileOutputStream(pdfFile);
fos.write(byteArray);
fos.flush();
fos.close();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}