当前位置:   article > 正文

通过路径下载文件和通过url下载文件

通过url下载文件

目录

1:通过url请求下载文件

2:从服务器的路径拿到文件


1:通过url请求下载文件

  1. /**
  2.  * URL
  3.  * 类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。
  4.  * 抓取文件:以下载一张图片为例
  5.  * 1、创建URL对象  指定文件的URL地址(图片的URL地址)
  6.  * 2、获取远程连接的对象
  7.  * 3、设置获取方式,一般用到GET、POST方式
  8.  * 4、获取HTTP响应的状态码  200表示请求成功
  9.  * 5、字节输入流  读取文件
  10.  * 6、字节输出流  把读取的文件存入本地
  11.  * @author hw
  12.  *
  13.  */
  14. ---------------------
  15. public class TestDowload {
  16. public static void main(String[] args) {
  17. BufferedInputStream bufferedInputStream = null;
  18. BufferedOutputStream bufferedOutputStream = null;
  19. try {
  20. URL url = new URL("https://e-cloudstore.com/files/ecology_dev.zip");
  21. HttpURLConnection conn=(HttpURLConnection)url.openConnection();
  22. conn.setRequestMethod("GET");
  23. //设置URL请求方式 GET方式
  24. conn.setRequestMethod("GET");
  25. //从HTTP响应消息获取状态码
  26. int code=conn.getResponseCode();
  27. //如果状态码为200表示请求成功
  28. if(code ==200){
  29. bufferedInputStream = new BufferedInputStream(conn.getInputStream());
  30. bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D://ecology_dev.zip"));
  31. byte[] by=new byte[1024];
  32. int len=0;
  33. while((len=bufferedInputStream.read(by))!=-1) {
  34. bufferedOutputStream.write(by, 0, len);
  35. //刷新
  36. bufferedOutputStream.flush();
  37. }
  38. }
  39. } catch (FileNotFoundException e) {
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. } finally {
  44. try {
  45. bufferedInputStream.close();
  46. bufferedOutputStream.close();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }

2:从服务器的路径拿到文件

  1. public class TestDowload {
  2. public static void main(String[] args) {
  3. FileInputStream fileInputStream = null;
  4. BufferedInputStream bufferedInputStream = null;
  5. FileOutputStream fileOutputStream = null;
  6. BufferedOutputStream bufferedOutputStream = null;
  7. try {
  8. fileInputStream = new FileInputStream(new File("d://xxx.txt"));
  9. bufferedInputStream = new BufferedInputStream(fileInputStream);
  10. fileOutputStream = new FileOutputStream(new File("D:/开发文档/txt"));
  11. bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
  12. int tag;
  13. while ((tag = (bufferedInputStream.read())) != -1) {
  14. bufferedOutputStream.write(tag);
  15. }
  16. bufferedOutputStream.flush();
  17. } catch (FileNotFoundException e) {
  18. e.printStackTrace();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. } finally {
  22. if (fileInputStream!=null){
  23. try {
  24. fileInputStream.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. if (fileOutputStream!=null){
  30. try {
  31. fileOutputStream.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. if (bufferedInputStream!=null){
  37. try {
  38. bufferedInputStream.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. if (bufferedOutputStream!=null){
  44. try {
  45. bufferedOutputStream.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51. }
  52. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55408
推荐阅读
相关标签
  

闽ICP备14008679号