在ftp上有个xml文件,现在在本地需要把它保存到数据库!本人对这块文件保存一直不怎么在行,现在又要远程保存,实在束手无策,哪位大侠帮帮忙,写下代码,就一个字段,把xml放到字段的代码就行,谢谢!
解决方案 »
- 请教一个struts2 <s:select> 抛异常的问题
- tomcat能启动但是报错,缺少核心jar包么
- sleep和wait的本质区别?
- 急求:一台机器上装两个版本的jdk,程序运行很慢,不知道是不是配置错误?
- 怎样架设一个web网站
- Connection reset 是指什么??????????????
- 新建了一个JAVA群,群号:17886000
- 为什么要将tomcat与apache整合?初学者问题
- 我的Hibernate为什么不能跑啊!
- 运行EJB,需要用什么应用服务器?讲讲区别,谢谢~~~~
- 关于nutch1.4的问题!!!!!!!
- java有没有想c#那样图形界面可以手动界面操作的呢??
File file = new File("http://192.168.3.20:8080\\e:\\ftp\\temp\\001.xml");
http://extjs2.iteye.com/blog/641455
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;
}
}