package cn.microvideo.util.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import sun.net.TelnetInputStream; import cn.microvideo.util.PropertyReader; public class FTPTools { private String ftp_server;//server private String ftp_user;//user private String ftp_password;//pwd private String projectDir;//upload path private PropertyReader pr;//read config public FTPTools(){ pr = new PropertyReader("fileupload.properties"); ftp_server = pr.getProperty("ftp_server"); ftp_user =pr.getProperty("ftp_user"); ftp_password =pr.getProperty("ftp_password"); projectDir = pr.getProperty("projectDir"); } /** * 上傳 * @param localPath 文件的本地路徑 */ public void upload(String localPath){ FTPTools t = new FTPTools(); try { boolean flag=t.connect(projectDir, ftp_server, 21, ftp_user, ftp_password); if (flag) { File file=new File(localPath); t.upload(file); } } catch (Exception e) { e.printStackTrace(); } } /** * 下載 * @param remotePath 文件所在的FTP絕對(duì)路徑 */ public InputStream download(String remotePath){ FTPTools t = new FTPTools(); InputStream is=null; try { boolean flag=t.connect(projectDir, ftp_server, 21, ftp_user, ftp_password); if (flag) { File file=new File(remotePath); is=t.download(file); } } catch (Exception e) { e.printStackTrace(); } return is; } //------------ private FTPClient ftp; /** * * @param path * 上傳到ftp服務(wù)器哪個(gè)路徑下 * @param addr * 地址 * @param port * 端口號(hào) * @param username * 用戶名 * @param password * 密碼 * @return * @throws Exception */ private boolean connect(String path, String addr, int port, String username, String password) throws Exception { boolean result = false; ftp = new FTPClient(); int reply; ftp.connect(addr, port); ftp.login(username, password); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(path); result = true; return result; } //------------- /** * 上傳的文件或文件夾 * @param file * @throws Exception */ private void upload(File file) throws Exception { if (file.isDirectory()) { ftp.makeDirectory(file.getName()); ftp.changeWorkingDirectory(file.getName()); String[] files = file.list(); for (int i = 0; i < files.length; i++) { File file1 = new File(file.getPath() + "\\" + files[i]); if (file1.isDirectory()) { upload(file1); ftp.changeToParentDirectory(); } else { File file2 = new File(file.getPath() + "\\" + files[i]); FileInputStream input = new FileInputStream(file2); ftp.storeFile(file2.getName(), input); input.close(); } } } else { File file2 = new File(file.getPath()); FileInputStream input = new FileInputStream(file2); ftp.storeFile(file2.getName(), input); input.close(); } } /** * 下載文件 * @param file * @throws IOException */ private InputStream download(File file) { InputStream is=null; try { is=ftp.retrieveFileStream(file.getName()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return is; } } |
|