import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class FileTrans {
private String head = "";
private String hostname = "";
private String FilePath = "";
private String hdfsFilePath = "";
private HDFSUtil hdfsutil = null;
private FtpClient ftp;
private HttpUtil http;
public void setFilePath(String FilePath){
this.FilePath = FilePath;
}
public String getFilePath(String FilePath){
return this.FilePath;
}
public void sethdfsFilePath(String hdfsFilePath){
this.hdfsFilePath = hdfsFilePath;
}
public String gethdfsFilePath(String hdfsFilePath){
return this.hdfsFilePath;
}
public void setHostName(String hostname){
this.hostname = hostname;
}
public String getHostName(){
return this.hostname;
}
public void setHead(String head){
this.head = head;
}
public String getHead(){
return this.head;
}
public FileTrans(String head, String hostname, String filepath, String hdfsnode,String hdfsFilepath){
this.head = head;
this.hostname = hostname;
this.FilePath = filepath;
this.hdfsFilePath = hdfsFilepath;
if (head.equals("ftp") && hostname != ""){
this.ftp = new FtpClient(this.hostname);
}
if ((head.equals("http") || head .equals("https")) && hostname != ""){
String httpurl = head + "://" + hostname + "/" + filepath;
this.http = new HttpUtil(httpurl);
}
if (hdfsnode != ""){
this.hdfsutil = new HDFSUtil(hdfsnode);
}
this.hdfsutil.setHdfsPath(this.hdfsFilePath);
this.hdfsutil.setFilePath(hdfsutil.getHdfsNode()+hdfsutil.getHdfsPath());
this.hdfsutil.setHadoopSite("./hadoop-site.xml");
this.hdfsutil.setHadoopDefault("./hadoop-default.xml");
this.hdfsutil.setConfigure(false);
}
public static void main(String[] args) throws IOException{
String head = "";
String hostname = "";
String filepath = "";
String hdfsfilepath = "";
String hdfsnode = "";
String localpath = "";
InputStream inStream = null;
int samplelines = 0;
try{
head = args[0]; //远端服务器类型,http还是ftp
hostname = args[1]; //远端服务器hostname
filepath = args[2]; //远端文件路径
hdfsnode = args[3]; //hdfs的机器名,不带hdfs开头
hdfsfilepath = args[4]; //hdfs的文件路径
localpath = args[5]; //如果需要在本地保存一份的话,输入本地的路径,不保存,传入空格或者samplelines传入0
samplelines = Integer.parseInt(args[6]); //保存在本地的话,保存前N行,如果不保存,填0
}catch (Exception e){
System.out.println("[FileTrans]:input args error!");
e.printStackTrace();
}
FileTrans filetrans = new FileTrans(head, hostname, filepath, hdfsnode,hdfsfilepath);
if (filetrans == null){
System.out.println("filetrans null");
return;
}
if (filetrans.ftp == null && head.equals("ftp")){
System.out.println("filetrans ftp null");
return;
}
if (filetrans.http == null && (head.equals("http") || head.equals("https"))){
System.out.println("filetrans ftp null");
return;
}
try{
if (head.equals("ftp")){
inStream = filetrans.ftp.getStream(filepath);
if (samplelines > 0){
filetrans.ftp.writeStream(inStream, localpath, samplelines);
}
}
else{
inStream = filetrans.http.getStream(head + "://" + hostname + "/" + filepath);
if (samplelines > 0){
filetrans.http.downLoad(head + "://" + hostname + "/" + filepath, localpath, samplelines);
}
}
filetrans.hdfsutil.upLoad(inStream, filetrans.hdfsutil.getFilePath());
if (head == "ftp"){
filetrans.ftp.disconnect();
}
}catch (IOException e){
System.out.println("[FileTrans]: file trans failed!");
e.printStackTrace();
}
System.out.println("[FileTrans]: file trans success!");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有