Download and parse TXT text content

Upload and download core code

package com.luntek.platform.ic_manufacturing_platform.utils;

import com.luntek.platform.ic_manufacturing_platform.enums.CommonExEnum;
import com.luntek.platform.ic_manufacturing_platform.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author Czw
 * @Description 将上传的multipartFile转为file
 * @Date 2019/4/23 0023 上午 9:47
 */
@Slf4j
public class FileUtil {
    
    

  

    /* 导出txt文件
     * @author
     * @param    response
     * @param    text 导出的字符串
     * @return
     */
    public static void exportTxt(HttpServletResponse response, String text) {
    
    
        response.setCharacterEncoding("utf-8");
        //设置响应的内容类型
        response.setContentType("text/plain");
        //设置文件的名称和格式
        response.addHeader("Content-Disposition", "attachment;filename="
                + genAttachmentFileName("试卷", "JSON_FOR_UCC_")//设置名称格式,没有这个中文名称无法显示
                + ".txt");
        BufferedOutputStream buff = null;
        ServletOutputStream outStr = null;
        try {
    
    
            outStr = response.getOutputStream();
            buff = new BufferedOutputStream(outStr);
            buff.write(text.getBytes("UTF-8"));
            buff.flush();
            buff.close();
        } catch (Exception e) {
    
    
            //LOGGER.error("导出文件文件出错:{}",e);
        } finally {
    
    
            try {
    
    
                buff.close();
                outStr.close();
            } catch (Exception e) {
    
    
                //LOGGER.error("关闭流对象出错 e:{}",e);
            }
        }
    }
    //防止中文文件名显示出错

    private static String genAttachmentFileName(String cnName, String defaultName) {
    
    
        try {
    
    
            cnName = new String(cnName.getBytes("gb2312"), "ISO8859-1");
        } catch (Exception e) {
    
    
            cnName = defaultName;
        }
        return cnName;
    }


    public static String fromTXTGainContent(MultipartFile file){
    
    
        //读取文件
        BufferedReader br = null;
        StringBuffer sb = null;
        try {
    
    
            br = new BufferedReader(new InputStreamReader(file.getInputStream(), StandardCharsets.UTF_8)); //这里可以控制编码
            sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
    
    
                sb.append(line);
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                assert br != null;
                br.close();
            } catch (Exception e) {
    
    
                e.printStackTrace();
            }
        }
        return new String(sb);
    }
}

Parsing

The export code is After FileUtil.exportTxt(response, objectEncJson);
importing, the parsed txt code is:fromTXTGainContent(MultipartFile file)

Guess you like

Origin blog.csdn.net/qq_42910468/article/details/105726463