设计思路:
1、先实现文件上传到服务器,获取到文件名和文件保存的路径
2、获取当前项目运行的路径,读取服务器中的文件
3、解析文件数据
4、数据处理,导入数据库
文件部分上传代码:
if (!file.isEmpty()) {
FileTb upLoadFile = null;
String originalName = file.getOriginalFilename();
int dotPosition = originalName.indexOf(".");
String fileType = null;
String filename;
if (dotPosition != -1) {
fileType = originalName.substring(
originalName.indexOf(".") + 1, originalName.length());
filename = originalName;
} else {
filename = DateUtils.currentDate();
}
File savedFile = new File(path, filename);
try {
FileUtils.copyInputStreamToFile(file.getInputStream(),
savedFile);
} catch (IOException e) {
e.printStackTrace();
excel文件上传(包括一些特定的处理方式POI常用模式:适合小数据量):
Workbook wookbook = getSheets(path);
//获取sheet的总数
for (int i = 0; i < wookbook.getNumberOfSheets(); i++) {
Date startTime = new Date();
//得到一个工作表
Sheet sheet = wookbook.getSheetAt(i);
//1.创建一个存储导入数据的列表:importList:List
List importList = new ArrayList<>();
//2.将excel中为表头保存为key-value的集合:excelFieldMap
Map
/**
private Workbook getSheets(String filePath) {
FileInputStream fis = null;
Workbook wookbook = null;
try {
//获取一个绝对地址的流
fis = new FileInputStream(filePath);
} catch (Exception e) {
e.printStackTrace();
}
if (filePath.endsWith(".xls")) {
/*2003版本的excel,用.xls结尾:得到工作簿*/
try {
wookbook = new HSSFWorkbook(fis);//
} catch (IOException e) {
e.printStackTrace();
}
} else if (filePath.endsWith(".xlsx")) {
try {
/*2007版本的excel,用.xlsx结尾 :工作薄*/
wookbook = new XSSFWorkbook(fis);
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
}
} else {
throw new CustomException("文件不是excel类型");
}
return wookbook;
}
csv 文件导入:
/* 读取文件内容*/
try {
// 创建CSV读对象 例如:CsvReader(文件路径,分隔符,编码格式);
CsvReader reader = new CsvReader(savePath, ',', Charset.forName("UTF-8"));
// 跳过表头 如果需要表头的话,这句可以忽略
reader.readHeaders();
/**
* 获取关系表中的字段名
*/
List tableFieldsList = commenUtilMapper.findAllPropsName('"' + tableName + '"');
// 逐行读入除表头的数据
while (reader.readRecord()) {
String excelfieldValue = reader.get(excelfield);
}
reader.close();
txt数据文件导入:
RandomAccessFile raf = new RandomAccessFile(path, “rw”);
while ((line_record = raf.readLine()) != null) {
line_record = new String(line_record.getBytes("ISO-8859-1"), "utf8");
excel 2007:快速读取数据(POI 用户模式:适合大数据量)
package com.swad.asura.common;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.util.SAXHelper;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class ExcelAnalyseUtils {
public static void main(String[] args) {
Date da = new Date();
List
用StreamingReader读取
public static Map>> readExcelByStreamingReader(String filePath, int num) {
Workbook wk = null;
Map>> metaInfo = null;
try {
wk = StreamingReader.builder().rowCacheSize(100) // 缓存到内存中的行数,默认是10
.bufferSize(4096) // 读取资源时,缓存到内存的字节大小,默认是1024
.open(new FileInputStream(filePath)); // 打开资源,必须,可以是InputStream或者是File,注意:只能打开XLSX格式的文件
// wk = StreamingReader.builder()
// .sstCacheSize(100)
// .open(new FileInputStream(filePath)); // 打开资源,必须,可以是InputStream或者是File,注意:只能打开XLSX格式的文件
metaInfo = new LinkedHashMap<>();
for (int i = 0; i < wk.getNumberOfSheets(); i++) {
Sheet sheet = wk.getSheetAt(i);
List> sample = new LinkedList<>();
for (Row row : sheet) {
// 遍历所有的列
List rowData = new LinkedList<>();
for (Cell cell : row) {
rowData.add(cell.getStringCellValue() + "");
}
if (num>=0 && sample.size() < num) {
sample.add(rowData);
break;
}
}
metaInfo.put(sheet.getSheetName(), sample);
}
} catch (FileNotFoundException e) {
throw new AsuraRuntimeException(e);
} finally {
try {
if (null != wk) {
wk.close();
}
} catch (IOException e) {
wk = null;
}
}
return metaInfo;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
/**
* @author liaowei
* @create 2018-05-03 16:46
* @desc 将数据load进mysql
* sql.csv:可不用
**/
public class LoadDataToMysqlUtils {
/**
* 获取字段列表及字段流
*
* @param datas
* @return String testSql = "LOAD DATA LOCAL INFILE '12.csv' IGNORE INTO TABLE
* test.test (a,b,c,d,e,f)";
*/
public static LoadDataToMysqlFieldsVo getDataInputStream(HttpServletRequest request,
List> datas, Map MysqlToExcelProp, String userName, String caseId) {
LoadDataToMysqlFieldsVo loadDataToMysqlFieldsVo = new LoadDataToMysqlFieldsVo();
String sql = "LOAD DATA LOCAL INFILE 'sql.csv' IGNORE INTO TABLE " + 表名 + " (";
StringBuilder builder = new StringBuilder();
String midSql = "";
request.getSession().setAttribute("TotalNumberOfData", datas.size());
for (int i = 0; i < datas.size(); i++) {
for (Map.Entry mapData : MysqlToExcelProp.entrySet()) {
if (i == 0) {
if (StringUtils.isEmpty(midSql)) {
midSql = mapData.getValue();
} else {
midSql += "," + mapData.getValue();
}
}
if ("member_register_date".equalsIgnoreCase(mapData.getValue())) {
if (StringUtils.isNotEmpty(datas.get(i).get(mapData.getKey()))) {
List convetTime = DateUtils.ArrayDateForm(datas.get(i).get(mapData.getKey()));
String date = convetTime.get(0) + "-" + convetTime.get(1) + "-" + convetTime.get(2) + " "
+ convetTime.get(3) + ":" + convetTime.get(4) + ":" + convetTime.get(5);
builder.append(date);
} else {
builder.append("");
}
} else {
if (StringUtils.isNotEmpty(datas.get(i).get(mapData.getKey()))) {
builder.append(datas.get(i).get(mapData.getKey()));
} else {
builder.append("");
}
}
builder.append("\t");
}
builder.append(caseId);
builder.append("\t");
builder.append(AsuraTimeUtils.currentDate_Time());
builder.append("\t");
builder.append(userName);
builder.append("\t");
builder.append(AsuraTimeUtils.currentDate_Time());
builder.append("\t");
builder.append(userName);
builder.append("\t");
builder.append("\n");
request.getSession().setAttribute("LoadedData", (long) (i + 1));
}
sql += midSql + ",case_id,create_date,create_account,last_modify_time,last_modify_account)";
byte[] bytes = builder.toString().getBytes();
InputStream is = new ByteArrayInputStream(bytes);
loadDataToMysqlFieldsVo.setSql(sql);
loadDataToMysqlFieldsVo.setDataStream(is);
return loadDataToMysqlFieldsVo;
}
/**
* 保存到数据库中
*
* @param loadDataToMysqlFieldsVo
*/
public static void loadDataIntoMysql(LoadDataToMysqlFieldsVo loadDataToMysqlFieldsVo) {
Connection conn = null;
try {
System.out.println(loadDataToMysqlFieldsVo.getSql());
conn = JdbcConnUtils.getConn();
PreparedStatement statement = conn.prepareStatement(loadDataToMysqlFieldsVo.getSql());
if (statement.isWrapperFor(com.mysql.jdbc.Statement.class)) {
com.mysql.jdbc.PreparedStatement mysqlStatement = statement
.unwrap(com.mysql.jdbc.PreparedStatement.class);
mysqlStatement.setLocalInfileInputStream(loadDataToMysqlFieldsVo.getDataStream());
mysqlStatement.executeUpdate();
}
} catch (Exception e) {
throw new RuntimeException("操作数据库错误!" + e);
} finally {
try {
if (null != conn) {
conn.close();
} else {
}
} catch (SQLException e) {
conn = null;
}
}
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.springframework.util.ResourceUtils;
/**
* @author liaowei
* @create 2017-11-06 10:36
* @desc 原始连接数据库方式
*/
public class JdbcConnUtils {
private static Connection conn = null;
private static Properties props = null;
static {
loadProps();
}
synchronized static private void loadProps() {
props = new Properties();
InputStream in = null;
try {
// 第一种 通过类加载器进行获取properties文件流
in = ResourceUtils.class.getClassLoader().getResourceAsStream("project_config.properties");
props.load(in);
} catch (FileNotFoundException e) {
throw new RuntimeException("未找到需要读取的文件!");
} catch (IOException e) {
throw new RuntimeException("IO异常!");
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
throw new RuntimeException("数据流关闭失败!");
}
}
}
public static String getProperty(String key) {
if (null == props || props.size() <= 0) {
loadProps();
}
return props.getProperty(key);
}
public static Connection getConn() {
try {
Class.forName(getProperty("asura.db.driver"));
conn = DriverManager.getConnection(props.getProperty("asura.db.url"),
props.getProperty("asura.db.username"), props.getProperty("asura.db.pwd"));
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException("加载驱动失败");
} catch (SQLException e) {
throw new RuntimeException("获取连接失败");
}
return conn;
}
public void closeConn() {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(JdbcConnUtils.getProperty("asura.db.pwd"));
}
}