JAVA WEB 파일 데이터 가 져 오기 (CSV, TXT, 엑셀 (큰 파일 읽 기)
18533 단어 JAVA
파일 부분 업로드 코드:
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();
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;
}
/* */
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();
RandomAccessFile raf = new RandomAccessFile(path, “rw”);
while ((line_record = raf.readLine()) != null) {
line_record = new String(line_record.getBytes("ISO-8859-1"), "utf8");
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("
");
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"));
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA 객체 작성 및 제거 방법정적 공장 방법 정적 공장 방법의 장점 를 반환할 수 있습니다. 정적 공장 방법의 단점 류 공유되거나 보호된 구조기를 포함하지 않으면 이불류화할 수 없음 여러 개의 구조기 파라미터를 만났을 때 구축기를 고려해야 한다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.