Ftp导出xlsx文件 解析 插入 数据库,这样写 性能如何

趁早 发布于 2016/03/25 10:03
阅读 216
收藏 0

public class test {


/***
* 1 读取文件 到本地 2 本地解析xsml文件 3 保存到数据库

* @param args
*/
public static void main(String[] args) {


// String filePath = "E://files.xlsx"; // 文件地址
String fileName = "Coupons_20151126.xlsx";
String fileGet = "E://Coupons_20151126.xlsx";
// String filePath2 = "D://file.xls"; // 文件地址
try {
 
System.out.println(test.downFile("", 21,"", "", "/test2/", fileName, "E:/"));
System.out.println("/////////////////////////////////////");
List<ElsxInfo> list = test.getXml(fileGet);
for (ElsxInfo s : list) {
System.out.println(s.getCarPrice() + " " + s.getFoodPrice()
+ "   " + s.getInfo() + "  " + s.getName() + "  "
+ s.getTime() + "  " + s.getTimeDay());
}
} catch (Exception e) {
e.printStackTrace();
}


}


// 验证 字符串的 method
public static Boolean isNull(String str) {
if (str != null && !str.equals("null")) {
return true;
} else {
return false;
}
}


/****
* 获取单属性

* @param path
* @throws Exception
*/
public static List<ElsxInfo> getXml(String path) throws Exception {
List<ElsxInfo> list = new ArrayList<ElsxInfo>();
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(path);
// 循环工作表Sheet
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
// 循环行Row
for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow row = xssfSheet.getRow(rowNum + 1);
ElsxInfo be = new ElsxInfo();


if (row == null) {
break;
}
// 循环列Cell
for (int cellNum = 0; cellNum <= row.getLastCellNum(); cellNum++) {
XSSFCell xssfCell = row.getCell(cellNum);
// System.out.println(xssfCell);
if (xssfCell == null) {
continue;
} else {
switch (cellNum) {
case 0:
if (isNull(xssfCell.toString())) {
be.setName(xssfCell.toString());
}


break;
case 1:
if (isNull(xssfCell.toString())) {
be.setTime(xssfCell.toString());
}


break;
case 2:
if (isNull(xssfCell.toString())) {
be.setCarPrice(xssfCell.toString());
}


break;
case 3:
if (isNull(xssfCell.toString())) {
be.setInfo(xssfCell.toString());
}


break;
case 4:
if (isNull(xssfCell.toString())) {
be.setTimeDay(xssfCell.toString());
}


break;
case 5:
if (isNull(xssfCell.toString())) {
be.setFoodPrice(xssfCell.toString());
}
break;
default:
break;
}
}
}
if (be != null) {
list.add(be);
}
}
}
return list;
}


/**
* 读取文件 下载到 本地

* @param url
*            FTP服务器hostname
* @param port
*            FTP服务器端口
* @param username
*            FTP登录账号
* @param password
*            FTP登录密码
* @param remotePath
*            FTP服务器上的相对路径
* @param fileName
*            要下载的文件名
* @param localPath
*            下载后保存到本地的路径
* @return
*/
public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());


OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
 
}

加载中
返回顶部
顶部