如何用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();
        }
    }
相关推荐
Sylvia33.10 分钟前
足球数据接口开发实战:如何用火星数据API盘活赛事应用
java·服务器·开发语言·数据库·python
小小放舟、11 分钟前
PaiCLI-Demo:从零实现 ReAct Agent + Tool Call
java·后端·intellij-idea·springboot·agent·react·tool call
z落落14 分钟前
C# WinForm 自定义控件
开发语言·c#
geovindu1 小时前
go: Enumeration Algorithm
开发语言·后端·算法·golang·枚举算法
C++、Java和Python的菜鸟2 小时前
第2章 前端Web基础(js、vue+Ajax)
java
harmful_sheep2 小时前
idea相关设置
java·ide·intellij-idea
奋发向前wcx2 小时前
y1,y2总复习笔记2 2026.7.15
java·笔记·算法
气概2 小时前
QT集成basler相机
开发语言·数码相机·qt
苦瓜汤补钙2 小时前
Oracle JDK8 环境配置-Win11
开发语言·数据库·笔记·oracle
overmind2 小时前
oeasy Python 102 集合_运算_交集_并集_差集_对称差集
开发语言·python