大文件压缩包处理解决方案,防止内存溢出

maven依赖

<!--导入word的文字-->
        <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>
        <!--zip压缩包配置-->
        <dependency>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.7.0</version>
        </dependency>

工具类

package com.ruoyi.common.utils.zip;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 解压Zip文件
 * @param path 文件目录
 */
import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class ZipUtil {
    static Logger logger = LoggerFactory.getLogger(ZipUtil.class.getName());
    private static final int buffer = 2048;
    /**
     * 解压Zip文件
     * @param path 文件目录
     */
    public static void unZip(String path)
    {
        int count = -1;
        String savepath = "";
        File file = null;
        InputStream is = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        savepath = path.substring(0, path.lastIndexOf(".")) + File.separator; //保存解压文件目录
        new File(savepath).mkdir(); //创建保存目录
        ZipFile zipFile = null;
        try
        {
            zipFile = new ZipFile(path,"gbk"); //解决中文乱码问题
            Enumeration<?> entries = zipFile.getEntries();
            while(entries.hasMoreElements())
            {
                byte buf[] = new byte[buffer];
                ZipEntry entry = (ZipEntry)entries.nextElement();
                String filename = entry.getName();
                boolean ismkdir = false;
                if(filename.lastIndexOf("/") != -1){ //检查此文件是否带有文件夹
                    ismkdir = true;
                }
                filename = savepath + filename;
                if(entry.isDirectory()){ //如果是文件夹先创建
                    file = new File(filename);
                    file.mkdirs();
                    continue;
                }
                file = new File(filename);
                if(!file.exists()){ //如果是目录先创建
                    if(ismkdir){
                        new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs(); //目录先创建
                    }
                }
                file.createNewFile(); //创建文件
                is = zipFile.getInputStream(entry);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos, buffer);
                while((count = is.read(buf)) > -1)
                {
                    bos.write(buf, 0, count);
                }
                bos.flush();
                bos.close();
                fos.close();
                is.close();
            }
            zipFile.close();
        }catch(IOException ioe){
            ioe.printStackTrace();
        }finally{
            try{
                if(bos != null){
                    bos.close();
                }
                if(fos != null) {
                    fos.close();
                }
                if(is != null){
                    is.close();
                }
                if(zipFile != null){
                    zipFile.close();
                }
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static File[] getFiles(String path){
        File file = new File(path);
        /* String[] test=file.list();
       for(int i=0;i<test.length;i++){
            logger.info(test[i]);
        }*/
       // logger.info("------------------");
        File[] tempList = file.listFiles();
        return tempList;
    }


    public static  List<String> getList(String patha){
        List<String> list = new ArrayList<>();
        String path=patha;
        File file=new File(path);
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            /*if (tempList[i].isFile()) {
                System.out.println("文件:"+tempList[i]);
            }*/
            if (tempList[i].isDirectory()) {
                list.add(tempList[i].getPath());
                //System.out.println("文件夹:"+tempList[i].getPath());
                //递归:
                getList(tempList[i].getPath());
            }
        }
        return list;
    }


    public static void main(String[] args)
    {
        String path ="F:\\";
        String zipFileName = "202006_2088801990822863.zip";
        String pathName = path+zipFileName;

        unZip(pathName);
        String f = "F:\\202006_2088801990822863";
        File file = new File(f);
        String[] test=file.list();
        for(int i=0;i<test.length;i++){
            System.out.println(test[i]);
        }
        System.out.println("------------------");
        String fileName = "";
        File[] tempList = file.listFiles();
        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                System.out.println("文   件:"+tempList[i]);
                fileName = tempList[i].getName();
                System.out.println("文件名:"+fileName);
            }
            if (tempList[i].isDirectory()) {
                System.out.println("文件夹:"+tempList[i]);
            }
        }
    }
}

工具类上传进行限制大小

package com.ruoyi.common.utils.file;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.Seq;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * 文件上传工具类
 *
 * @author ruoyi
 */
public class FileUploadUtils
{
    /**
     * 默认大小 50M
     */
    public static final long DEFAULT_MAX_SIZE = 10240* 1024 * 1024;

    /**
     * 默认的文件名最大长度 100
     */
    public static final int DEFAULT_FILE_NAME_LENGTH = 1000;

    /**
     * 默认上传的地址
     */
    private static String defaultBaseDir = RuoYiConfig.getProfile();

