欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)將文件上傳到ftp服務(wù)器的方法

 更新時(shí)間:2016年08月23日 09:38:47   作者:xiangqian0505  
這篇文章主要介紹了java實(shí)現(xiàn)將文件上傳到ftp服務(wù)器的方法,結(jié)合實(shí)例形式分析了基于java實(shí)現(xiàn)的ftp文件傳輸類(lèi)定義與使用方法,需要的朋友可以參考下

本文實(shí)例講述了java實(shí)現(xiàn)將文件上傳到ftp服務(wù)器的方法。分享給大家供大家參考,具體如下:

工具類(lèi):

package com.fz.common.util;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FileUtil {
 /**
 *
 * @date Sep 26, 2011 10:17:39 AM
 * @return
 * @author zhangh
 */
 public static DataInputStream getInput(){
 DataInputStream d = null;
 try {
  d = new DataInputStream(new FileInputStream("c:/wmc.dat"));
  return d;
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 return d;
 }
 /**
 *
 * @date Sep 26, 2011 10:17:44 AM
 * @param whites
 * @return
 * @author zhangh
 */
 public static boolean creatWhiteManageFile(byte[] whites,String file) {
 DataOutputStream d;
 try {
  d = new DataOutputStream(new FileOutputStream(file));
  d.write(whites);
  d.flush();
 } catch (Exception e) {
  // TODO Auto-generated catch block
  return false;
//  e.printStackTrace();
 }
 return true;
 }
 /**
 *
 * @date Sep 16, 2011 4:39:22 PM
 * @param url
 * @param username
 * @param password
 * @param path
 * @param filename
 * @param input
 * @return
 * @author zhangh
 */
 public static boolean uploadFile(String url, String username,
  String password, String path, String filename, InputStream input) {
 boolean success = false;
 FTPClient ftp = new FTPClient();
 try {
  int reply;
  ftp.connect(url);
//  ftp.connect(url, port);// 連接FTP服務(wù)器
  // 如果采用默認(rèn)端口,可以使用ftp.connect(url)的方式直接連接FTP服務(wù)器
  ftp.login(username, password);// 登錄
  reply = ftp.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
  ftp.disconnect();
  return success;
  }
  ftp.changeWorkingDirectory(path);
  ftp.storeFile(filename, input);
  ftp.logout();
  input.close();
  success = true;
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  if (ftp.isConnected()) {
  try {
   ftp.disconnect();
  } catch (IOException ioe) {
  }
  }
 }
 return success;
 }
 /**
 *
 * 方法名稱(chēng):uploadFileFtp
 * 方法描述:黑名名單,黑用戶(hù)文件上傳ftp服務(wù)器
 * @param url
 * @param username
 * @param password
 * @param path
 * @param filename
 * @param input
 * @param input2
 * @return
 * boolean
 * version 1.0
 * author wuxq
 * Oct 26, 2011 3:19:09 PM
 */
 public static boolean uploadFileFtp(String url, String username,
  String password, String path, String filename, InputStream input,
  InputStream input2) {
 Date date = new Date();
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String time = formatter.format(date);
 boolean success = false;
 FTPClient ftp = new FTPClient();
 try {
  int reply;
  ftp.connect(url);
  ftp.login(username, password);// 登錄
  reply = ftp.getReplyCode();
  if (!FTPReply.isPositiveCompletion(reply)) {
  ftp.disconnect();
  return success;
  }
  ftp.changeWorkingDirectory(path);
  ftp.storeFile(filename, input);
  ftp.storeFile(filename + time, input2);
  ftp.logout();
  input.close();
  success = true;
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  if (ftp.isConnected()) {
  try {
   ftp.disconnect();
  } catch (IOException ioe) {
  }
  }
 }
 return success;
 }
}

讀取配置文件:

package com.fz.fzbike.domain;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.eNets.framework.util.SysConstants;
/**
 * 獲取ftp服務(wù)器信息的bean類(lèi)
 *
 * @author wuxq
 *
 */
