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();
 }

 

}

 

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值