java csv读取内容和下载存储

 

@PostMapping(value = "/upload")
public String uploadCsv(  @RequestPart MultipartFile file) {
    //获取当前文件路径
    String temporaryUrl = System.getProperty("user.dir");

    try {
        // 原始文件名
        String sourceName = file.getOriginalFilename();
        String fileType = sourceName.substring(sourceName.lastIndexOf("."));
        if (file.isEmpty() || StringUtils.isBlank(fileType)) {
            return OutVO.fail("文件格式不对!");
        }
        if (!CommonConstant.FILE_TEXT.equals(fileType.toLowerCase())
                && !CommonConstant.FILE_CSV.equals(fileType.toLowerCase())) {
            return OutVO.fail("文件格式不对!");
        }
        long size = file.getSize();
        if (size <= GregorianCalendar.ALL_STYLES) { return "文件大小不能小于0!"; }
        PredictionDataInfoVerify verify;
        CsvFileParser csvFileParser = new CsvFileParser(file.getInputStream());
        List title = csvFileParser.getLabels();
        csvFileParser.fileCoverOrAdd(file, title, verify,false,GregorianCalendar.AD);
        verify.setTitle(title);
        verify.setSpace(files.getSize());
        verify.setRowColumn(csvFileParser.getCurrLineNum() + File.separator + title.size());
        //保存对应的数据和地址

    } catch (Exception e) {
        log.error("上传文件异常,userId:{},error:{}", authUser.getUserId(), e.getMessage());
        return"上传失败!";
    }
}

/**
 * @author lichengying
 * @date 2019-11-25
 * @since 1.0
 */
public class CommonConstant {

    /** csv */
    public static String FILE_CSV = ".csv";

    /** text */
    public static String FILE_TEXT = ".text";

    public static Integer NUMBER_500 = 500;

    public static Integer NUMBER_1024 = 1024;



}
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;


/**
 * 上传入参
 * @author lichengying
 * @date 2019-11-25
 * @since 1.0
 */
@Setter
@Getter
@ToString
public class PredictionDataInfoVerify implements Serializable {

    private static final long serialVersionUID =  7238110508550390832L;

    private String uid;

    /**
     * 文件名称
     */
    private String fileName;

    /**
     * 文件空间大小
     */
    private Long space;

    /**
     * 数据行/列
     */
    private String rowColumn;

    /**
     * 文件模式,1:覆盖,2:追加,3:新建
     */
    @NotNull(message = "文件模式不能为空!")
    private Integer fileMode;

    /**
     * 文件目录
     */
    @NotBlank(message = "文件目录不能为空!")
    private String directory;

    /**
     * 上传地址
     */
    private String url;

    private List titleList;
    /**
     * 文件类型,1:表,2:文件夹
     */
    @NotNull(message = "文件类型不能为空!")
    private Integer fileType;

    /**
     * 父级id
     */
    @NotBlank(message = "父级id不能为空!")
    private String parentId;

    /**
     * 数据表名
     */
    @NotBlank(message = "表明不能为空!")
    private String tableName;

import java.io.*;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;

import com.Ostermiller.util.ExcelCSVParser;
import com.Ostermiller.util.LabeledCSVParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import static org.springframework.util.StringUtils.hasText;


/**
 *
 * @author lichengying
 * csv文件解析器
 *
 */
@Slf4j
public class CsvFileParser{

    private LabeledCSVParser csvParser;//csv解析器,对于第一行的表头信息,自动加载为索引关键字

    private int currLineNum = -1;//文件所读到行数

    private String[] currLine = null;//用来存放当前行的数据


    /**
     *  构造函数,
     *  Param: in InputStream 要解析的信息流
     *  throws IOException
     */
    public CsvFileParser(InputStream in) throws IOException {
        csvParser = new LabeledCSVParser(new ExcelCSVParser(in));
        currLineNum = csvParser.getLastLineNumber();
    }