public class SysConstats {
 private static Logger log = Logger.getLogger(SysConstats.class);
 public static String FTPSERVER;// ftp服務(wù)器ip地址
 public static String FTPUSERNAME;// ftp服務(wù)器用戶(hù)名
 public static String FTPPASSWORD;// ftp服務(wù)器用戶(hù)密碼
 public static String ENVELOPERESULTROOT;// 存放ftp服務(wù)器的路徑
 public SysConstats() {
 try {
  InputStream in = new BufferedInputStream(new FileInputStream(
   SysConstants.PUBLIC_PATH.substring(0,
    SysConstants.PUBLIC_PATH.length() - 7)
    + "/bidfileconfig.properties"));
  Properties prop = new Properties();
  prop.load(in);
  SysConstats.FTPSERVER = prop.getProperty("ftpServer", "none");
  SysConstats.FTPUSERNAME = prop.getProperty("ftpUserName", "none");
  SysConstats.FTPPASSWORD = prop.getProperty("ftpPassword", "none");
  SysConstats.ENVELOPERESULTROOT = prop.getProperty(
   "envelopeResultRoot", "none");
  log.debug("讀取ftp配置信息成功!");
 } catch (IOException e) {
  log.debug("讀取ftp配置信息失??!");
  e.printStackTrace();
 }
 }
 public static String getFTPSERVER() {
 return FTPSERVER;
 }
 public static void setFTPSERVER(String ftpserver) {
 FTPSERVER = ftpserver;
 }
 public static String getFTPUSERNAME() {
 return FTPUSERNAME;
 }
 public static void setFTPUSERNAME(String ftpusername) {
 FTPUSERNAME = ftpusername;
 }
 public static String getFTPPASSWORD() {
 return FTPPASSWORD;
 }
 public static void setFTPPASSWORD(String ftppassword) {
 FTPPASSWORD = ftppassword;
 }
 public static String getENVELOPERESULTROOT() {
 return ENVELOPERESULTROOT;
 }
 public static void setENVELOPERESULTROOT(String enveloperesultroot) {
 ENVELOPERESULTROOT = enveloperesultroot;
 }
 public static void main(String args[]) {
 new SysConstats();
 }
}

將文件上傳ftp:

package com.fz.fzbike.biz;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import com.eNets.basesys.user.vo.UserVO;
import com.eNets.framework.assemble.RequestHashNew;
import com.eNets.framework.database.DBConnection;
import com.fz.common.util.FileUtil;
import com.fz.fzbike.common.StringUtil;
import com.fz.fzbike.domain.SysConstats;
/**
 * 上傳卡內(nèi)碼到ftp服務(wù)器 生成bat文件
 *
 * @author wuxq 2011-09-28
 */
