当前位置:   article > 正文

XXXXXXXXXXXXXXXXXXXXX

XXXXXXXXXXXXXXXXXXXXX

public class FTPUtil {
 public static Log logger = LogFactory.getLog(FTPUtil.class);

 private static String userName; // FTP ��¼�û���
 private static String password; // FTP ��¼����
 private static String ip; // FTP �������ַIP��ַ
 private static int port; // FTP �˿�
 private static Properties configFile = null; // �����ļ���·����
 private static String httpPayh = "http://192.168.1.10:9990";
 
 private static final String servicePath= "21060001/0001";
 // FTP״̬��
 public static int i = 1;

 static {
  InputStream in = ServiceFactory.class.getClassLoader()
    .getResourceAsStream("ftpconfig.properties");
  configFile = new Properties();
  try {
   configFile.load(in);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  userName = configFile.getProperty("username");
  password = configFile.getProperty("password");
  ip = configFile.getProperty("ip");
  port = Integer.valueOf(String.valueOf(configFile.getProperty("port")));

 }

 public FTPClient connectServer() {
  FTPClient ftpClient = null; // FTP �ͻ��˴���
  if (ftpClient == null) {
   int reply;
   try {
    ftpClient = new FTPClient();
    ftpClient.setControlEncoding("GBK");
    ftpClient.setDefaultPort(port);
    ftpClient.configure(getFtpConfig());
    ftpClient.connect(ip);
    ftpClient.login(userName, password);
    ftpClient.setDefaultPort(port);
    /*
     * ftpClient.connect("192.168.1.10");
     * ftpClient.login("testuser", "test");
     */
    // System.out.print(ftpClient.getReplyString());
    reply = ftpClient.getReplyCode();
    ftpClient.setDataTimeout(120000);

    if (!FTPReply.isPositiveCompletion(reply)) {
     ftpClient.disconnect();
     System.err.println("FTP server refused connection.");
     // logger.debug("FTP ����ܾ�l�ӣ�");

    }
    // System.out.println(i);
    i++;
   } catch (SocketException e) {
    e.printStackTrace();
      } catch (IOException e) {
    e.printStackTrace();
      }
  }
  return ftpClient;
 }

 private static FTPClientConfig getFtpConfig() {
  FTPClientConfig ftpConfig = new FTPClientConfig(
    FTPClientConfig.SYST_UNIX);
  ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
  return ftpConfig;
 }

 public String uploadFile(File localFile) {
  
  boolean flag = true;
  String _httpPath = null;
  try { 
   //connectServer();
   FTPClient fc = connectServer();
   fc.setFileType(FTP.BINARY_FILE_TYPE);
   fc.enterLocalPassiveMode();
   fc.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
   InputStream input = new FileInputStream(localFile);
   // �����ļ���
   //System.out.println(ftpClient.printWorkingDirectory());
   //ftpClient.makeDirectory("new");
   // System.out.println("x>>>>>>> : " + objFolder);
   // �л���new
   //ftpClient.changeWorkingDirectory("//");
   fc.changeWorkingDirectory(servicePath);
   String yyMMdd = new SimpleDateFormat("yyMMdd").format(new Date());
   
   if(fc.changeWorkingDirectory(yyMMdd)){
    String hhmmss = new SimpleDateFormat("hhmmss").format(new Date());
    fc.makeDirectory(hhmmss);
    fc.changeWorkingDirectory(hhmmss);
    _httpPath = fc.printWorkingDirectory();
   }else{
    fc.makeDirectory(yyMMdd);
    fc.changeWorkingDirectory(yyMMdd);
    String hhmmss = new SimpleDateFormat("hhmmss").format(new Date());
    fc.makeDirectory(hhmmss);
    fc.changeWorkingDirectory(hhmmss);
    _httpPath = fc.printWorkingDirectory();
   }
   
   /*System.out.println("b>>>>>>> : " + localFile.getPath() + " "
     + ftpClient.printWorkingDirectory());*/
   flag = fc.storeFile(localFile.getName(), input);
   if (flag) {
    System.out.println("�ϴ��ļ��ɹ���");
   } else {
    System.out.println("�ϴ��ļ�ʧ�ܣ�");
   }
   input.close();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  return httpPayh+_httpPath.substring(_httpPath.indexOf("/", 1))+"/"+localFile.getName();
 }

 

}

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/71871
推荐阅读
相关标签
  

闽ICP备14008679号