首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

java 保存ftp上的xml文件,该如何解决

2012-06-15 
java 保存ftp上的xml文件在ftp上有个xml文件,现在在本地需要把它保存到数据库!本人对这块文件保存一直不怎

java 保存ftp上的xml文件
在ftp上有个xml文件,现在在本地需要把它保存到数据库!本人对这块文件保存一直不怎么在行,现在又要远程保存,实在束手无策,哪位大侠帮帮忙,写下代码,就一个字段,把xml放到字段的代码就行,谢谢!

[解决办法]
下载下来;然后你想在怎么处理都可以;下载代码如下:

*/
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;
}
}

热点排行