欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java根据url下载附件源码

程序员文章站 2022-07-16 14:12:32
...
public static void runDownLoad(String title,String fileurl,String filename){ 
         // 构造URL 
        URL url; 
        try { 
            url = new URL(fileurl); 
             // 打开URL连接 
            URLConnection con = (URLConnection)url.openConnection(); 
             // 得到URL的输入流 
            InputStream input = con.getInputStream(); 
            // 设置数据缓冲 
            byte[] bs = new byte[1024 * 2]; 
            // 读取到的数据长度 
            int len; 
            // 输出的文件流保存图片至本地
            String path1 = "c:/aa/"+title;
            File f = new File(path1);
            f.mkdirs();
           // String hzm=fileurl.split("\\.")[fileurl.split("\\.").length-1];
            OutputStream os = new FileOutputStream(path1+"\\"+filename);
            while ((len = input.read(bs)) != -1) { 
            os.write(bs, 0, len); 
            } 
            os.close(); 
            input.close(); 
        } catch (MalformedURLException e) { 
            // TODO 自动生成的 catch 块 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // TODO 自动生成的 catch 块 
            e.printStackTrace(); 
        } 
         
    }
相关标签: java URLConnection