如何用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();
        }
    }
相关推荐
曹牧2 分钟前
C#:函数参数指定默认值
开发语言·c#
AAA@峥40 分钟前
Python 基础开篇:认识 Python、版本选择、环境部署与首个 HelloWorld
开发语言·python
caimouse1 小时前
ReactOS 图形系统分析(47):GDI 批处理 — gdibatch.c
c语言·开发语言
qq21084629531 小时前
在python中什么是 self 和 cls?
开发语言·前端·python
杨充2 小时前
05.多用组合和少继承
开发语言·bash
带多刺的玫瑰2 小时前
Leecode#4刷题之寻找两个正序数组的中位数
java·前端·算法
傻啦嘿哟2 小时前
王者荣耀爬虫:爬取全英雄皮肤数据,用Python做可视化分析
开发语言·爬虫·python
阿无,2 小时前
布隆过滤器
java·算法·哈希算法
AI直播技术杂谈2 小时前
直播画面突然模糊了,排查了三个小时
开发语言·php