JAVA -- 生成CSV并上传到 FTP 不生成本地文件

直接上代码

POM.XML 这里只是主要的jar


	net.sf.supercsv
	super-csv
	2.4.0
/**
     * 导出csv文件
     *
     * @param fileName 要导出的文件名(包含路径)
     * @param content
     * 内容
     * @throws IOException
     */
    public boolean exportToCsv(String fileName, List content){
        log.info("转化csv开始:fileName: " + fileName + ",data个数:" + content.size());
        if (!fileName.endsWith(".csv")) {
            log.error("fileName有误,需要以.csv结尾!");
            return false;
        }
        boolean result = false;
        ICsvListWriter writer = null;
        InputStream in = null;
        File file = null;
        try {
            file = new File(fileName);
            String[] header = getHeaderFromTemplate();
            log.info("从模版中获取head个数:" + header.length);
            writer = new CsvListWriter(new FileWriter(file), CsvPreference.EXCEL_PREFERENCE);
            if (header.length > 0){
                writer.writeHeader(header);
            }
            for (String[] array : content) {
                for (int i=0;i headerList = new ArrayList<>();
        Sheet sheet = null;
        Row row = null;
        Row rowHeader = null;
        Workbook wb = null;

        InputStream is = null;
        try {
            is = ExcelExportUtil.class.getResourceAsStream("/template/policy_Template.xlsx");
            wb = new XSSFWorkbook(is);
            // 用来存放表中数据
            // 获取第一个sheet
            sheet = wb.getSheetAt(0);
            // 获取第一行
            rowHeader = sheet.getRow(0);
            row = sheet.getRow(0);
            // 获取最大列数
            int colnum = row.getPhysicalNumberOfCells();

            for (int j = 0; j < colnum; j++) {
                headerList.add(rowHeader.getCell(j).getStringCellValue());
            }

        } catch (FileNotFoundException e) {
            log.error("/template/policy_Template.xlsx,模版文件不存在!");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(wb);
        }
        return headerList.toArray(new String[headerList.size()]);
    }

/***
     * 判断字符串是否为数字(包含小数点)
     * @param str
     * @return
     */
    private static boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]+\\.*[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }

关于上传FTP代码 可看上一篇文章JAVA -- 生成Excel 并上传到 FTP 不生成本地文件

public void uploadFile(InputStream inputStream,String fileName) {

		BufferedInputStream inBuf = null;

		String ftpPath = "/export_file_path/";
		// 数据库存入地址
		try {
			inBuf = new BufferedInputStream(inputStream);
			// 开始上传文件
			FileFtp ftp = new FileFtp();
			ftp.login(ftpIp, ftpUser, ftpPassWord);
			ftp.CreateDirecroty(ftpPath);
			ftp.uploadFile(inBuf, fileName);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null)
					inputStream.close();
				if (inBuf != null)
					inBuf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 

你可能感兴趣的:(web,java,开发常用)