java 保存ftp上的xml文件

inchon 2012-06-11 11:41:33
在ftp上有个xml文件,现在在本地需要把它保存到数据库!本人对这块文件保存一直不怎么在行,现在又要远程保存,实在束手无策,哪位大侠帮帮忙,写下代码,就一个字段,把xml放到字段的代码就行,谢谢!
...全文
141 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zwl175369 2012-06-12
  • 打赏
  • 举报
回复
访问路径用户名密码改改下;这代码能用
zwl175369 2012-06-12
  • 打赏
  • 举报
回复
下载下来;然后你想在怎么处理都可以;下载代码如下:

*/
public long uploadFile(String newname,byte[] b) throws Exception{
long result = 0;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
os = ftpClient.put(newname);
os.write(b);

}catch (Exception e) {
e.printStackTrace();
}finally{
if(is != null){
is.close();
}
if(os != null){
os.close();
}
}
return result;
}

/**
* 从ftp下载文件到本地
*
* @param filename 服务器上的文件名
* @param newfilename 本地生成的文件名
* @return
* @throws Exception
*/
public long downloadFile(String filename, String newfilename){
long result = 0;
TelnetInputStream is = null;
FileOutputStream os = null;
try{
is = ftpClient.get(filename);
java.io.File outfile = new java.io.File(newfilename);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
result = result + c;
}
}catch (IOException e){
e.printStackTrace();
}finally{
try {
if(is != null){
is.close();
}
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}

/**
* 取得相对于当前连接目录的某个目录下所有文件列表
*
* @param path
* @return
*/
public List getFileList(String path){
List list = new ArrayList();
DataInputStream dis;
try {
dis = new DataInputStream(ftpClient.nameList(this.path + path));
String filename = "";
while((filename = dis.readLine()) != null){
list.add(filename);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}

public static void main(String[] args){
FtpUtil ftp = new FtpUtil();

ftp.connectServer();
List list = ftp.getFileList("/");
for(int i=0;i<list.size();i++){
String name = list.get(i).toString();
System.out.println(name);
}
ftp.closeServer();
// System.out.println(ftp.isDirExist("/jinyuan/22.jpg"));
// boolean result = ftp.upload("D:/3_1339055260593.jpg", "3_1339055260593.jpg");
// System.out.println(result?"上传成功!":"上传失败!");
//System.out.println(ftp.downloadFile("/1234.jpg","D:/1234.jpg"));
// String str="/jinyuan/pic5.jpg";
// ftp.connectServerFTP();
// System.out.println(ftp.deleteFTP(str));
// ftp.closeConnectFTP();
// List<String> list=ftp.getFileList("/jinyuan");
// for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i));
// }
// System.out.println(list.size());
//ftp.delete(str);
// ftp.createDir("/test");
// ftp.connectServer();
// List list1 = ftp.getFileList("/jinyuan");
// for(int i=0;i<list1.size();i++){
// String name = list1.get(i).toString();
// System.out.println(name);
// }
// ftp.closeServer();
/**
FTP远程命令列表
USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT
PASS PASV STOR REST CWD STAT RMD XCUP OPTS
ACCT TYPE APPE RNFR XCWD HELP XRMD STOU AUTH
REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ
QUIT MODE SYST ABOR NLST MKD XPWD MDTM PROT
在服务器上执行命令,如果用sendServer来执行远程命令(不能执行本地FTP命令)的话,所有FTP命令都要加上\r\n
ftpclient.sendServer("XMKD /test/bb\r\n"); //执行服务器上的FTP命令
ftpclient.readServerResponse一定要在sendServer后调用
nameList("/test")获取指目录下的文件列表
XMKD建立目录,当目录存在的情况下再次创建目录时报错
XRMD删除目录
DELE删除文件
*/
}
/**
* 删除文件
* @param remoteFile ****删除在服务器上的路径和文件名***
* @return
*/
public boolean deleteFTP(String remoteFile)
{
boolean flag = false;
try
{
flag = FTP.deleteFile(remoteFile);
}
catch (IOException e)
{
closeConnectFTP();
return false;
}
return flag;
}
/**
* FTPClient 登录ftp服务器
*
* @param server
* @param port
* @param user
* @param password
* @throws IOException
* @
*/
public boolean connectServerFTP() throws IOException {
boolean megString=true;
ResourceBundle bundle=PropertyResourceBundle.getBundle("config");
if (FTP == null) {
FTP=new FTPClient();
//所有使用iso-8859-1做为通讯编码集
// //ftpClient.setControlEncoding("iso-8859-1");
// //ftpClient.configure(getFTPClientConfig());
//
// 连接FTP服务器

FTP.connect(ip,Integer.valueOf(port));
// 登录FTP服务器
if(!FTP.login(username, password)){
megString=false;
}
//状态
int reply = FTP.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
closeConnectFTP();
System.out.println("ccc");
// this.addActionError(getText("customer.electroFile.errors.logonFTP"));
}

// 用被动模式传输
FTP.enterLocalPassiveMode();
// 将文件传输类型设置为二进制
FTP.setFileType(FTP.BINARY_FILE_TYPE);
//防止server超时断开
FTP.setDefaultTimeout(60000);
//10超时
FTP.setSoTimeout(10000);

}

return megString;
}
/**
* 关闭连接
*/
public void closeConnectFTP() {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}

if (FTP != null) {
FTP.logout();
FTP.disconnect();
}
System.out.println("已从服务器断开");
} catch (Exception e) {

}
}
/**
* 转码[GBK -> ISO-8859-1]
*不同的平台需要不同的转码
* @param obj
* @return
*/
private static String gbktoiso8859(Object obj) {
try {
if (obj == null)
return "";
else
return new String(obj.toString().getBytes("GBK"), "iso-8859-1");
} catch (Exception e) {
return "";
}
}
/**
* 控制文件的大小
* @param file_in
*/
private boolean fileSize(File file_in) {
if (file_in == null || file_in.length() == 0) {
//addActionError(getText("customer.electroFile.errors.fileUnallowed"));
return false;
} else {
if (file_in.length() > (1024 * 1024 * 5)){
return false;
}
}
return true;
}
}
古布 2012-06-12
  • 打赏
  • 举报
回复
很少用,遇到的问题。只要你描述清楚,回答你的人会很多
  • 打赏
  • 举报
回复
你可以用程序链接ftp,将xml文件下载下来,然后进行读取操作,取得xml文件的方法网上有很多,放狗去搜吧!
inchon 2012-06-11
  • 打赏
  • 举报
回复
请问下3楼的朋友,你有QQ吗?加QQ聊好吗?
古布 2012-06-11
  • 打赏
  • 举报
回复
用apache FTPClient
http://extjs2.iteye.com/blog/641455
inchon 2012-06-11
  • 打赏
  • 举报
回复
我知道思路的,只是这个远程的操作,没做过,而且,路径也老不对!你看看我的路径对不?
File file = new File("http://192.168.3.20:8080\\e:\\ftp\\temp\\001.xml");

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