﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-月亮学习屋</title><link>http://www.blogjava.net/sunbojie/</link><description>技术港湾</description><language>zh-cn</language><lastBuildDate>Thu, 18 Jun 2026 12:55:09 GMT</lastBuildDate><pubDate>Thu, 18 Jun 2026 12:55:09 GMT</pubDate><ttl>60</ttl><item><title>java操作FTP服务器文件系统代码(转)</title><link>http://www.blogjava.net/sunbojie/articles/333073.html</link><dc:creator>月亮哭了</dc:creator><author>月亮哭了</author><pubDate>Mon, 27 Sep 2010 09:33:00 GMT</pubDate><guid>http://www.blogjava.net/sunbojie/articles/333073.html</guid><wfw:comment>http://www.blogjava.net/sunbojie/comments/333073.html</wfw:comment><comments>http://www.blogjava.net/sunbojie/articles/333073.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/sunbojie/comments/commentRss/333073.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/sunbojie/services/trackbacks/333073.html</trackback:ping><description><![CDATA[<span  style="color: #6d6d6d; font-family: Arial, Helvetica, sans-serif; font-size: 14px; line-height: 22px; ">
<p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">package com.dicpsi.common.file;</p>
<p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br style="line-height: 22px; " />
import java.io.BufferedReader;<br style="line-height: 22px; " />
import java.io.File;<br style="line-height: 22px; " />
import java.io.FileInputStream;<br style="line-height: 22px; " />
import java.io.FileReader;<br style="line-height: 22px; " />
import java.io.FileWriter;<br style="line-height: 22px; " />
import java.io.IOException;<br style="line-height: 22px; " />
import java.io.InputStream;<br style="line-height: 22px; " />
import java.io.PrintWriter;<br style="line-height: 22px; " />
import java.net.*;</p>
<p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">import sun.net.TelnetOutputStream;<br style="line-height: 22px; " />
import sun.net.ftp.FtpClient;</p>
<p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">public class ftpWriteAndRead {<br style="line-height: 22px; " />
&nbsp;String ip="10.140.112.120";//ftp服务器默认IP地址<br style="line-height: 22px; " />
&nbsp;int port=21;//ftp服务默认端口<br style="line-height: 22px; " />
&nbsp;String user="";//登录ftp所需帐户<br style="line-height: 22px; " />
&nbsp;String pwd="";//密码<br style="line-height: 22px; " />
&nbsp;String path="";//所在的路径<br style="line-height: 22px; " />
&nbsp;String err="";<br style="line-height: 22px; " />
&nbsp;/*<br style="line-height: 22px; " />
&nbsp; * 测试方法，用URL类直接读取ftp上的文件<br style="line-height: 22px; " />
&nbsp; * */<br style="line-height: 22px; " />
&nbsp;public void tt() throws MalformedURLException{<br style="line-height: 22px; " />
&nbsp;&nbsp;try{<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;URL u=new URL("<a href="ftp://xjdl93:yzj@10.140.112.120/HBSQ/System_copy/123.txt" style="line-height: 22px; text-decoration: none; color: #000000; ">ftp://xjdl93:yzj@10.140.112.120/HBSQ/System_copy/123.txt</a>");<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;String _file=u.getFile();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;System.out.println(_file+"&gt;&gt;&gt;"+u.getHost());<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;InputStream is= u.openStream();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;if(is == null){<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("没有能够获取指定的资源");<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;return;<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;&nbsp;}catch(Exception e){<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;e.printStackTrace();<br style="line-height: 22px; " />
&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;}<br style="line-height: 22px; " />
&nbsp;/**<br style="line-height: 22px; " />
&nbsp; * @comment 获取FTP客户端连接<br style="line-height: 22px; " />
&nbsp; * @author&nbsp; chenshan<br style="line-height: 22px; " />
&nbsp; * @date&nbsp;&nbsp;&nbsp; 2008-11-11<br style="line-height: 22px; " />
&nbsp; * @param&nbsp;&nbsp; ip：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftp服务器所在的IP地址；&nbsp;<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;port：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ftp服务占用的端口；<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;user：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 登录ftp所需帐户；<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;pwd：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 登录ftp所需密码；<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;path：&nbsp;&nbsp;要登录到ftp的相对路径，如要进入<a href="ftp://10.140.112.120/HBSQ/System_copy" style="line-height: 22px; text-decoration: none; color: #000000; ">ftp://10.140.112.120/HBSQ/System_copy</a>，<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;那么path应该为&#8221;HBSQ/System_copy&#8220;<br style="line-height: 22px; " />
&nbsp; */<br style="line-height: 22px; " />
&nbsp;public FtpClient getFtpConnection(String ip,int port,String user,String pwd,String path){<br style="line-height: 22px; " />
&nbsp;&nbsp;try{<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;FtpClient cftp=new FtpClient();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;cftp.openServer(ip,port);<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;cftp.login(user, pwd);<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;System.out.println("!");<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;if (path.length() != 0) cftp.cd(path);<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;//cftp.binary();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;return cftp;<br style="line-height: 22px; " />
&nbsp;&nbsp;}catch(Exception ex){<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;err+=ex.getMessage();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;return null;<br style="line-height: 22px; " />
&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;}<br style="line-height: 22px; " />
&nbsp;<br style="line-height: 22px; " />
&nbsp;/**<br style="line-height: 22px; " />
&nbsp; * @comment 读本地文件将其写入ftp服务器（该方法测试用）<br style="line-height: 22px; " />
&nbsp; * @author&nbsp; chenshan<br style="line-height: 22px; " />
&nbsp; * @date&nbsp;&nbsp;&nbsp; 2008-11-11<br style="line-height: 22px; " />
&nbsp; * @param&nbsp;&nbsp; textContext：所需写入的内容&nbsp;<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;<br style="line-height: 22px; " />
&nbsp; */<br style="line-height: 22px; " />
&nbsp;public void upload(String textContext) {<br style="line-height: 22px; " />
&nbsp;&nbsp;this.user="xjdl93";<br style="line-height: 22px; " />
&nbsp;&nbsp;this.pwd="yzj";<br style="line-height: 22px; " />
&nbsp;&nbsp;this.path="HBSQ/System_copy";<br style="line-height: 22px; " />
&nbsp;&nbsp;FtpClient ftpClient = getFtpConnection(this.ip,this.port,this.user,this.pwd,this.path);<br style="line-height: 22px; " />
&nbsp;&nbsp;String localfilename = "E://testftp//qqq.txt";<br style="line-height: 22px; " />
&nbsp;&nbsp;String remotefilename = "qqq.txt";<br style="line-height: 22px; " />
&nbsp;&nbsp;<br style="line-height: 22px; " />
&nbsp;&nbsp;/*以下为获取需要上传的文件列表*/<br style="line-height: 22px; " />
//&nbsp;&nbsp;try{<br style="line-height: 22px; " />
//&nbsp;&nbsp;&nbsp;URL url=new URL("E:\testftp\qqq.txt");<br style="line-height: 22px; " />
//&nbsp;&nbsp;&nbsp;String [] fileNames=null;<br style="line-height: 22px; " />
//&nbsp;&nbsp;&nbsp;//fileNames=url.getFile().;<br style="line-height: 22px; " />
//&nbsp;&nbsp;&nbsp;System.out.print(url.getFile());<br style="line-height: 22px; " />
//&nbsp;&nbsp;&nbsp;return;<br style="line-height: 22px; " />
//&nbsp;&nbsp;}catch(Exception a){<br style="line-height: 22px; " />
//&nbsp;&nbsp;&nbsp;a.printStackTrace();<br style="line-height: 22px; " />
//&nbsp;&nbsp;&nbsp;System.out.println("获取所需上传文件列表失败");<br style="line-height: 22px; " />
//&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;&nbsp;<br style="line-height: 22px; " />
&nbsp;&nbsp;try {<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;TelnetOutputStream os = ftpClient.put(remotefilename);//在服务器上创建文件<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;/*以下为读出本地文件*/<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;java.io.File file_in = new java.io.File(localfilename);<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;FileInputStream is = new FileInputStream(file_in);<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;byte[] bytes = new byte[1024];<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;int c;<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;while ((c = is.read(bytes)) != -1) {<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;os.write(bytes, 0, c);//写入到ftp上创建的文件中<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;System.out.println("upload success");<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;is.close();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;os.close();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;ftpClient.closeServer();<br style="line-height: 22px; " />
&nbsp;&nbsp;} catch (IOException ex) {<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;ex.printStackTrace();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;System.out.println("not upload");<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;System.out.println(ex);<br style="line-height: 22px; " />
&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;}<br style="line-height: 22px; " />
&nbsp;<br style="line-height: 22px; " />
&nbsp;/**<br style="line-height: 22px; " />
&nbsp; * @comment 在ftp服务器创建文件，并将指定的内容写入文件<br style="line-height: 22px; " />
&nbsp; * @author&nbsp; chenshan<br style="line-height: 22px; " />
&nbsp; * @date&nbsp;&nbsp;&nbsp; 2008-11-11<br style="line-height: 22px; " />
&nbsp; * @param&nbsp;&nbsp; textContext:&nbsp;&nbsp;&nbsp; 需要写入ftp文件的内容字符串,&nbsp;<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;remotefilename: 在ftp上创建的文件名称<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;bankCode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 银行代码（夏津的银行代码为93和97，93是邮政局，97是中国银行）<br style="line-height: 22px; " />
&nbsp; * &nbsp;&nbsp;&nbsp;path&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 代扣文件需要发送至服务器的相对路径<br style="line-height: 22px; " />
&nbsp; */<br style="line-height: 22px; " />
&nbsp;public String writeFileToFtp(String textContext,String remotefilename,String bankCode,String path){<br style="line-height: 22px; " />
&nbsp;&nbsp;try{<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;this.path=path;<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;if(bankCode.equals("97")){<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;this.user="xjdl97";<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;this.pwd="123456";<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;else if(bankCode.equals("93")){<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;this.user="xjdl93";<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;this.pwd="yzj";<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;else{<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("发现不合法的银行代码: "+bankCode+"=========&gt;代扣文件未能发送至FTP服务器");<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;&nbsp;return "发现不合法的银行代码: "+bankCode+" --&gt;代扣文件未能发送至FTP服务器,请仔细检查并手工拷贝！";<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;FtpClient ftpClient = getFtpConnection(this.ip,this.port,this.user,this.pwd,this.path);<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;byte[] _textContext = textContext.getBytes();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;TelnetOutputStream os = ftpClient.put(remotefilename);//在服务器上创建文件<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;//将字节内容写入到os所代表的文件中<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;os.write(_textContext,0,_textContext.length);<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;//关闭与ftp服务器的连接<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;System.out.println("upload success");<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;os.close();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;ftpClient.closeServer();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;return "0";<br style="line-height: 22px; " />
&nbsp;&nbsp;}catch(Exception ex){<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;this.err+=ex.getMessage();<br style="line-height: 22px; " />
&nbsp;&nbsp;&nbsp;return err;<br style="line-height: 22px; " />
&nbsp;&nbsp;}<br style="line-height: 22px; " />
&nbsp;}</p>
<p style="line-height: 22px; margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">}</p>
</span>
<img src ="http://www.blogjava.net/sunbojie/aggbug/333073.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/sunbojie/" target="_blank">月亮哭了</a> 2010-09-27 17:33 <a href="http://www.blogjava.net/sunbojie/articles/333073.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>