    public CsvFileParser(String fileName) throws IOException {
        InputStream in = new FileInputStream(fileName);
        csvParser = new LabeledCSVParser(new ExcelCSVParser(in));
        currLineNum = csvParser.getLastLineNumber();
    }

    /**
     * 检查是否还有数据
     *
     * return ture 还有一行数据,false 没有数据
     */
    public boolean hasMore() throws IOException {
        int count = 1000;
        currLine = csvParser.getLine();
        currLineNum = csvParser.getLastLineNumber();
        if (count < currLineNum) {
            return false;
        }
        if (null == currLine) {
            return false;
        }
        return true;
    }

    /**
     * 返回当前行数据,关键字所指向的数据
     * param:String filedName 该行的表头
     * return:String 返回当前行数据,关键字所指向的数据
     */
    public String getByFieldName(String fieldName) {
        return csvParser.getValueByLabel(fieldName);
    }

    /**
     * 关闭解析器
     *
     *
     */
    public void close() throws IOException {
        csvParser.close();
    }

    /**
     * 读取当前行数据
     *
     *  return String[] 读取当前行数据
     */
    public String[] readLine() throws IOException {
        currLine = csvParser.getLine();
        currLineNum = csvParser.getLastLineNumber();
        return currLine;
    }

    public int getCurrLineNum(){
        return currLineNum;
    }

    /**
     * 解析文件字节
     * @param size
     * @return
     */
    public static String getPrintSize(long size) {
        // 如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义
        double value = (double) size;
        if (value < CommonConstant.NUMBER_1024) {
            return value + "B";
        } else {
            value = new BigDecimal(value / CommonConstant.NUMBER_1024).setScale(GregorianCalendar.LONG, BigDecimal.ROUND_DOWN).doubleValue();
        }
        // 如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位
        // 因为还没有到达要使用另一个单位的时候
        // 接下去以此类推
        if (value < CommonConstant.NUMBER_1024) {
            return value + "KB";
        } else {
            value = new BigDecimal(value / CommonConstant.NUMBER_1024).setScale(GregorianCalendar.LONG, BigDecimal.ROUND_DOWN).doubleValue();
        }
        if (value < CommonConstant.NUMBER_1024) {
            return value + "MB";
        } else {
            // 否则如果要以GB为单位的,先除于1024再作同样的处理
            value = new BigDecimal(value / CommonConstant.NUMBER_1024).setScale(GregorianCalendar.LONG, BigDecimal.ROUND_DOWN).doubleValue();
            return value + "GB";
        }
    }

    private static List query(String title,InputStream inputStream) throws IOException{
        CsvFileParser parser = new CsvFileParser(inputStream);
        List values = new ArrayList<>();
        try {
            while (parser.hasMore()) {
                values.add(parser.getByFieldName(title));
            }
            parser.close();
        } catch (IOException e) {
            log.error("获取csv文件数据异常,title:{},error:{}",title,e.getMessage());
            throw new IOException("获取csv文件数据异常!");
        }finally {
            parser.close();
        }



        return values;
    }
}

    /**
     * 下载
     * @param file
     * @param titles
     * @param verify
     * @throws Exception
     */
    public void fileCoverOrAdd(MultipartFile file, List titles, PredictionDataInfoVerify verify, boolean flag, int type) throws Exception{
        CsvFileParser parser = new CsvFileParser(file.getInputStream());
        int count = GregorianCalendar.ALL_STYLES;
        while (parser.hasMore()) {
            count ++ ;
        }
        verify.setRowColumn(String.valueOf(count));
        List data = new ArrayList<>();
        if (!flag) {
            String[] title = titles.toArray(new String[titles.size()]);
            data.add(title);
        }
        verify.setTitleList(data);
        parser.close();
        String url = verify.getUrl();
        //add
        if (type == GregorianCalendar.AD
                || type == GregorianCalendar.LONG) {
            addOrCover(url,file);
        }else {
            addition(url,file);
        }


    }

