多线程io分离与合并(大文件压缩包解密)

需求:用户上传了一个大文件压缩包,压缩包是加密的,需要在后台进行解密操作,文件大小(1G)左右,解密过程需要1分钟。。。需要提速

ok,直接用java多线程的方式来解决吧,先读取文件,然后将io流切分,每段io开启一个线程进行解密,最后按顺序将解密后的io片段进行合并


闲话不说,直接上代码,不需要导入其他jar包

首先是工具

ioutil,就是关闭流而已

package com.noryar.filesystem.util;

import java.io.Closeable;
import java.io.IOException;

/**
 * IOUtil.
 * @author Leon Lee.
 */
public class IOUtil {
	/**
	 * close IO resource.
	 * @param closeables :IO resource, implements Closable
	 * @throws IOException :Exception
	 */
	public static void close(Closeable... closeables) throws IOException {
		if (closeables != null) {
			for (Closeable closeable : closeables) {
				if (closeable != null) {
					closeable.close();
				}
			}
		}
	}
	/**
	 * close IO resource. do not throw Exception.
	 * @param closeables : IO resource, implements Closable
	 */
	public static void closeQuietly(Closeable... closeables) {
		try {
			close(closeables);
		} catch (IOException e) {
			// do nothing
		}
	}
}


加密解密工具类

package com.noryar.filesystem.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * SecurityFileUtil.
 * @author Leon Lee.
 */
public class SecurityUtil {

    /**
     * init AES Cipher.
     * @param keySource keySource
     * @param cipherMode keyMode
     * @return Cipher
     */
    private static Cipher initAESCipher(final String keySource, final int cipherMode) {
        KeyGenerator keyGenerator = null;
        Cipher cipher = null;
        try {
            keyGenerator = KeyGenerator.getInstance("AES");
            // init 128 key with import random source[key]
            keyGenerator.init(128, new SecureRandom(keySource.getBytes()));
            // create key
            SecretKey secretKey = keyGenerator.generateKey();
            // get key of byte[]
            byte[] codeFormat = secretKey.getEncoded();
            SecretKeySpec key = new SecretKeySpec(codeFormat, "AES");
       
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值