标签:
1.确定ftp文件夹
1 /** FTP文件夹 **/ 2 private String ftpPath = "hshscrenncap" + "/" + DateUtils.convertDateToShortString(new Date());
2.连接服务器
1 public static FTPClient ftpClient; 2 3 // server:服务器名字 4 // user:用户名 5 // password:密码 6 // path:服务器上的路径 7 public static boolean connectServer(String ip, String user, 8 String password) { 9 if(ip!=null){ 10 ip = ip.trim(); 11 } 12 if(user!=null){ 13 user = user.trim(); 14 } 15 if(password!=null){ 16 password = password.trim(); 17 } 18 boolean is_connected; 19 try { 20 ftpClient = new FTPClient(); 21 //连接服务器 22 try { 23 ftpClient.connect(ip); 24 } catch (UnknownHostException ex) { 25 throw new IOException("不能找到FTP服务:" + ip + "‘"); 26 } 27 //在连接尝试检查响应. 28 int reply = ftpClient.getReplyCode(); 29 if (!FTPReply.isPositiveCompletion(reply)) { 30 disconnect(); 31 throw new IOException("不能连接到服务‘:" + ip + "‘"); 32 } 33 //登录. 34 if (!ftpClient.login(user, password)) { 35 is_connected = false; 36 disconnect(); 37 throw new IOException("不能登录到FTP服务:‘" + ip + "‘"); 38 } else { 39 is_connected = true; 40 } 41 Log.i(Constants.LOG_TAG, "ftp连接成功 result=" + is_connected); 42 } catch (IOException e) { 43 Log.i(Constants.LOG_TAG, "连接ftp服务器出错:" + e.getMessage()); 44 is_connected = false; 45 return is_connected; 46 } 47 return is_connected; 48 }
3.判断ftp路径是否存在,没有则创建;
1 /** 2 * 检查文件夹是否存在 3 * 4 * @param dir 5 * @param ftpClient 6 * @return 7 */ 8 private static Boolean isDirExist(String dir, FTPClient ftpClient) { 9 try { 10 return ftpClient.changeWorkingDirectory(dir); 11 } catch (Exception e) { 12 e.printStackTrace(); 13 return false; 14 } 15 }
1 /** 2 * 创建ftp文件 3 */ 4 public static boolean createFtpFile(String ip, 5 String userName, String userPassword, String path) { 6 7 if (null != path) { 8 path = path.trim(); 9 } 10 11 boolean result = connectServer(ip, userName, userPassword); 12 if (!result) { 13 return result; 14 } 15 try { 16 ftpClient.enterLocalPassiveMode(); 17 path = new String(path.getBytes("GBK"), "ISO-8859-1"); 18 //通过远程命令 创建一个文件夹 19 if(isDirExist(path, ftpClient)){ 20 return true; 21 } 22 boolean flag = ftpClient.makeDirectory(path); 23 if (flag) { 24 Log.i(Constants.LOG_TAG, "创建文件 " + path + " 成功!"); 25 } else { 26 Log.i(Constants.LOG_TAG, "创建ftp文件失败," + path + " 文件不存在!"); 27 } 28 return flag; 29 } catch (UnsupportedEncodingException e) { 30 Log.i(Constants.LOG_TAG, "创建ftp文件编码出错:" + e.getMessage()); 31 return false; 32 } catch (IOException e) { 33 Log.i(Constants.LOG_TAG, "创建ftp文件出错出错:" + e.getMessage()); 34 return false; 35 } finally { 36 if (null != ftpClient && ftpClient.isConnected()) { 37 try { 38 ftpClient.disconnect(); 39 } catch (Exception e) { 40 Log.i(Constants.LOG_TAG, "关闭ftp连接出错:" + e.getMessage()); 41 } 42 } 43 } 44 }
上传图片文件:
/** * localFile 本地存储文件夹路径 * ip * userName * userPassword * path ftp文件夹路径 **/ public static String uploadFileByApacheByBinary(String localFile, String ip, String userName, String userPassword, String path) { String resultMsg = ""; if (null != ip) { path = path.trim(); } //检查是否存在目录 createFtpFile(Constants.WS_FTP_IP , Constants.WS_FTP_ACCOUNTNAME , Constants.WS_FTP_ACCOUNTPASSWORD, path); boolean result = connectServer(ip, userName, userPassword); if (!result) { resultMsg = Constants.HTTP_INTERVAL_ERROR_STATUS; return resultMsg; } else { ftpClient.setBufferSize(1024); //每次数据连接之前,FTP client告诉FTP server开通一个端口来传输数据。 // Use passive mode to pass firewalls. ftpClient.enterLocalPassiveMode(); FileInputStream fis = null; try { File file = new File(localFile); if(!file.exists()){ resultMsg = Constants.HTTP_ILLEGAL_ARGUMENT_STATUS; return resultMsg; } fis = new FileInputStream(file); String fileName = file.getName(); ftpClient.changeWorkingDirectory(path); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); Log.i(Constants.LOG_TAG, "upload--------begin"); boolean flag = ftpClient.storeFile(new String(fileName.getBytes("GBK"), "iso-8859-1"), fis); if (flag) { Log.i(Constants.LOG_TAG, "ftp上传成功!"); resultMsg = Constants.HTTP_SUCCESS_STATUS; } else { Log.i(Constants.LOG_TAG, "ftp上传失败!"); resultMsg = Constants.HTTP_ERROR_STATUS; } fis.close(); return resultMsg; }catch (Exception e) { Log.i(Constants.LOG_TAG, "上传ftp文件时出错:" + e.getMessage()); resultMsg = Constants.HTTP_INTERVAL_ERROR_STATUS; return resultMsg; } finally { if (null != ftpClient && ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (Exception e) { Log.i(Constants.LOG_TAG, "关闭ftp连接出错:" + e.getMessage()); } } } } }
标签:
原文地址:http://www.cnblogs.com/CharlesGrant/p/4747006.html