如何用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 小时前
python之向量、向量和、向量点积
开发语言·python·numpy
代码AI弗森3 小时前
一文理清楚“算力申请 / 成本测算 / 并发评估”
java·服务器·数据库
Old Uncle Tom3 小时前
OpenClaw 记忆系统 -- 记忆预加载
java·数据结构·算法·agent
小小小米粒3 小时前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。
java·开发语言·windows
摇滚侠4 小时前
expdp 查看帮助
java·数据库·oracle
czhc11400756634 小时前
C# 428 线程、异步
开发语言·c#
:1214 小时前
java基础
java·开发语言
SilentSamsara5 小时前
Python 环境搭建完整指南:从下载安装到运行第一个程序
开发语言·python
曹牧5 小时前
Spring:@RequestMapping注解,匹配的顺序与上下文无关
java·后端·spring
daixin88485 小时前
cursor无法正常使用gpt5.5等模型解决方案
java·redis·cursor