根据图片地址下载图片

Myth丶恋晨 2022-06-02 10:15 352阅读 0赞
  1. public static void saveFp(String url, String savePath,String filename){
  2. HttpGet httpGet = new HttpGet(url);
  3. HttpClient httpClient=new DefaultHttpClient();
  4. try {
  5. HttpResponse response=httpClient.execute(httpGet);
  6. HttpEntity entity = response.getEntity();
  7. if (entity != null) {
  8. byte[] byteArray = EntityUtils.toByteArray(entity);
  9. //创建目录
  10. File pathFile = new File(savePath);
  11. if(!pathFile.exists()){
  12. pathFile.mkdir();
  13. }
  14. //创建pdf文件
  15. File pdfFile = new File(savePath+filename+".pdf");
  16. if(!pdfFile.exists()){
  17. pdfFile.createNewFile();
  18. }
  19. // //如果该文件所在目录不存在,则新创建该目录
  20. // if(!file.getParentFile().exists()){
  21. // File file2=new File(savePath);
  22. // file2.mkdir();
  23. // System.out.println(file2.mkdir());
  24. // }
  25. // if(!file.exists()){
  26. // file.createNewFile();
  27. // //创建目录
  28. // //file.mkdir();
  29. // System.out.println(file.createNewFile());
  30. // } else {
  31. // //file.delete();
  32. // }
  33. FileOutputStream fos = new FileOutputStream(pdfFile);
  34. fos.write(byteArray);
  35. fos.flush();
  36. fos.close();
  37. }
  38. } catch (ClientProtocolException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. } catch (FileNotFoundException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. } catch (IOException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }

发表评论

表情:
评论列表 (有 0 条评论,352人围观)

还没有评论,来说两句吧...

相关阅读