模拟文件上传

package com.hp.demo;

import java.io.*;
import java.util.concurrent.atomic.AtomicLong;

public class PictureUp {
    public static void main(String[] args) throws IOException, InterruptedException {
        //创建文件对象
        File file = new File("C:\\Users\\46237\\Desktop\\test");

        String s[] =  file.list();
        for (int i = 0; i < s.length; i++) {
//            AtomicLong len = new AtomicLong();
//            AtomicLong len1 = new AtomicLong();

            int finalI = i;
            Thread  thread = new Thread(() -> {

                FileInputStream inputStream1 = null;
                FileOutputStream outputStream1 =null;
                try {
                    //读取此目标目录下的图片文件
                    inputStream1 = new FileInputStream("C:\\Users\\46237\\Desktop\\test\\" + s[finalI]);

                    File file1 = new File("C:\\Users\\46237\\Desktop\\test\\" + s[finalI]);
//                    len.set(file1.length());
//                    System.out.println("len = " + len);
                    //将目标目录下的图片文件保存在此新目录下
                    outputStream1 = new FileOutputStream("C:\\Users\\46237\\Desktop\\test1\\" + s[finalI]);
                    int a = 0;
                    byte b[] = new byte[512];
                    //判断,调用inputStream.read ()方法,从输入流中读取一个数据,并返回这个字节。读取内容达到末尾后是否返回-1
                    while(true){
                        try {
                            if (!((a = inputStream1.read()) != -1)) break;
                                outputStream1.write(b, 0, a);
                                File file2 = new File("C:\\Users\\46237\\Desktop\\test1\\" + s[finalI]);
//                                len1.set(file2.length());
//                            System.out.println("len1 = " + len1);
//                                double cha = len1.get() / len.get();
//                            System.out.println(cha);
                        } catch (IOException e) {
                            e.printStackTrace();

                        }
                        //调用outputStream.write()方法字节输出流写入

                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }finally {
                    //关闭并释放输出流资源
                    try {
                        outputStream1.close();
                        inputStream1.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    //关闭并释放输入流资源

                }
            });

            thread.setName("子程序");
            System.out.println(thread.getName());
            thread.start();
            Thread.sleep(1000);
            Thread.yield();
            System.out.println("线程结束");
        }

    }
}

运行截图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值