模拟用户上传头像功能,假设所有的用户头像都应该上传到:项目的lib文件夹中

题目:模拟用户上传头像功能,假设所有的用户头像都应该上传到:项目的lib文件夹中

//需求:模拟用户上传头像功能,假设所有的用户头像都应该上传到:项目的lib文件夹中
public class UpPicture {
    public static void main(String[] args) throws IOException {
        //1.定义一个方法,用来获取要上传的用户头像的路径   getPath
        File path = getpath();
        System.out.println(path);//e:\temp.bmp
        //2.定义一个方法,用来判断要上传的用户头像,在lib文件夹中是否存在
        boolean flag=isExists(path.getName());//path.getName:只拿到全路径最后得名字,也就是temp.bmp
        //3.如果存在,提示:该用户已经存在,上传失败
        if (flag){
            System.out.println("该用户头像已经存在,上传失败!");
        }
        else {
            //4.如果不存在,就上传该用户头像,并提示上传成功
            upPicture(path);

        }
    }

    //1.定义一个方法,用来获取要上传的用户头像的路径   getPath
    public static File getpath(){
        //1.提示用户录入要上传的用户头像路径,并接收
        Scanner sc=new Scanner(System.in);
        while (true) {
            System.out.println("请录入您要上传的用户头像路径:");
            String path = sc.nextLine();
            //2.判断该路径的后缀名是否是: .jpg .png .bmp
            //3.如果不是,就提示:你录入的不是图片,请重新录入
            if (!path.endsWith(".jpg") && !path.endsWith(".png") && !path.endsWith(".bmp")) {
                System.out.println("你录入的不是图片,请重新录入!");
                //细节,千万注意,别忘了写
                continue;
            }

            //4.如果是,程序接着执行,判断该路径是否存在,并且是否是文件
            File file = new File(path);
            if (file.exists() && file.isFile()) {
                return file;
            } else {
                //5.如果不是,就提示:你录入的路径不合法,请重新输入
                System.out.println("你录入的路径不合法,请重新输入!");
            }
        }
    }

    //2.定义一个方法,用来判断要上传的用户头像,在lib文件夹中是否存在
    public static boolean isExists(String path){
        //1.将lib文件夹封装成对象
        File file = new File("lib");
        //2.获取lib文件夹中的所有文件(夹)的名称数组
        String[] names = file.list();
        //3.遍历第二步获取到的数组,用获取到的数据一次和path进行比较
        for (String name : names) {
            if (name.equals(path)){
                //4.如果一致,说明giant用户头像已经存在了,就返回true
                return true;
            }
        }
        //5.如果不一致,说明该用户头像不存在,就返回false
        return false;
    }

    public static void upPicture(File path) throws IOException {
        //创建一个缓冲输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
        //创建一个缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("lib/"+path.getName()));
        //定义一个变量,作为读取数据的有效长度
        int len;
        //循环
        while ((len = bis.read()) != -1){
            //将读取到的数据写入目标文件中
            bos.write(len);
        }

        //关闭资源
        bis.close();
        bos.close();

        System.out.println("上传成功!");
    }
}

运行结果:

e:\temp.bmp
e:\temp.bmp
上传成功!

在这里插入图片描述
此题为个人学习笔记,题目来源:黑马程序员,若有不足,感谢各位大佬不吝赐教。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值