    public static void setDefaultBaseDir(String defaultBaseDir)
    {
        FileUploadUtils.defaultBaseDir = defaultBaseDir;
    }

    public static String getDefaultBaseDir()
    {
        return defaultBaseDir;
    }

    /**
     * 以默认配置进行文件上传
     *
     * @param file 上传的文件
     * @return 文件名称
     * @throws Exception
     */
    public static final String upload(MultipartFile file) throws IOException
    {
        try
        {
            return upload(getDefaultBaseDir(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        }
        catch (Exception e)
        {
            throw new IOException(e.getMessage(), e);
        }
    }

    /**
     * 根据文件路径上传
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @return 文件名称
     * @throws IOException
     */
    public static final String upload(String baseDir, MultipartFile file) throws IOException
    {
        try
        {
            return upload(baseDir, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        }
        catch (Exception e)
        {
            throw new IOException(e.getMessage(), e);
        }
    }

    /**
     * 文件上传
     *
     * @param baseDir 相对应用的基目录
     * @param file 上传的文件
     * @param allowedExtension 上传文件类型
     * @return 返回上传成功的文件名
     * @throws FileSizeLimitExceededException 如果超出最大大小
     * @throws FileNameLengthLimitExceededException 文件名太长
     * @throws IOException 比如读写文件出错时
     * @throws InvalidExtensionException 文件校验异常
     */
/*    public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
            InvalidExtensionException
    {
        int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
        {
            throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
        }

        assertAllowed(file, allowedExtension);

        String fileName = extractFilename(file);

        String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
        file.transferTo(Paths.get(absPath));
        return getPathFileName(baseDir, fileName);
    }*/
    public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException
    {
        String fileName = extractFilename(file);
        String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
        file.transferTo(Paths.get(absPath));
        return getPathFileName(baseDir, fileName);
    }
    /**
     * 编码文件名
     */
    public static final String extractFilename(MultipartFile file)
    {
        return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
                FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
    }

    public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
    {
        File desc = new File(uploadDir + File.separator + fileName);

        if (!desc.exists())
        {
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
        }
        return desc;
    }

    public static final String getPathFileName(String uploadDir, String fileName) throws IOException
    {
        int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
        String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
        return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
    }

    /**
     * 文件大小校验
     *
     * @param file 上传的文件
     * @return
     * @throws FileSizeLimitExceededException 如果超出最大大小
     * @throws InvalidExtensionException
     */
    public static final void assertAllowed(MultipartFile file, String[] allowedExtension)
            throws FileSizeLimitExceededException, InvalidExtensionException
    {
        long size = file.getSize();
        if (size > DEFAULT_MAX_SIZE)
        {
            throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
        }

        String fileName = file.getOriginalFilename();
        String extension = getExtension(file);
        if (allowedExtension != null && !isAllowedExtension(extension, allowedExtension))
        {
            if (allowedExtension == MimeTypeUtils.IMAGE_EXTENSION)
            {
                throw new InvalidExtensionException.InvalidImageExtensionException(allowedExtension, extension,
                        fileName);
            }
            else if (allowedExtension == MimeTypeUtils.FLASH_EXTENSION)
            {
                throw new InvalidExtensionException.InvalidFlashExtensionException(allowedExtension, extension,
                        fileName);
            }
            else if (allowedExtension == MimeTypeUtils.MEDIA_EXTENSION)
            {
                throw new InvalidExtensionException.InvalidMediaExtensionException(allowedExtension, extension,
                        fileName);
            }
            else if (allowedExtension == MimeTypeUtils.VIDEO_EXTENSION)
            {
                throw new InvalidExtensionException.InvalidVideoExtensionException(allowedExtension, extension,
                        fileName);
            }
            else
            {
                throw new InvalidExtensionException(allowedExtension, extension, fileName);
            }
        }
    }

    /**
     * 判断MIME类型是否是允许的MIME类型
     *
     * @param extension
     * @param allowedExtension
     * @return
     */
    public static final boolean isAllowedExtension(String extension, String[] allowedExtension)
    {
        for (String str : allowedExtension)
        {
            if (str.equalsIgnoreCase(extension))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取文件名的后缀
     *
     * @param file 表单文件
     * @return 后缀名
     */
    public static final String getExtension(MultipartFile file)
    {
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        if (StringUtils.isEmpty(extension))
        {
            extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
        }
        return extension;
    }
}

直接解压压缩包内存进行上传

public AjaxResult addnew(@RequestParam("file") MultipartFile file) throws IOException {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        if (fileName.equals("") || fileName == null) {
            return AjaxResult.error("上传文件为空");
        }
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        String id = "";
        if (!prefix.equals(".zip")) {
            return AjaxResult.error("只支持.zip格式的压缩包文件");
        }
        String fileNamereal = fileName.substring(0, fileName.indexOf("."));


        Material material = new Material();
        material.setTitle(fileNamereal);
        if (UserConstants.NOT_UNIQUE.equals(materialService.checkScbNameUniqueQizidou(material))) {
            return AjaxResult.error("新增素材包'" + material.getTitle() + "'失败,素材包名称已存在");
        } else {
            materialService.addScbQizidou(material);
            Material ww = materialService.selectScbQizidou(material);
            id = String.valueOf(ww.getId());
            //materialService.addScb(material);
        }

        //----------------------------------------------------------------处理文件名存储
        InputStream input = file.getInputStream();
        //获取ZIP输入流(一定要指定字符集Charset.forName("GBK")否则会报java.lang.IllegalArgumentException: MALFORMED)
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));

        try {
            //定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题

            ZipEntry ze;
            Integer index = 0;
            //循环遍历
            while ((ze = zipInputStream.getNextEntry()) != null) {
                if (index >= 20000) {
                    zipInputStream.closeEntry();
                    input.close();
                    logger.info("最多支持20000个循环-棋子豆增加");
                    break;
                }

                Integer zhangjieid = null;//章节id 最多支持12个
                Integer mujieid = null;//幕id 最多支持20个
                Integer typecate = null;//文件类型依托用户使用


                String[] strings = ze.toString().split("/");
                try {
                    if (strings.length < 2) {
                        continue;
                    }
                    if (strings.length == 2) {
                        String type1 = strings[1];
                        char[] chars = type1.toCharArray();
                        zhangjieid = chars[1] - '0';
                        logger.info("第几章节" + zhangjieid);

                    }
                    if (strings.length == 3) {
                        String type1 = strings[2];
                        char[] chars1 = type1.toCharArray();
                        zhangjieid = chars1[1] - '0';
                        logger.info("第几章节" + zhangjieid);

                        String type2 = strings[2];
                        char[] chars2 = type2.toCharArray();
                        mujieid = chars2[1] - '0';
                        logger.info("第几幕" + mujieid);
                    }
                    if (strings.length == 4) {
                        String ifcate = strings[3];
                        if (ifcate.equals("图片")) {
                            String type1 = strings[1];
                            char[] chars1 = type1.toCharArray();
                            zhangjieid = chars1[1] - '0';
                            logger.info("第几章节" + zhangjieid);

                            String type2 = strings[2];
                            char[] chars2 = type2.toCharArray();
                            mujieid = chars2[1] - '0';
                            logger.info("第几幕" + mujieid);
                            typecate = 1;
                        } else {
                            if (ifcate.equals("音频")) {
                                String type1 = strings[1];
                                char[] chars1 = type1.toCharArray();
                                zhangjieid = chars1[1] - '0';
                                logger.info("第几章节" + zhangjieid);

                                String type2 = strings[2];
                                char[] chars2 = type2.toCharArray();
                                mujieid = chars2[1] - '0';
                                logger.info("第几幕" + mujieid);
                                typecate = 2;
                            } else {
                                if (ifcate.equals("视频")) {
                                    String type1 = strings[1];
                                    char[] chars1 = type1.toCharArray();
                                    zhangjieid = chars1[1] - '0';
                                    logger.info("第几章节" + zhangjieid);

                                    String type2 = strings[2];
                                    char[] chars2 = type2.toCharArray();
                                    mujieid = chars2[1] - '0';
                                    logger.info("第几幕" + mujieid);
                                    typecate = 3;
                                } else {
                                    if (ifcate.equals("文本")) {
                                        String type1 = strings[1];
                                        char[] chars1 = type1.toCharArray();
                                        zhangjieid = chars1[1] - '0';
                                        logger.info("第几章节" + zhangjieid);

                                        String type2 = strings[2];
                                        char[] chars2 = type2.toCharArray();
                                        mujieid = chars2[1] - '0';
                                        logger.info("第几幕" + mujieid);
                                        typecate = 4;
                                    } else {
                                        logger.info("传入的类型错误不支持");
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (strings.length == 5) {
                        String ifcate = strings[3];
                        if (ifcate.equals("图片")) {
                            String type1 = strings[1];
                            char[] chars1 = type1.toCharArray();
                            zhangjieid = chars1[1] - '0';
                            logger.info("第几章节" + zhangjieid);

                            String type2 = strings[2];
                            char[] chars2 = type2.toCharArray();
                            mujieid = chars2[1] - '0';
                            logger.info("第几幕" + mujieid);
                            typecate = 1;
                        } else {
                            if (ifcate.equals("音频")) {
                                String type1 = strings[1];
                                char[] chars1 = type1.toCharArray();
                                zhangjieid = chars1[1] - '0';
                                logger.info("第几章节" + zhangjieid);

                                String type2 = strings[2];
                                char[] chars2 = type2.toCharArray();
                                mujieid = chars2[1] - '0';
                                logger.info("第几幕" + mujieid);
                                typecate = 2;
                            } else {
                                if (ifcate.equals("视频")) {
                                    String type1 = strings[1];
                                    char[] chars1 = type1.toCharArray();
                                    zhangjieid = chars1[1] - '0';
                                    logger.info("第几章节" + zhangjieid);

                                    String type2 = strings[2];
                                    char[] chars2 = type2.toCharArray();
                                    mujieid = chars2[1] - '0';
                                    logger.info("第几幕" + mujieid);
                                    typecate = 3;
                                } else {
                                    if (ifcate.equals("文本")) {
                                        String type1 = strings[1];
                                        char[] chars1 = type1.toCharArray();
                                        zhangjieid = chars1[1] - '0';
                                        logger.info("第几章节" + zhangjieid);

                                        String type2 = strings[2];
                                        char[] chars2 = type2.toCharArray();
                                        mujieid = chars2[1] - '0';
                                        logger.info("第几幕" + mujieid);
                                        typecate = 4;
                                    } else {
                                        logger.info("传入的类型错误不支持");
                                        break;
                                    }
                                }
                            }
                        }

                    }

                } catch (Exception e) {
                    logger.info("传入的类型错误不支持" + e.getStackTrace());
                    break;
                }

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                if ((!ze.isDirectory() && ze.toString().endsWith("txt"))) {
                    //读取
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = zipInputStream.read(buffer)) > -1) {
                        baos.write(buffer, 0, len);
                    }
                    baos.flush();
                    byte[] bytes = baos.toByteArray();
                    String title = ze.getName().substring(ze.getName().lastIndexOf("/") + 1);
                    String contex = new String(baos.toByteArray());
                    String bq = title.substring(0, title.indexOf("."));
                    Material addsc = new Material();
                    addsc.setBq(bq);
                    addsc.setTitle(bq);
                    addsc.setText(contex);
                    addsc.setType(typecate);
                    addsc.setZhangjieid(zhangjieid);
                    addsc.setMujieid(mujieid);
                    addsc.setPage_id(id);
                    //新增素材
                    addsc.setBqTitle(addsc.getBq() + addsc.getTitle());
                    String type = addsc.getType().toString();
                    if ("5".equals(type)) {
                        materialService.addMaterialJs(addsc);
                    } else {
                        materialService.addMaterial(addsc);
                    }
                    baos.close();
                    continue;
                }

                try {
                    if ((!ze.isDirectory() && ze.toString().endsWith("doc")) || (!ze.isDirectory() && ze.toString().endsWith("docx"))) {
                        String context = null;
                        if (ze.toString().endsWith("doc")) {
                            //读取
                            byte[] buffer = new byte[1024];
                            int len;
                            while ((len = zipInputStream.read(buffer)) > -1) {
                                baos.write(buffer, 0, len);
                            }
                            baos.flush();
                            InputStream in = new ByteArrayInputStream(baos.toByteArray());
                            //InputStream inputStream = baos.toInputStream();
                            WordExtractor ex = new WordExtractor(in);
                            //WordExtractor ex = new WordExtractor(is);
                            context = ex.getText();
                            ex.close();
                            baos.close();
                        }
                        if (ze.toString().endsWith("docx")) {
                            //读取
                            byte[] buffer = new byte[1024];
                            int len;
                            while ((len = zipInputStream.read(buffer)) > -1) {
                                baos.write(buffer, 0, len);
                            }
                            baos.flush();
                            InputStream inputStream = baos.toInputStream();
                            XWPFDocument xdoc = null;
                            try {
                                xdoc = new XWPFDocument(OPCPackage.open(inputStream));
                            } catch (InvalidFormatException e) {
                                e.printStackTrace();
                            }
                            XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
                            context = extractor.getText();
                            extractor.close();
                        }


                        String title = ze.getName().substring(ze.getName().lastIndexOf("/") + 1);
                        String bq = title.substring(0, title.indexOf("."));
                        Material addsc = new Material();
                        addsc.setBq(bq);
                        addsc.setTitle(bq);
                        addsc.setText(context);
                        addsc.setType(typecate);
                        addsc.setZhangjieid(zhangjieid);
                        addsc.setMujieid(mujieid);
                        addsc.setPage_id(id);
                        //新增素材
                        addsc.setBqTitle(addsc.getBq() + addsc.getTitle());
                        String type = addsc.getType().toString();
                        if ("5".equals(type)) {
                            materialService.addMaterialJs(addsc);
                        } else {
                            materialService.addMaterial(addsc);
                        }
                        continue;
                    }
                } catch (Exception e) {
                    logger.error("解析文本失败");
                }
                if (!ze.isDirectory()) {
                    logger.info("进入了视频 ");
                    //读取
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = zipInputStream.read(buffer)) > -1) {
                        baos.write(buffer, 0, len);
                    }
                    logger.info("进入了读取视频阶段 ");
                    baos.flush();
                    String filenameurl = ze.getName().substring(ze.getName().lastIndexOf("/") + 1);
                    logger.info("进入了流过程filename: " + filenameurl);

                    InputStream inputStream = baos.toInputStream();
                    logger.info("走完了流的转化");
                    //MultipartFile files = new MockMultipartFile(filenameurl,  baos.toInputStream());
                    // Map<String, String> qizidou = add(files, "qizidou");//上传腾讯云
                    Map<String, String> qizidou = addstream(filenameurl, inputStream, "qizidou");//上传腾讯云
                    logger.info("走完了上传腾讯云");
                    if (!qizidou.isEmpty()) {
                        String filePath = qizidou.get("filePath");
                        String name = qizidou.get("name");
                        String cosName = qizidou.get("cosName");
                        String bq = filenameurl.substring(0, filenameurl.indexOf("."));

                        Material addsc = new Material();
                        addsc.setBq(bq);
                        addsc.setTitle(bq);
                        addsc.setType(typecate);
                        addsc.setMujieid(mujieid);
                        addsc.setZhangjieid(zhangjieid);
                        addsc.setUrl(filePath);
                        addsc.setImage(name);
                        addsc.setCosName(cosName);
                        addsc.setPage_id(id);
                        //新增素材
                        addsc.setBqTitle(addsc.getBq() + addsc.getTitle());
                        String type = addsc.getType().toString();
                        if ("5".equals(type)) {
                            materialService.addMaterialJs(addsc);
                        } else {
                            materialService.addMaterial(addsc);
                        }

                    }


                }
                ++index;
            }
        } catch (Exception e) {
            //一定记得关闭流
            zipInputStream.closeEntry();
            input.close();
        } finally {
            //一定记得关闭流
            zipInputStream.closeEntry();
            input.close();
            return AjaxResult.success("棋子豆提醒您:成功了嘿!");
        }

    }

先上传服务器再解压多线程异步处理

 @PostMapping("/add")
    public AjaxResult add(@RequestParam("file") MultipartFile file) throws IOException {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        if (fileName.equals("") || fileName == null) {
            return AjaxResult.error("上传文件为空");
        }
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        String id = "";
        if (!prefix.equals(".zip")) {
            return AjaxResult.error("只支持.zip格式的压缩包文件");
        }
        String fileNamereal = fileName.substring(0, fileName.indexOf("."));
        Material material = new Material();
        material.setTitle(fileNamereal);
        if (UserConstants.NOT_UNIQUE.equals(materialService.checkScbNameUniqueQizidou(material))) {
            return AjaxResult.error("新增素材包'" + material.getTitle() + "'失败,素材包名称已存在");
        } else {
            materialService.addScbQizidou(material);
            Material ww = materialService.selectScbQizidou(material);
            id = String.valueOf(ww.getId());
            //materialService.addScb(material);
        }

        //上传文件,返回文件名
        String fileNamelinux = null;
        String baseDir = RuoYiConfig.getUploadPath();
        try {
            fileNamelinux = FileUploadUtils.upload(baseDir, file);
        } catch (IOException e) {
            e.printStackTrace();
            String message = "附件上传失败,请重新上传或联系管理员";
            return AjaxResult.error(message);
        }
        String unZipFile = fileNamelinux.replaceAll("/profile/upload", baseDir);

        SysAttachmentlog sysAttachment = new SysAttachmentlog();
        sysAttachment.setBusinessType("");
        sysAttachment.setBusinessId(id);
        sysAttachment.setDelFlag(1);
        sysAttachment.setFileNameReal(fileName);
        sysAttachment.setFileNameShow(fileNamereal);
        sysAttachment.setFilePath(unZipFile);// ChessmenBean:原逻辑没有路径,不好跟踪
        sysAttachment.setFilePath(fileNamelinux);//linux直接返回的路径
        sysAttachment.setFileSize(file.getSize());//文件大小
        sysAttachment.setCreateTime(DateUtils.getNowDate());
        sysAttachmentService.insertCwdzSysAttachment(sysAttachment);
        System.out.println("开始一个新线程");
        String finalId = id;
        new Thread(() -> {
            printA(unZipFile, finalId);
        }).start();
        System.out.println("主线程完成");
        return AjaxResult.success("压缩包上传成功,后台正在玩命处理中,等待素材显示了再进行剧本填充");
    }

    private void printA(String unZipFile, String id) {
        logger.info("开始读取服务上的文件进行解压,上传云端,整理素材包");
        //解压压缩包
        ZipUtil.unZip(unZipFile);
        //获取文件列表
       /* File[] tempList=ZipUtil.getFiles(unZipFile.replaceAll(".zip",""));
        for (File f : tempList) {
            logger.info("文件列表"+f);
        }*/
        List<String> list = ZipUtil.getList(unZipFile.replaceAll(".zip", ""));
        if (list.size() > 0) {
            String jieyafile = list.get(0);
            List<String> zhangji = ZipUtil.getList(jieyafile);
            if (zhangji.size() > 0) {
                logger.info("第几章" + zhangji);
                for (String ze : zhangji) {
                    Integer zhangjieid = null;
                    String title = ze.substring(ze.lastIndexOf("\\") + 1);
                    char[] chars = title.toCharArray();
                    zhangjieid = chars[1] - '0';
                    List<String> mu = ZipUtil.getList(ze);
                    if (mu.size() > 0) {
                        logger.info("第几幕" + mu);
                        Integer mujieid = null;
                        for (String m : mu) {
                            String muti = m.substring(m.lastIndexOf("\\") + 1);
                            char[] charsd = muti.toCharArray();
                            mujieid = charsd[1] - '0';
                            List<String> type = ZipUtil.getList(m);
                            if (type.size() > 0) {
                                logger.info("什么类型" + type);
                                for (String mm : type) {
                                    Integer typecate = null;
                                    String typeyin = mm.substring(mm.lastIndexOf("\\") + 1);
                                    logger.info("typeyin" + typeyin);
                                    if (typeyin.equals("图片")) {
                                        typecate = 1;
                                        logger.info("mm" + mm);
                                        File[] jpgs = ZipUtil.getFiles(mm);
                                        if (jpgs.length > 0) {
                                            logger.info("jpg图片路径" + jpgs);
                                            for (File tupian : jpgs) {
                                                logger.info("打印图片" + tupian);
                                                upchuan(zhangjieid, mujieid, typecate, tupian, id);
                                            }
                                        }

                                    }
                                    if (typeyin.equals("音频")) {
                                        typecate = 2;
                                        File[] yinpin = ZipUtil.getFiles(mm);
                                        if (yinpin.length > 0) {
                                            for (File file : yinpin) {
                                                logger.info("打印音频" + file);
                                                upchuan(zhangjieid, mujieid, typecate, file, id);
                                            }
                                        }

                                    }
                                    if (typeyin.equals("视频")) {
                                        typecate = 3;
                                        File[]  shipin = ZipUtil.getFiles(mm);
                                        if (shipin.length > 0) {
                                            for (File file : shipin) {
                                                    logger.info("打印视频" + file);
                                                    upchuan(zhangjieid, mujieid, typecate, file, id);
                                            }
                                        }
                                    }
                                    if (typeyin.equals("文本")) {
                                        typecate = 4;
                                        File[]  wenben = ZipUtil.getFiles(mm);
                                        if (wenben.length > 0) {
                                            for (File file : wenben) {
                                                logger.info("打印文本" + file);
                                                upchuan(zhangjieid, mujieid, typecate, file, id);
                                            }
                                        }

                                    }

                                }

                            }

                        }
                    }
                }

            }


        }

    }


    public void upchuan(Integer zhangjieid, Integer mujieid, Integer typecate, File file, String id) {

        logger.info("filename" + file.getName());

        logger.info("普通打印" + file);
        if (file.getName().endsWith("txt")) {
            byte[] buffer = null;
            try {
                InputStream fis = new FileInputStream(file);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] b = new byte[1024];
                int n;
                while ((n = fis.read(b)) != -1) {
                    bos.write(b, 0, n);
                }
                fis.close();
                bos.close();
                byte[] bytes = bos.toByteArray();
                String title = file.getName().substring(file.getName().lastIndexOf("/") + 1);
                String contex = new String(bytes);
                String bq = title.substring(0, title.indexOf("."));
                Material addsc = new Material();
                addsc.setBq(bq);
                addsc.setTitle(bq);
                addsc.setText(contex);
                addsc.setType(typecate);
                addsc.setZhangjieid(zhangjieid);
                addsc.setMujieid(mujieid);
                addsc.setPage_id(id);
                //新增素材
                addsc.setBqTitle(addsc.getBq() + addsc.getTitle());
                String type = addsc.getType().toString();
                if ("5".equals(type)) {
                    materialService.addMaterialJs(addsc);
                } else {
                    materialService.addMaterial(addsc);
                }
                return;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return;

            } catch (IOException e) {

                e.printStackTrace();
                return;

            }


        }

        try {
            if ((file.getName().endsWith("doc")) || file.getName().endsWith("docx")) {
                String context = null;
                if (file.getName().endsWith("doc")) {
                    InputStream in = new FileInputStream(file);
                    //InputStream inputStream = baos.toInputStream();
                    WordExtractor ex = new WordExtractor(in);
                    //WordExtractor ex = new WordExtractor(is);
                    context = ex.getText();
                    ex.close();
                    in.close();
                }
                if (file.getName().endsWith("docx")) {
                    InputStream in = new FileInputStream(file);
                    XWPFDocument xdoc = null;
                    try {
                        xdoc = new XWPFDocument(OPCPackage.open(in));
                    } catch (InvalidFormatException e) {
                        e.printStackTrace();
                    }
                    XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
                    context = extractor.getText();
                    extractor.close();
                    in.close();
                }


                String title = file.getName().substring(file.getName().lastIndexOf("/") + 1);
                String bq = title.substring(0, title.indexOf("."));
                Material addsc = new Material();
                addsc.setBq(bq);
                addsc.setTitle(bq);
                addsc.setText(context);
                addsc.setType(typecate);
                addsc.setZhangjieid(zhangjieid);
                addsc.setMujieid(mujieid);
                addsc.setPage_id(id);
                //新增素材
                addsc.setBqTitle(addsc.getBq() + addsc.getTitle());
                String type = addsc.getType().toString();
                if ("5".equals(type)) {
                    materialService.addMaterialJs(addsc);
                } else {
                    materialService.addMaterial(addsc);
                }
                return;
            }
        } catch (Exception e) {
            logger.error("解析文本失败");
            return;
        }

        String filenameurl = file.getName();
        logger.info("filenameurl" + filenameurl);
        InputStream input = null;
        try {
            input = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Map<String, String> qizidou = addstream(filenameurl, input, "qizidou");//上传腾讯云
        logger.info("走完了上传腾讯云");
        if (!qizidou.isEmpty()) {
            String filePath = qizidou.get("filePath");
            String name = qizidou.get("name");
            String cosName = qizidou.get("cosName");
            String bq = filenameurl.substring(0, filenameurl.indexOf("."));

            Material addsc = new Material();
            addsc.setBq(bq);
            addsc.setTitle(bq);
            addsc.setType(typecate);
            addsc.setMujieid(mujieid);
            addsc.setZhangjieid(zhangjieid);
            addsc.setUrl(filePath);
            addsc.setImage(name);
            addsc.setCosName(cosName);
            addsc.setPage_id(id);
            //新增素材
            addsc.setBqTitle(addsc.getBq() + addsc.getTitle());
            String type = addsc.getType().toString();
            if ("5".equals(type)) {
                materialService.addMaterialJs(addsc);
            } else {
                materialService.addMaterial(addsc);
            }

        }


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值