java ftp文件读取文件_Java实现对ftp的读写文件

/*** 下载pdf文件*/

publicString downLoadPdf(String url,String contNo,String localPdfName){

String newUrl="";

String pathUrl="172.18.100.165"; //FTP服务器hostname

int port=21;//FTP服务器端口

String username="shwasextt20\\ftp"; //FTP登录账号

String password="qwerty1!"; //FTP登录密码

String remotePath="/Imagedownload";//FTP服务器上的相对路径

String fileName;//要下载的文件名//String localPath="I:\\2015\\workspace\\workspace_newng\\wj\\WebContent\\wwwroot\\ng\\downLoad";//下载后保存到本地的路径

String localPath="C:\\project\\b2c\\cms.ear\\cms.war\\wwwroot\\ng\\downLoad";//下载后保存到本地的路径//String localPath="C:\\B2C\\cms.ear\\cms.war\\wwwroot\\ng\\downLoad";//下载后保存到本地的路径

String localName = "";

Date date= newDate();

SimpleDateFormat dr= new SimpleDateFormat("yyyyMMddHHmmss");//重命名(保单号+时间)

localName =localPdfName;if(!"".equals(url) && url !=null){//String backUrl = "ftp://172.18.100.165/Imagedownload/0180050037-个险合同-电子合同-201537071208(66e735db-8ddf-4e0e-b70c-339544ff630b).PDF";

fileName = url.split("/")[4];

pathUrl= url.split("/")[2];//生产环境ftp判断

if("172.16.252.100".equals(pathUrl)){

username= "shwasextp20\\ftp";

}else if("172.16.252.110".equals(pathUrl)){

username= "shwasextp21\\ftp";

}else{

username= "shwasextt20\\ftp";

}

FTPClient ftp= newFTPClient();try{

ftp.connect(pathUrl,port);

ftp.login(username,password);

System.out.println(ftp.isConnected());

ftp.enterLocalPassiveMode();

ftp.setControlEncoding("GBK");

ftp.setFileType(ftp.BINARY_FILE_TYPE);

ftp.changeWorkingDirectory(remotePath);

OutputStream outputStream= null;

FTPFile[] fs=ftp.listFiles();for (int i = 0; i < fs.length; i++) {

FTPFile ff=fs[i];if(ff.getName().equals(fileName)) {

InputStream in= ftp.retrieveFileStream(new String(ff.getName().getBytes("GBK"), "ISO-8859-1"));int len = 0;long size = 0;byte[] bt = new byte[1024];

outputStream=new BufferedOutputStream(new FileOutputStream(localPath+"\\"+localName+".pdf"));while ((len = in.read(bt)) > 0) {

outputStream.write(bt,0, len); //outputStream.flush();

size = size +len;//System.out.println(fileName + "已xiazai :" + size);

}

newUrl= "cms/wwwroot/ng/downLoad/"+localName+".pdf";

outputStream.flush();

}

}

outputStream.close();

ftp.logout();

}catch(SocketException e) {//TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}returnnewUrl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值