日期:2014-05-19  浏览次数:20683 次

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));
//