public class UploadCardInNoFtpAction {
 /**
 *
 * 方法名稱(chēng):uploadFtp 方法描述:上傳文件到ftp
 *
 * @param reh
 *      void version 1.0 author wuxq Sep 28, 2011 10:38:38 AM
 */
 public void uploadFtp(RequestHashNew reh) {
 String cardType = reh.get("cardType").toString();
 DBConnection dbc = reh.getDBC();// 鏈接數(shù)據(jù)庫(kù)
 dbc.endTran();
 // 判斷是否是空值 空有可能是掛失,退出掛失, 退出黑名單, 根據(jù)卡id得到卡類(lèi)型
 if (StringUtil.isNull(cardType)) {
  String cardtypesql = "select ci.card_type from lc_t_card_info ci where ci.card_id="
   + reh.get("SELECTEDID");
  cardType = dbc.getList0(cardtypesql);
 }
 String top = "c:/upload/";
 String file = top + "bmc.dat"; // 定義一個(gè)目錄存放臨時(shí)的黑名單bat文件
 String whiteFile = top + "wmc.dat";// 定義一個(gè)目錄存放臨時(shí)的白名單bat文件
 String buserfile = top + "buser.dat"; // 定義一個(gè)目錄存放臨時(shí)的黑用戶(hù)文件
 String fileID = dbc.setOracleGlideValue("LC_T_UPGRADE_FILE");// 得到文件表的序列號(hào)
 // 得到當(dāng)前用戶(hù)的ID
 UserVO userVo = reh.getUserVO();
 String UserID = userVo.getUserID();
 DecimalFormat df = new DecimalFormat("0.0");
 if (cardType.equals("7")) {
  StringBuffer bf = new StringBuffer(1024);
  bf
   .append(
    "select distinct card_in_no from(select tc.card_in_no")
   .append(
    " from lc_t_blacklist tb left join lc_t_card_info tc")
   .append(
    " on tb.card_id = tc.card_id where tc.card_type = 7")
   .append(" and tb.whether_effective = 1 union all select")
   .append(" tc.card_in_no from lc_t_card_loss cl left join")
   .append(
    " lc_t_card_info tc on cl.card_id=tc.card_id where tc.card_type = 7 and")
   .append(" cl.whether_effective=1) t order by t.card_in_no");// 黑名單及掛失記錄表中所有的管理員記錄
  StringBuffer bffer = new StringBuffer(1024);
  bffer
   .append("select ti.card_in_no from lc_t_card_info ti")
   .append(
    " where ti.card_type=7 and ti.card_make_status=2 order by ti.card_in_no");// 卡信息表中所有的管理員記錄
  // 定義一個(gè)數(shù)組來(lái)接收黑名單中排序好的管理員卡內(nèi)碼
  String arr[][] = dbc.getArr(bf.toString());
  // 定義一個(gè)數(shù)組來(lái)接收卡信息表中排序好的管理員卡內(nèi)碼
  String listarr[][] = dbc.getArr(bffer.toString());
  upload_f(arr, file);
  // 得到黑名單bat文件的版本號(hào), 初始值為1.0
  String vesionSql = "select file_vesion from(select row_number() over(ORDER BY t.file_vesion DESC) num,"
   + "t.file_vesion from lc_t_upgrade_file t where t.file_type=2) where num=1";
  String vesion = dbc.getList0(vesionSql);
  double ve = 1.0;// 定義黑名單版本編號(hào)變量,初始值為1.0
  /*
  * 數(shù)據(jù)庫(kù)中存在舊版本則在版本增加0.1
  */
  if (StringUtil.isNotNull(vesion)) {
  ve = (Double.parseDouble(vesion) + 0.1);
  }
  vesion = df.format(ve);
  // 版本記錄sql語(yǔ)句
  String bmcsql = "insert into lc_t_upgrade_file values(" + fileID
   + ",'" + file + "','" + vesion + "','2',sysdate," + UserID
   + ")";
  dbc.insertDB(bmcsql);// 持久化到數(shù)據(jù)庫(kù)
  upload_f(listarr, whiteFile);
  // 得到白名單bat文件的版本號(hào), 初始值為1.0
  String vesionSql2 = "select file_vesion from(select row_number() over(ORDER BY t.file_vesion DESC) num,"
   + "t.file_vesion from lc_t_upgrade_file t where t.file_type=5) where num=1";
  String vesion2 = dbc.getList0(vesionSql2);
  double ve2 = 1.0;// 定義白名單版本編號(hào)變量,初始值為1.0
  /*
  * 數(shù)據(jù)庫(kù)中存在舊版本則在版本增加0.1
  */
  if (StringUtil.isNotNull(vesion2)) {
  ve2 = (Double.parseDouble(vesion2) + 0.1);
  }
  String bfileID = dbc.setOracleGlideValue("LC_T_UPGRADE_FILE");// 得到文件表的序列號(hào)
  vesion2 = df.format(ve2);
  // 版本記錄sql語(yǔ)句
  String bmcsql2 = "insert into lc_t_upgrade_file values(" + bfileID
   + ",'" + whiteFile + "','" + vesion2 + "','5',sysdate,"
   + UserID + ")";
  dbc.insertDB(bmcsql2);// 持久化到數(shù)據(jù)庫(kù)
 } else {
  StringBuffer bf2 = new StringBuffer(1024);
  bf2
   .append(
    "select distinct card_in_no from (select tc.card_in_no")
   .append(
    " from lc_t_blacklist tb left join lc_t_card_info tc")
   .append(
    " on tb.card_id = tc.card_id where tc.card_type <> 7")
   .append(" and tb.whether_effective = 1 union all select")
   .append(" tc.card_in_no from lc_t_card_loss cl left join")
   .append(" lc_t_card_info tc on cl.card_id = tc.card_id")
   .append(" where tc.card_type <> 7 and cl.whether_effective")
   .append(" = 1) t order by t.card_in_no");// 黑名單表及掛失用戶(hù)表中所有非管理員記錄
  // 定義一個(gè)數(shù)組來(lái)接收黑用戶(hù)中排序好的用戶(hù)卡內(nèi)碼
  String arr2[][] = dbc.getArr(bf2.toString());
  upload_f(arr2, buserfile);
  // 得到黑用戶(hù)bat文件的版本號(hào), 初始值為1.0
  String huserSql = "select file_vesion from(select row_number() over(ORDER BY t.file_vesion DESC) num,"
   + "t.file_vesion from lc_t_upgrade_file t where t.file_type=4) where num=1";
  String vesion3 = dbc.getList0(huserSql);
  double ves = 1.0;// 定義黑用戶(hù)版本編號(hào)變量,初始值為1.0
  /*
  * 數(shù)據(jù)庫(kù)中存在舊版本則在版本增加0.1
  */
  if (StringUtil.isNotNull(vesion3)) {
  ves = (Double.parseDouble(vesion3) + 0.1);
  }
  vesion3 = df.format(ves);
  // 版本記錄sql語(yǔ)句
  String husersql = "insert into lc_t_upgrade_file values(" + fileID
   + ",'" + buserfile + "','" + vesion3 + "','4',sysdate,"
   + UserID + ")";
  dbc.insertDB(husersql);// 持久化到數(shù)據(jù)庫(kù)
 }
 }
 /**
 *
 * 方法名稱(chēng):writeLong 方法描述:向輸出流中寫(xiě)長(zhǎng)整型
 *
 * @param input
 * @return byte[] version 1.0 author wuxq Sep 28, 2011 10:54:58 AM
 */
 public static byte[] writeLong(long input) {
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 DataOutputStream os = new DataOutputStream(baos);
 try {
  os.writeLong(Long.reverseBytes(input));
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 byte[] b = baos.toByteArray();
 return b;
 }
 /**
 *
 * 方法名稱(chēng):upload_f 方法描述:把文件上傳到ftp服務(wù)器
 *
 * @param arr
 * @param file
 *      void version 1.0 author wuxq Oct 8, 2011 11:37:27 AM
 */
 public static void upload_f(String[][] arr, String file) {
 byte by[] = null;
 byte[] result = new byte[1];
 if (StringUtil.isNotNull(arr)) {
  result = new byte[arr.length * 4];
  int position = 0;
  for (int i = 0; i < arr.length; i++) {
  by = writeLong(Long.parseLong(arr[i][0]));
  byte list[] = new byte[4];
  for (int h = 0; h < list.length; h++) {
   list[h] = by[h];
  }
  for (int g = position; g < position + 4; g++) {
   result[g] = list[g - 4 * i];
  }
  position = position + 4;
  }
 }
 boolean bool = FileUtil.creatWhiteManageFile(result, file);// 創(chuàng)建一個(gè)bat文件
 if (bool) {
  // InputStreamReader isr = new InputStreamReader(new
  // FileInputStream(file));
  InputStream inp = null;
  InputStream inp2 = null;
  try {
  inp = new BufferedInputStream(new FileInputStream(file));
  inp2 = new BufferedInputStream(new FileInputStream(file));
  } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
  // 截取文件名
  String f = file.substring(10, file.length());
  // 獲取ftp配置信息
  SysConstats sc = new SysConstats();
  FileUtil.uploadFileFtp(sc.FTPSERVER, sc.FTPUSERNAME,
   sc.FTPPASSWORD, sc.ENVELOPERESULTROOT, f, inp, inp2);
 }
 }
}

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Spring Boot中的那些條件判斷的實(shí)現(xiàn)方法

