Java 如何将网络资源url转化为File文件

作者:狂风之力 时间:2023-05-28 09:23:37 

将网络资源url转化为File文件

将互联网上的http开头的url资源,保存到本地。


private File getNetUrlHttp(String path){
      //对本地文件命名,path是http的完整路径,主要得到资源的名字
       String newUrl = path;
       newUrl = newUrl.split("[?]")[0];
       String[] bb = newUrl.split("/");
       //得到最后一个分隔符后的名字
       String fileName = bb[bb.length - 1];
       //保存到本地的路径
       String filePath="e:\\audio\\"+fileName;
       File file = null;

URL urlfile;
       InputStream inputStream = null;
       OutputStream outputStream = null;
       try{
           //判断文件的父级目录是否存在,不存在则创建
           file = new File(filePath);
           if(!file.getParentFile().exists()){
               file.getParentFile().mkdirs();
           }
           try{
               //创建文件
               file.createNewFile();
           }catch (Exception e){
               e.printStackTrace();
           }
           //下载
           urlfile = new URL(newUrl);
           inputStream = urlfile.openStream();
           outputStream = new FileOutputStream(file);

int bytesRead = 0;
           byte[] buffer = new byte[8192];
           while ((bytesRead=inputStream.read(buffer,0,8192))!=-1) {
               outputStream.write(buffer, 0, bytesRead);
           }
           }catch (Exception e) {
               e.printStackTrace();
           }finally {
               try {
                   if (null != outputStream) {
                       outputStream.close();
                   }
                   if (null != inputStream) {
                       inputStream.close();
                   }

} catch (Exception e) {
                   e.printStackTrace();
               }
           }
         return file;
       }

url转变为 MultipartFile对象


/**
* url转变为 MultipartFile对象
* @param url
* @param fileName
* @return
* @throws Exception
*/
private static MultipartFile createFileItem(String url, String fileName) throws Exception{
   FileItem item = null;
   try {
       HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
       conn.setReadTimeout(30000);
       conn.setConnectTimeout(30000);
       //设置应用程序要从网络连接读取数据
       conn.setDoInput(true);
       conn.setRequestMethod("GET");
       if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
           InputStream is = conn.getInputStream();
           FileItemFactory factory = new DiskFileItemFactory(16, null);
           String textFieldName = "uploadfile";
           item = factory.createItem(textFieldName, ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName);
           OutputStream os = item.getOutputStream();
           int bytesRead = 0;
           byte[] buffer = new byte[8192];
           while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
               os.write(buffer, 0, bytesRead);
           }
           os.close();
           is.close();
       }
   } catch (IOException e) {
       throw new RuntimeException("文件下载失败", e);
   }
   return new CommonsMultipartFile(item);
}

来源:https://blog.csdn.net/x_christ1/article/details/112536203

标签:Java,网络资源,File,url
0
投稿

猜你喜欢

  • Java C++ 算法题解拓展leetcode670最大交换示例

    2022-05-18 23:31:51
  • C#异步编程详解

    2023-02-13 13:10:39
  • Android实现拍照及图片裁剪(6.0以上权限处理及7.0以上文件管理)

    2022-05-19 09:18:14
  • flutter窗口初始和绘制流程详析

    2023-08-17 21:07:30
  • 详解Java发送HTTP请求

    2022-01-09 14:53:57
  • Unity Shader实现水墨效果

    2021-08-29 11:05:01
  • Java微信支付之公众号支付、扫码支付实例

    2022-07-10 10:23:11
  • 浅析Java中Apache BeanUtils和Spring BeanUtils的用法

    2021-07-10 21:29:15
  • Java如何将字符串String转换为整型Int

    2023-11-10 21:40:19
  • Android实现支付宝支付密码输入界面

    2023-06-20 14:57:15
  • 读写Android中assets目录下的文件的方法详解

    2023-03-03 15:50:35
  • 详解kafka中的消息分区分配算法

    2021-06-02 08:16:15
  • java 垃圾回收机制以及经典垃圾回收器详解

    2022-07-06 05:16:08
  • 从内存地址解析Java的static关键字的作用

    2022-03-18 03:49:26
  • c# 图片加密解密的实例代码

    2023-08-20 21:21:01
  • Spring Boot常用注解(经典干货)

    2023-11-24 22:29:10
  • arthas jprofiler做复杂链路的调用分析

    2022-01-15 12:01:25
  • java操作mongodb之多表联查的实现($lookup)

    2023-08-08 10:24:07
  • 在IDEA里gradle配置和使用的方法步骤

    2023-11-23 16:07:58
  • Android仿QQ微信实时监测网络状态

    2022-10-10 06:48:52
  • asp之家 软件编程 m.aspxhome.com