如何用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();
        }
    }
相关推荐
小李子呢02115 小时前
前端八股性能优化(1)---防抖和节流
开发语言·前端·javascript
henrylin99995 小时前
Hermes Agent 核心运行系统调用流程--源码分析
开发语言·人工智能·python·机器学习·hermesagent
珎珎啊5 小时前
Python3 字符串核心知识点
开发语言·python
会编程的土豆5 小时前
01背包与完全背包详解
开发语言·数据结构·c++·算法
lbb 小魔仙5 小时前
Python_多模态大模型实战指南
开发语言·python
XDHCOM5 小时前
Python os.system() 和 subprocess 怎么选?运行系统命令哪个更好用?
开发语言·网络·python
猫耳球团5 小时前
IDEA与Cursor跨平台协作指南
java·ide·intellij-idea
亚历克斯神5 小时前
Java 职业发展:2026 指南
java·spring·微服务
xuhaoyu_cpp_java6 小时前
Maven学习(二)
java·经验分享·笔记·学习·maven
西门吹-禅6 小时前
java 微服务学习笔记
java·学习·微服务