java之使用字节流上传用户头像

1.对字节流、字符流区分,字节流处理音视频,文本;字符流只能处理文本
字节流顶层类InputStream/OutputStream
使用FileInputStream BufferInputStream,使用缓冲区读写时,需要传入FileInputStream对象
BufferInputStream bo = new BufferInputStream(new FileInputStream("file"));
字符流,Reader Writer Filereader BufferReader
常用成员方法read,write;
读写时可使用逐个字符读写,返回int ASCII码值,当-1时,代表结束
可使用数组接收,当读取的内容为-1时,代表结束
2.SimpleDateFormat处理时间字符串
3.substring字符串截取,截取后n位,截取某个标志后n位
4.endswith(),验证文件后缀,返回boolean

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class PracticeuploadDemo {
    public static void main(String[] args) throws IOException {
        //调用getpath
        //Practiceuploadimggetpath plg = new Practiceuploadimggetpath();
        String str = getPath();
        File file = new File(str);
        if(file.exists() == false){
            System.out.println("文件不存在");
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHMMss");
        String curdate = sdf.format(new Date());
        File updateimgdir = new File("D:\\lib\\"+curdate+".png");
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str));
        BufferedOutputStream bos =
                new BufferedOutputStream(new FileOutputStream(updateimgdir));
        //使用字节读取
        int files ;
        while((files = bis.read()) != -1){
            bos.write(files);
        }
        if(updateimgdir.exists() == false){
            System.out.println("图片上传失败!请重试");
            bis.close();
            bos.close();
            return;
        }
        else
        {
            System.out.println("图片上传成功,请查看");
            bis.close();
            bos.close();
        }
    }
    public static  String getPath(){
        String updir1 = "eror";
        //定义上传地址
        final String UPDIR = "D:\\lib";
        //限制上传次数
        int count = 0;
        Scanner scanner = new Scanner(System.in);
        while(count < 5){
            System.out.println("请输入头像路径");
            String uploaddir = scanner.nextLine();
            if(uploaddir.length() <= 4){
                System.out.println("输入内容过短,请重试");
                count ++;
            }
            else {
                String imgname = uploaddir.substring(uploaddir.length() - 4);

                File file =new File(uploaddir);
                File file1 = new File("D:\\lib");
             if(file.exists() == false){
                System.out.println("路径不存在,请重试");
                count ++;
                //break;
            }
            else if(file.isFile() == false){
                System.out.println("上传的不是文件,请重试");
                count ++;
            }
            else if(".img".equals(imgname) || ".png".equals(imgname) || ".jpg".equals(imgname)){
                System.out.println("文件校验通过!!");
                updir1 = uploaddir;
                break;
            }
            else {
                System.out.println("上传文件不是图片格式,请重试");
                count++;
            }
        }}
        if(count >= 5){
            System.out.println("上传次数超限,请稍后再试!");
        }
        return updir1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值