import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
public class SagaFTP { FTPClient ftpClient ; public boolean connectServer(String server, int port, String user,String password) throws SocketException, IOException { ftpClient = new FTPClient(); ftpClient.setControlEncoding("GBK"); ftpClient.connect(server, port == 0 ? 21: port); System.out.println("Ftp Connected to " + server + "."); boolean login = ftpClient.login(user, password); return login ; } public boolean uploadFile(String fileName, String newName, String path) throws IOException { if (path.length() != 0) { ftpClient.makeDirectory(path); ftpClient.changeWorkingDirectory(path); ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); // 这个很重要,否则会出现附件上传后文件损毁打不开的问题! } boolean flag = false; InputStream iStream = null; try { iStream = new FileInputStream(fileName); flag = ftpClient.storeFile(newName, iStream); System.out.println("File upload is successful!"); } catch (IOException e) { flag = false; return flag; } finally { try { if (iStream != null) { iStream.close(); } } catch (IOException e) { e.printStackTrace(); }finally{ ftpClient.logout(); ftpClient.disconnect(); } } return flag; } public void downLoadFile(String ftpPath, String fileName , OutputStream os){ try { ftpClient.setControlEncoding("GBK"); ftpClient.changeWorkingDirectory(ftpPath); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); ftpClient.retrieveFile(fileName, os) ; } catch (IOException e) { e.printStackTrace(); }finally{ try { os.close() ; } catch (IOException e) { e.printStackTrace(); }finally{ try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } public String readFile(String ftpPath, String fileName) { StringBuffer resultBuffer = new StringBuffer(); FileInputStream inFile = null; InputStream in = null; try { ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); ftpClient.changeWorkingDirectory(ftpPath); in = ftpClient.retrieveFileStream(fileName); } catch (FileNotFoundException e) { e.printStackTrace(); return "下载配置文件失败,请联系管理员."; } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); e.printStackTrace(); return "配置文件读取失败,请联系管理员."; } if (in != null) { BufferedReader br = new BufferedReader(new InputStreamReader(in)); String data = null; try { while ((data = br.readLine()) != null) { resultBuffer.append(data + "\n"); } br.close() ; } catch (IOException e) { e.printStackTrace(); return "配置文件读取失败,请联系管理员."; }finally{ try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } try { in.close() ; } catch (IOException e) { e.printStackTrace(); } }else{ return "配置文件读取失败,请联系管理员."; }
return resultBuffer.toString(); }
}
|