    Spring Boot中的那些條件判斷的實(shí)現(xiàn)方法

    這篇文章主要介紹了Spring Boot中的那些條件判斷的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Unity2019-2020 個(gè)人版官方免費(fèi)激活詳細(xì)方法

    Unity2019-2020 個(gè)人版官方免費(fèi)激活詳細(xì)方法

    這篇文章主要介紹了Unity2019-2020 個(gè)人版官方免費(fèi)激活詳細(xì)方法,激活方法分位兩種一種是激活新許可證,一種是手動(dòng)激活,感興趣的朋友跟隨小編一起看看吧
    2021-04-04
  • 詳解JAVA常用的時(shí)間操作【實(shí)用】

    詳解JAVA常用的時(shí)間操作【實(shí)用】

    本文主要介紹了JAVA一些常用的時(shí)間操作,很實(shí)用,相信大家在開(kāi)發(fā)項(xiàng)目時(shí)會(huì)用到,下面就跟小編一起來(lái)看下吧
    2016-12-12
  • spring/springboot整合curator遇到的坑及解決

    spring/springboot整合curator遇到的坑及解決

    這篇文章主要介紹了spring/springboot整合curator遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 淺談Thread.sleep()為什么要拋出中斷異常

    淺談Thread.sleep()為什么要拋出中斷異常

