工具类—Java下载远程文件到本地

淩亂°似流年 2022-05-31 01:18 879阅读 0赞
  1. package com.alipay.util;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. /**
  10. * @Name: RemoteFile.java
  11. * @Description: Java下载远程文件到本地。
  12. * @Author: PeiFeng
  13. * @Create Date: 2017-8-12
  14. */
  15. public class RemoteFile {
  16. public static void main(String[] args) throws Exception {
  17. downRemoteFile(
  18. "https://image.baidu.com/search/down?tn=download&ipn=dwnl&word=download&ie=utf8&fr=result&url=http%3A%2F%2Fh.hiphotos.baidu.com%2Fzhidao%2Fpic%2Fitem%2F060828381f30e9240ff2cd434c086e061d95f76a.jpg&thumburl=https%3A%2F%2Fss2.bdstatic.com%2F70cFvnSh_Q1YnxGkpoWK1HF6hhy%2Fit%2Fu%3D2504031429%2C1727248259%26fm%3D26%26gp%3D0.jpg",
  19. "sssss.jpg", "D:/sss");
  20. }
  21. /**
  22. * @Name: downRemoteFile。
  23. * @Description: 下载远程文件。
  24. * @Parameters: remoteFileUrl,要下载的远程文件地址。
  25. * @Parameters: saveFileName,下载后保存的文件名。
  26. * @Parameters: saveDir,下载后保存的文件路径。
  27. * @Return: String,文件保存的地址。
  28. * @Author: PeiFeng
  29. * @Version: V1.00
  30. * @Create Date: 2017-8-12
  31. */
  32. public static String downRemoteFile(String remoteFileUrl, String saveFileName, String saveDir) {
  33. HttpURLConnection conn = null;
  34. OutputStream oputstream = null;
  35. InputStream iputstream = null;
  36. try {
  37. // 创建保存文件的目录
  38. File savePath = new File(saveDir);
  39. if (!savePath.exists()) {
  40. savePath.mkdir();
  41. }
  42. // 创建保存的文件
  43. File file = new File(savePath + "/" + saveFileName);
  44. if (file != null && !file.exists()) {
  45. file.createNewFile();
  46. }
  47. URL url = new URL(remoteFileUrl);
  48. // 将url以open方法返回的urlConnection连接强转为HttpURLConnection连接(标识一个url所引用的远程对象连接)
  49. // 此时cnnection只是为一个连接对象,待连接中
  50. conn = (HttpURLConnection) url.openConnection();
  51. // 设置是否要从 URL连接读取数据,默认为true
  52. conn.setDoInput(true);
  53. // 建立连接
  54. // (请求未开始,直到connection.getInputStream()方法调用时才发起,以上各个参数设置需在此方法之前进行)
  55. conn.connect();
  56. // 连接发起请求,处理服务器响应 (从连接获取到输入流)
  57. iputstream = conn.getInputStream();
  58. // 创建文件输出流,用于保存下载的远程文件
  59. oputstream = new FileOutputStream(file);
  60. // 用来存储响应数据
  61. byte[] buffer = new byte[4 * 1024];
  62. int byteRead = -1;
  63. // 循环读取流
  64. while ((byteRead = (iputstream.read(buffer))) != -1) {
  65. oputstream.write(buffer, 0, byteRead);
  66. }
  67. // 输出完成后刷新并关闭流
  68. oputstream.flush();
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. } finally {
  72. try {
  73. // 重要且易忽略步骤 (关闭流,切记!)
  74. if (iputstream != null) {
  75. iputstream.close();
  76. }
  77. if (oputstream != null) {
  78. oputstream.close();
  79. }
  80. // 销毁连接
  81. if (conn != null) {
  82. conn.disconnect();
  83. }
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. // 返回保存后的文件路径
  89. return saveDir + "/" + saveFileName;
  90. }
  91. }

转载 http://blog.csdn.net/u012758088/article/details/77119530?locationNum=4&fps=1

发表评论

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

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

相关阅读