    /**
     * 读取文件所有内容
     * @param titles
     * @param fileName
     * @return
     * @throws IOException
     */
    public static Map queryCsvAll(List titles,String fileName) throws IOException{
        String record = null;
        List records = new ArrayList<>();
        int count = GregorianCalendar.ALL_STYLES;

        Map map = new HashMap<>();
        BufferedReader bufferedReader = null;
        try{
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            while ((record = bufferedReader.readLine()) != null) {
                if (count > GregorianCalendar.ALL_STYLES && count <= CommonConstant.NUMBER_500) {
                    String[] value = record.split(",");
                    List valueList = new ArrayList<>();
                    for (String file : value) {
                        String values = " ";
                        if (hasText(file)) {
                            values = file;
                        }
                        valueList.add(values);
                    }
                    if (value.length < titles.size()) {
                        valueList.add(" ");
                    } else if (value.length > titles.size()){
                        break;
                    }
                    records.add(valueList.toArray(new String[]{}));
                }
                count++;
            }
            map.put("fileList",records);
            map.put("fileTitle",titles);
            bufferedReader.close();
            return map;
        } catch (IOException e) {
            log.error("获取csv文件数据异常,title:{},error:{}", StringUtils.join(titles,","),e.getMessage());
            throw new IOException("获取csv文件数据异常!");
        }finally {
            bufferedReader.close();
        }


    }

    /**
     * 追加
     * @param fileName
     * @throws IOException
     */
    public void addition(String fileName,MultipartFile file1) throws Exception{
        File file = new File(fileName);
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(file1.getInputStream(), StandardCharsets.UTF_8),2048);

        FileOutputStream fos = new FileOutputStream(file,true);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");

        BufferedWriter out = new BufferedWriter(osw);
        PrintWriter printWriter = new PrintWriter(out);
        try {
            String value = null;
            int i=0;
            while ((value = bufferedReader.readLine()) != null){
                if(i!=0){
                    printWriter.println(value);
                }else{
                    printWriter.println();
                }
                i++;
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            log.error("csv追加文件异常,fileName:{},error:{}", fileName, e.getMessage());
            throw new Exception("csv追加文件异常!");
        }finally {
            if (out != null) {
                out.close();
            }
        }


    }

    /**
     * 新增和覆盖
     * @throws IOException
     */
    public void addOrCover(String url,MultipartFile files) throws Exception{
        //根据真实路径创建目录文件
        File picSaveFile = new File(url + File.separator + files.getOriginalFilename());
        //判断是否存在路径
        if(!picSaveFile.exists()) {
            //如果不存在则创建路径
            picSaveFile.mkdirs();
        }
        try {
            files.transferTo(picSaveFile); // 保存文件
        // 转存文件 */
        } catch (Exception e) {
            log.error("csv新增或覆盖文件异常,url:{},error:{}", url, e.getMessage());
            throw new Exception("csv新增或覆盖文件异常");
        }
    }


    public List getLabels() throws IOException{
        List titles = Arrays.asList(csvParser.getLabels());
        return titles;
    }

    public static List queryFileAll(String fileName) throws IOException{
        String record = null;
        List records = new ArrayList<>();
        int count = GregorianCalendar.ALL_STYLES;

        BufferedReader bufferedReader = null;
        try{
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            while ((record = bufferedReader.readLine()) != null) {



                if (count > GregorianCalendar.ALL_STYLES && count <= CommonConstant.NUMBER_500) {
                    String[] value = record.split(",");
                    List valueList = new ArrayList<>();
                    for (String file : value) {
                        String values = " ";
                        if (hasText(file)) {
                            values = file;
                        }
                        valueList.add(values);
                    }
                    records.add(valueList.toArray(new String[]{}));
                }
            }
            bufferedReader.close();
            return records;
        } catch (IOException e) {
            log.error("读取文件数据异常,fileName:{},error:{}", fileName, e.getMessage());
            throw new IOException("获取csv文件数据异常!");
        }finally {
            bufferedReader.close();
        }


    }




}

你可能感兴趣的:(java,spring,boot)