URL下载网络资源到本地

URL统一资源定位符

协议://ip地址:端口/项目名/资源

下载网络资源到本地

package com.li.changGe.urlDownload;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class URLFileDownload {
  public static void main(String[] args) {
    new URLFileDownload().download();
  }

  /**
   * URL对象获取Connection对象,
   * Connection对象加文件输出流下载文件
   */
  public void download(){
    
    InputStream inputStream = null;
    FileOutputStream fileOutputStream = null;
    
    try {
      URL url = new URL("https://tse2-mm.cn.bing.net/th/id/OIP-C.DXKj0u8XttHNJorm0vUooAHaEK?pid=ImgDet&rs=1");
      
      //打开链接
      URLConnection urlConnection = url.openConnection();

      //获取流
      inputStream = urlConnection.getInputStream();

      fileOutputStream = new FileOutputStream("迎风飘扬的旗.jpeg");

      byte[] bytes = new byte[1024];
      int length;
      while ((length = inputStream.read(bytes)) != -1){
        fileOutputStream.write(bytes,0,length);
      }

      System.out.println("文件输出完毕");
//------------------------------------------------------------
    }catch (Exception e){
      System.out.println("文件找不到");
      e.printStackTrace();

    }finally {

      if(fileOutputStream != null){

        try {
          fileOutputStream.close();
        }catch (Exception e){
          e.printStackTrace();
          System.out.println("文件输出流关闭错误");
        }

      }

      if(inputStream != null){

        try {
          inputStream.close();
        }catch (Exception e){
          e.printStackTrace();
          System.out.println("文件输入流关闭错误");
        }

      }

    }//finally


  }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在 Android Webview 中将 H5 资源本地化,可以使用以下步骤: 1. 在 Android 项目的 assets 目录下创建一个文件夹(比如说叫做 html),将 H5 资源放到该文件夹下。 2. 在 Webview 中加载 H5 页面时,将其 URL 改为本地路径,例如: ```java webView.loadUrl("file:///android_asset/html/index.html"); ``` 3. 在 Webview 的 setWebViewClient 方法中重写 shouldInterceptRequest 方法,拦截 H5 页面资源请求,并返回本地资源的 InputStream,例如: ```java webView.setWebViewClient(new WebViewClient() { @Override public WebResourceResponse shouldInterceptRequest(WebView view, String url) { if (url.contains("html/")) { // 判断是否为 H5 资源请求 try { AssetManager assetManager = getAssets(); InputStream inputStream = assetManager.open(url.replace("file:///android_asset/", "")); String mimeType = getMimeType(url); // 获取资源的 MIME 类型 return new WebResourceResponse(mimeType, "UTF-8", inputStream); } catch (IOException e) { e.printStackTrace(); } } return super.shouldInterceptRequest(view, url); } }); ``` 4. 在 getMimeType 方法中根据文件后缀名获取 MIME 类型,例如: ```java private String getMimeType(String url) { String extension = MimeTypeMap.getFileExtensionFromUrl(url); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); return mimeType != null ? mimeType : "application/octet-stream"; } ``` 这样就可以在 Android Webview 中将 H5 资源本地化了。需要注意的是,在加载 H5 页面时,如果 H5 页面中包含外部链接,例如 CDN 链接,这些链接将不会被拦截,仍然会从网络中加载。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helloses

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值