如何用java复制图片

如何用java复制文件中的图片

java 复制代码
    public static void main(String[] args)
    {
        String src="这里放你要复制的图片的地址,记得在后面加.jpg";//否则可能会报拒绝访问或者无权限
        String target="这里放你要复制到的文件的地址,同样,也要加.jpg";
        copyFile(src,target);
    }
    public static void copyFile(String src,String target)
    {
    //首先需要确定两个源
        File srcFile = new File(src);
        File targetFile = new File(target);
        try {
        //选择两个流
            InputStream in = new FileInputStream(srcFile);
            OutputStream out = new FileOutputStream(targetFile);
            //操作流
            byte[] bytes = new byte[1024];
            int len = -1;
            while((len=in.read(bytes))!=-1)
            {
                out.write(bytes, 0, len);
            }
            //关闭流
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文件复制成功");
    }

如何用java从网站链接中复制图片到文件中

java 复制代码
    public static void main(String[] args) {

        // 声明stream输入输出类
        OutputStream os = null;
        InputStream is = null;

        try {
            // 生成URL类并建立连接
            URL url = new URL("链接地址");
            URLConnection conn = url.openConnection();
            is = conn.getInputStream();

            // 准备输出文件,开始读写
            File f = new File("io.jpg");
            os = new FileOutputStream(f);
            byte[] b = new byte[1024];
            int len = 0;
            while((len = is.read(b))!= -1) {
                os.write(b, 0 ,len);
            }
            os.close();
            is.close();
            //最后记得关闭
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
相关推荐
源代码•宸14 小时前
分布式缓存-GO(简历写法、常见面试题)
服务器·开发语言·经验分享·分布式·后端·缓存·golang
sszdlbw14 小时前
后端springboot框架入门学习--第二篇
java·spring boot·学习
阿拉斯攀登14 小时前
MyBatis 全面解析 & Spring Boot 集成实战
java·spring boot·mybatis·持久层框架
A尘埃14 小时前
Java业务场景(高并发+高可用+分布式)
java·开发语言·分布式
晨曦夜月14 小时前
头文件与目标文件的关系
linux·开发语言·c++
白仑色14 小时前
java中的anyMatch和allMatch方法
java·linux·windows·anymatch·allmatch
刃神太酷啦14 小时前
C++ list 容器全解析:从构造到模拟实现的深度探索----《Hello C++ Wrold!》(16)--(C/C++)
java·c语言·c++·qt·算法·leetcode·list
wearegogog12314 小时前
C# 条码打印程序(一维码 + 二维码)
java·开发语言·c#
码农阿豪14 小时前
用 PlaylistDL 攒私人音乐库?加个 cpolar,出门在外也能随时听!
java