java 文件上传(使用多线程)

1、对一个文件babse64加密,再上传到指定位置。使用多线程可以提高文件上传速度。
2、java 实现:
@Slf4j
public class FileUploadDemo {


    public static void uploadFiles(String localFilePath,String uploadFile){
        InputStream is = null;
        FileOutputStream os = null;
        //获取上传文件的base64编码文件并进行解码
        try {
            is = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(localFilePath));
            //创建一个文件输出流
            os = new FileOutputStream(new File(uploadFile));
            //创建一个缓冲区
            byte[] buffer = new byte[10 * 1024 * 1024];
            //判断输入流中的数据是否已经读完的标识
            //循环将输入流读入到缓冲区当中,(len = in.read(buffer)>0表示里面还有数据)
            int length;
            while ((length = is.read(buffer))>0){
                os.write(buffer,0,length);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            log.error("未找到上传的文件"+ e);
            e.printStackTrace();
//            throw new ServerException("未找到文件");
        }catch (IOException e){
            log.error("文件读取异常" + e);
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    log.error("Exception:" + e);
                    e.printStackTrace();
                }


            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    log.error("Exception:"+ e);
                }
            }
        }


    }


    public static String EncoderFile(String filePath){
        InputStream is = null;
        byte[] data = null;
        try {
            //读取文件所有字节,方法1
//            is = new FileInputStream(filePath);
//            data = new byte[is.available()];
//            is.read(data);
//            is.close();
            //读取文件所有字节,方法2
            data = Files.readAllBytes(Paths.get(filePath));


        }catch (FileNotFoundException e) {
            log.error("未找到上传的文件"+ e);
            e.printStackTrace();
//            throw new ServerException("未找到文件");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    log.error("Exception:" + e);
                    e.printStackTrace();
                }


            }
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }
        //创建线程池
    static ThreadPoolExecutor executor = new ThreadPoolExecutor(2
            ,50
            ,10
            , TimeUnit.SECONDS
            ,new LinkedBlockingQueue<>(100)
            , Executors.defaultThreadFactory()
            ,new ThreadPoolExecutor.AbortPolicy()
    );

    public static void main(String[] args) {

        //计算文件上传时间
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        String localPath = "/Users/yangxusheng/Desktop/2.pdf";
        String s = EncoderFile(localPath);
        String s1 = EncoderFile(localPath);
        String s2 = EncoderFile(localPath);
        String uploadPath = "/Users/ttt/Desktop/1.pdf";
        String uploadPath1 = "/Users/ttt/Desktop/1.pdf";
        String uploadPath2 = "/Users/ttt/Desktop/1.pdf";
        //一般情况
//        uploadFiles(s,uploadPath);
//        uploadFiles(s1,uploadPath1);
//        uploadFiles(s2,uploadPath2);
        //使用多线程的情况
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(()->{
            uploadFiles(s,uploadPath);
            return 200;
        },executor);

        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(()->{
            uploadFiles(s1,uploadPath1);
            return 300;
        },executor);

        CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(()->{
            uploadFiles(s2,uploadPath2);
            return 300;
        },executor);
//
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeMillis());
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值