gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Java 如何将网络资源url转化为File文件由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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对象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
  * 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 如何将网络资源url转化为File文件的文章就讲到这里了,如果你想了解更多关于Java 如何将网络资源url转化为File文件的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com