Commit 872c1927 authored by 刘文胜's avatar 刘文胜

ftp 工具类异常处理

parent 8b45c19a
...@@ -106,12 +106,9 @@ public class FtpUtil { ...@@ -106,12 +106,9 @@ public class FtpUtil {
System.err.println("FTP server refused connection."); System.err.println("FTP server refused connection.");
System.exit(1); System.exit(1);
} }
} catch (SocketException e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
logger.error(e.toString()); logger.error(e.toString());
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
} }
return ftpClient; return ftpClient;
} }
...@@ -121,8 +118,9 @@ public class FtpUtil { ...@@ -121,8 +118,9 @@ public class FtpUtil {
*/ */
public List<String[]> readCSVFile(String fileName) { public List<String[]> readCSVFile(String fileName) {
List<String[]> list = new ArrayList<String[]>(); List<String[]> list = new ArrayList<String[]>();
FTPClient ftpClient = null;
try { try {
FTPClient ftpClient = this.getFTPClient(); ftpClient = this.getFTPClient();
InputStream csv = ftpClient.retrieveFileStream(getFileDir InputStream csv = ftpClient.retrieveFileStream(getFileDir
+ fileName); + fileName);
if (csv == null) { if (csv == null) {
...@@ -135,11 +133,16 @@ public class FtpUtil { ...@@ -135,11 +133,16 @@ public class FtpUtil {
CSVReader csvReader = new CSVReader(inputStreamReader); CSVReader csvReader = new CSVReader(inputStreamReader);
list = csvReader.readAll(); list = csvReader.readAll();
csvReader.close(); csvReader.close();
ftpClient.disconnect(); } catch (Throwable e) {
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally{
if(ftpClient!=null){
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
return list; return list;
} }
...@@ -148,8 +151,9 @@ public class FtpUtil { ...@@ -148,8 +151,9 @@ public class FtpUtil {
* 移动文件 * 移动文件
*/ */
public void moveFile(String fileName) { public void moveFile(String fileName) {
FTPClient ftpClient = null;
try { try {
FTPClient ftpClient = this.getFTPClient(); ftpClient = this.getFTPClient();
// 新的文件名 // 新的文件名
String suffix = fileName.substring(fileName.lastIndexOf(".")); String suffix = fileName.substring(fileName.lastIndexOf("."));
...@@ -159,9 +163,16 @@ public class FtpUtil { ...@@ -159,9 +163,16 @@ public class FtpUtil {
ftpClient.rename(getFileDir + fileName, getFileHistoryDir ftpClient.rename(getFileDir + fileName, getFileHistoryDir
+ newFileName); + newFileName);
ftpClient.disconnect(); } catch (Throwable e) {
} catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}finally{
if(ftpClient!=null){
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
} }
...@@ -183,7 +194,7 @@ public class FtpUtil { ...@@ -183,7 +194,7 @@ public class FtpUtil {
CSVWriter.NO_QUOTE_CHARACTER); CSVWriter.NO_QUOTE_CHARACTER);
} }
} catch (IOException e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
} }
return writer; return writer;
...@@ -204,7 +215,7 @@ public class FtpUtil { ...@@ -204,7 +215,7 @@ public class FtpUtil {
ftpClient.storeFile(putAddFileName, addInputStream); ftpClient.storeFile(putAddFileName, addInputStream);
ftpClient.storeFile(putUpdateFileName, updateInputStream); ftpClient.storeFile(putUpdateFileName, updateInputStream);
} catch (IOException e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();
logger.error(e.toString()); logger.error(e.toString());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment