设为首页 收藏本站
查看: 1087|回复: 0

[经验分享] 将文件上传ftp服务器

[复制链接]

尚未签到

发表于 2016-6-9 09:24:46 | 显示全部楼层 |阅读模式
//工具类
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服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
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;
}
/**
*
* 方法名称:uploadFileFtp
* 方法描述:黑名名单,黑用户文件上传ftp服务器
* @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服务器信息的bean类
*
* @author wuxq
*
*/
public class SysConstats {
private static Logger log = Logger.getLogger(SysConstats.class);
public static String FTPSERVER;// ftp服务器ip地址
public static String FTPUSERNAME;// ftp服务器用户名
public static String FTPPASSWORD;// ftp服务器用户密码
public static String ENVELOPERESULTROOT;// 存放ftp服务器的路径
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;
/**
* 上传卡内码到ftp服务器 生成bat文件
*
* @author wuxq 2011-09-28
*/
public class UploadCardInNoFtpAction {
/**
*
* 方法名称: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();// 链接数据库
dbc.endTran();
// 判断是否是空值 空有可能是挂失,退出挂失, 退出黑名单, 根据卡id得到卡类型
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"; // 定义一个目录存放临时的黑名单bat文件
String whiteFile = top + "wmc.dat";// 定义一个目录存放临时的白名单bat文件
String buserfile = top + "buser.dat"; // 定义一个目录存放临时的黑用户文件
String fileID = dbc.setOracleGlideValue("LC_T_UPGRADE_FILE");// 得到文件表的序列号
// 得到当前用户的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");// 卡信息表中所有的管理员记录
// 定义一个数组来接收黑名单中排序好的管理员卡内码
String arr[][] = dbc.getArr(bf.toString());
// 定义一个数组来接收卡信息表中排序好的管理员卡内码
String listarr[][] = dbc.getArr(bffer.toString());
upload_f(arr, file);
// 得到黑名单bat文件的版本号, 初始值为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;// 定义黑名单版本编号变量,初始值为1.0
/*
* 数据库中存在旧版本则在版本增加0.1
*/
if (StringUtil.isNotNull(vesion)) {
ve = (Double.parseDouble(vesion) + 0.1);
}
vesion = df.format(ve);
// 版本记录sql语句
String bmcsql = "insert into lc_t_upgrade_file values(" + fileID
+ ",'" + file + "','" + vesion + "','2',sysdate," + UserID
+ ")";
dbc.insertDB(bmcsql);// 持久化到数据库
upload_f(listarr, whiteFile);
// 得到白名单bat文件的版本号, 初始值为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;// 定义白名单版本编号变量,初始值为1.0
/*
* 数据库中存在旧版本则在版本增加0.1
*/
if (StringUtil.isNotNull(vesion2)) {
ve2 = (Double.parseDouble(vesion2) + 0.1);
}
String bfileID = dbc.setOracleGlideValue("LC_T_UPGRADE_FILE");// 得到文件表的序列号
vesion2 = df.format(ve2);
// 版本记录sql语句
String bmcsql2 = "insert into lc_t_upgrade_file values(" + bfileID
+ ",'" + whiteFile + "','" + vesion2 + "','5',sysdate,"
+ UserID + ")";
dbc.insertDB(bmcsql2);// 持久化到数据库
} 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");// 黑名单表及挂失用户表中所有非管理员记录
// 定义一个数组来接收黑用户中排序好的用户卡内码
String arr2[][] = dbc.getArr(bf2.toString());
upload_f(arr2, buserfile);
// 得到黑用户bat文件的版本号, 初始值为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;// 定义黑用户版本编号变量,初始值为1.0
/*
* 数据库中存在旧版本则在版本增加0.1
*/
if (StringUtil.isNotNull(vesion3)) {
ves = (Double.parseDouble(vesion3) + 0.1);
}
vesion3 = df.format(ves);
// 版本记录sql语句
String husersql = "insert into lc_t_upgrade_file values(" + fileID
+ ",'" + buserfile + "','" + vesion3 + "','4',sysdate,"
+ UserID + ")";
dbc.insertDB(husersql);// 持久化到数据库
}
}
/**
*
* 方法名称:writeLong 方法描述:向输出流中写长整型
*
* @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;
}
/**
*
* 方法名称:upload_f 方法描述:把文件上传到ftp服务器
*
* @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[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);// 创建一个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);
}
}
}
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-228081-1-1.html 上篇帖子: FTP乱码与DataBase乱码问题整理 下篇帖子: Ftp上传文件客户端java代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表