    本文主要介紹了淺談Thread.sleep()為什么要拋出中斷異常,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Kotlin中常見(jiàn)的List使用示例教程

    Kotlin中常見(jiàn)的List使用示例教程

    filter 就像其本意一樣,可以通過(guò) filter 對(duì) Kotlin list 進(jìn)行過(guò)濾,本文重點(diǎn)給大家介紹Kotlin中常見(jiàn)的List使用,感興趣的朋友一起看看吧
    2023-11-11
  • spring + shiro + cas 實(shí)現(xiàn)sso單點(diǎn)登錄的示例代碼

    spring + shiro + cas 實(shí)現(xiàn)sso單點(diǎn)登錄的示例代碼

    本篇文章主要介紹了spring + shiro + cas 實(shí)現(xiàn)sso單點(diǎn)登錄的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-09-09
  • java中orElse和orElseGet方法區(qū)別小結(jié)

    java中orElse和orElseGet方法區(qū)別小結(jié)

    這篇文章主要給大家介紹了關(guān)于java中orElse和orElseGet方法區(qū)別的相關(guān)資料,兩者之間的區(qū)別細(xì)微,但是卻在某些場(chǎng)景下顯的很重要,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 超個(gè)性修改SpringBoot項(xiàng)目的啟動(dòng)banner的方法

    超個(gè)性修改SpringBoot項(xiàng)目的啟動(dòng)banner的方法

    這篇文章主要介紹了超個(gè)性修改SpringBoot項(xiàng)目的啟動(dòng)banner的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • springboot結(jié)合websocket聊天室實(shí)現(xiàn)私聊+群聊

    springboot結(jié)合websocket聊天室實(shí)現(xiàn)私聊+群聊

    本文主要介紹了springboot結(jié)合websocket聊天室實(shí)現(xiàn)私聊+群聊,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評(píng)論