网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Java中怎么对接远程文件

Java中怎么对接远程文件,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联是一家专业从事成都做网站、网站制作、成都外贸网站建设、网页设计的品牌网络公司。如今是成都地区具影响力的网站设计公司,作为专业的成都网站建设公司,创新互联依托强大的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、营销型网站建设及网站设计开发服务!

1、配置文件:copyRemoteFile.properties

#  src/dao.properties    #      这里保存的都是键值对信息    #  interface name(no packgage) = implementation class  # 注意:    #A:【路径符号】【必须】是【/】【如:D:/home/publish】    #B:【键key=值value】对【后面】【绝不允许有空格】【如:REMOTE_HOST_IP=172.77.9.77】   # REMOTE_HOST_IP  远程机器IP    # LOGIN_ACCOUNT   远程机器登录名    # LOGIN_PASSWORD  远程机器登录密码    # SHARE_DOC_NAME  远程机器共享文件夹名(设置共享后必须授予读写权限)   # sourcePath      本地路径    # targetPath      目标路径(真实路径=共享文件夹路径+目标路径)   REMOTE_HOST_IP=172.77.9.77   LOGIN_ACCOUNT=77   LOGIN_PASSWORD=77   SHARE_DOC_NAME=vfs_home    sourcePath=D:/home/publish   targetPath=publish

2、导入jar包:jcifs-1.3.16.jar

3、读取配置文件中key对应的value类:RemoteConfigUtil.java

package com.remote;       import java.io.IOException;   import java.util.Properties;       /**    * 读取配置文件中key对应的value    * @author JunjieQin    */ public class RemoteConfigUtil {       private String REMOTE_HOST_IP;       private String LOGIN_ACCOUNT;       private String LOGIN_PASSWORD;       private String SHARE_DOC_NAME;       private String sourcePath;       private String targetPath;           //无参构造方法       public RemoteConfigUtil() {           try {               // 读取配置文件               Properties prop = new Properties();               prop.load(this.getClass().getClassLoader().getResourceAsStream("copyRemoteFile.properties"));               // 根据 key 获取 value               REMOTE_HOST_IP = prop.getProperty("REMOTE_HOST_IP");               LOGIN_ACCOUNT = prop.getProperty("LOGIN_ACCOUNT");               LOGIN_PASSWORD = prop.getProperty("LOGIN_PASSWORD");               SHARE_DOC_NAME = prop.getProperty("SHARE_DOC_NAME");               sourcePath = prop.getProperty("sourcePath");               targetPath = prop.getProperty("targetPath");           } catch (IOException e) {               e.printStackTrace();           }       }       public String getLOGIN_ACCOUNT() {           return LOGIN_ACCOUNT;       }           public void setLOGIN_ACCOUNT(String login_account) {           LOGIN_ACCOUNT = login_account;       }           public String getLOGIN_PASSWORD() {           return LOGIN_PASSWORD;       }           public void setLOGIN_PASSWORD(String login_password) {           LOGIN_PASSWORD = login_password;       }           public String getREMOTE_HOST_IP() {           return REMOTE_HOST_IP;       }           public void setREMOTE_HOST_IP(String remote_host_ip) {           REMOTE_HOST_IP = remote_host_ip;       }           public String getSHARE_DOC_NAME() {           return SHARE_DOC_NAME;       }           public void setSHARE_DOC_NAME(String share_doc_name) {           SHARE_DOC_NAME = share_doc_name;       }           public String getSourcePath() {           return sourcePath;       }           public void setSourcePath(String sourcePath) {           this.sourcePath = sourcePath;       }           public String getTargetPath() {           return targetPath;       }           public void setTargetPath(String targetPath) {           this.targetPath = targetPath;       }   }

4、操作远程共享文件夹类: RemoteFileUtil.java

根据需求选择相应的 Method

package com.remote;   import java.io.BufferedOutputStream;   import java.io.BufferedReader;   import java.io.File;   import java.io.FileInputStream;   import java.io.FileNotFoundException;   import java.io.IOException;   import java.io.InputStream;   import java.io.InputStreamReader;   import java.io.OutputStream;   import java.net.MalformedURLException;   import java.net.UnknownHostException;   import java.util.ArrayList;   import java.util.List;       import jcifs.smb.SmbException;   import jcifs.smb.SmbFile;   import jcifs.smb.SmbFileInputStream;   import jcifs.smb.SmbFileOutputStream;       /***********************************************    *  File Name: RemoteFileUtil.java                 *  Created by: JunjieQin          *  Checked in by:        *  Date: 2011-9-6               *  Revision: 1.7            *  Description:操作远程共享文件夹类    *  Amendment History    *  Modified Date:2011-9-16             *  Modified By:JunjieQin       *  Change Description:From local copy files to remote directory    *    ***********************************************/ public class RemoteFileUtil {                  private ArrayList filelist = new ArrayList();       RemoteConfigUtil rc = new RemoteConfigUtil();           private String remoteHostIp;  //远程主机IP          private String account;       //登陆账户          private String password;      //登陆密码          private String shareDocName;  //共享文件夹名称                     /**          * 默认构造函数          */        public RemoteFileUtil(){             this.remoteHostIp = rc.getREMOTE_HOST_IP();              this.account = rc.getLOGIN_ACCOUNT();              this.password = rc.getLOGIN_PASSWORD();              this.shareDocName = rc.getSHARE_DOC_NAME();          }                     /**          * 构造函数          * @param remoteHostIp  远程主机Ip          * @param account       登陆账户          * @param password      登陆密码          * @param sharePath     共享文件夹路径          */        public RemoteFileUtil(String remoteHostIp, String account, String password,String shareDocName) {              this.remoteHostIp = remoteHostIp;              this.account = account;              this.password = password;              this.shareDocName = shareDocName;          }                        /**          * 对远程共享文件进行读取所有行          * @param remoteFileName  文件名  说明:参数为共享目录下的相对路径          *  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);          *  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;          * @return  文件的所有行          */        public List readFile(String remoteFileName){              SmbFile smbFile = null;              BufferedReader reader = null;              List resultLines = null;              //构建连接字符串,并取得文件连接              String conStr = null;              conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;              try {                  smbFile = new SmbFile(conStr);              } catch (MalformedURLException e) {                  e.printStackTrace();              }              //创建reader              try {                  reader = new BufferedReader(new InputStreamReader(new SmbFileInputStream(smbFile)));              } catch (SmbException e) {                  e.printStackTrace();              } catch (MalformedURLException e) {                  e.printStackTrace();              } catch (UnknownHostException e) {                  e.printStackTrace();              }                     //循环对文件进行读取              String line;              try {                  line = reader.readLine();                  if(line != null && line.length()>0){                      resultLines = new ArrayList();                  }                  while (line != null) {                      resultLines.add(line);                      line = reader.readLine();                  }              } catch (IOException e) {                  e.printStackTrace();              }              //返回              return resultLines;          }                     /**          * 对远程共享文件进行写入          * @param is                本地文件的输入流          * @param remoteFileName    远程文件名    说明:参数为共享目录下的相对路径          *  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);          *  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;          * @return            */        public boolean writeFile(InputStream is,String remoteFileName){              SmbFile smbFile = null;              OutputStream os = null;              byte[] buffer = new byte[1024*8];              //构建连接字符串,并取得文件连接              String conStr = null;              conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/"+remoteFileName;              try {                  smbFile = new SmbFile(conStr);              } catch (MalformedURLException e) {                  e.printStackTrace();                  return false;              }                             //获取远程文件输出流并写文件到远程共享文件夹              try {                  os = new BufferedOutputStream(new SmbFileOutputStream(smbFile));                  while((is.read(buffer))!=-1){                      os.write(buffer);                         }              } catch (Exception e) {                  e.printStackTrace();                  return false;              }                              return true;          }                                /**          * 对远程共享文件进行写入重载          * @param localFileName   要写入的本地文件全名          * @param remoteFileName  远程文件名    说明:参数为共享目录下的相对路径          *  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);          *  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;          * @return          */        public boolean writeFile(String localFileFullName ,String remoteFileName){              try {                  return writeFile(new FileInputStream(new File(localFileFullName)),remoteFileName);              } catch (FileNotFoundException e) {                  e.printStackTrace();                  return false;              }          }                     /**          * 对远程共享文件进行写入重载          * @param localFileName   要写入的本地文件          * @param remoteFileName  远程文件名    说明:参数为共享目录下的相对路径          *  若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称);          *  若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt;          * @return          */        public boolean writeFile(File localFile ,String remoteFileName){              try {                  return writeFile(new FileInputStream(localFile),remoteFileName);              } catch (FileNotFoundException e) {                  e.printStackTrace();                  return false;              }          }                         /**          * 对远程共享文件所有文件          * @return  所有文件         */        public List getFiles(){              SmbFile smbFile = null;              BufferedReader reader = null;              List resultLines = new ArrayList();              //构建连接字符串,并取得文件连接              String conStr = null;              conStr = "smb://"+account+":"+password+"@"+remoteHostIp+"/"+shareDocName+"/";              try {                  smbFile = new SmbFile(conStr);              } catch (MalformedURLException e) {                  e.printStackTrace();              }              //创建reader              try {                String[] a = smbFile.list();             for(int i=0;i= 0) {                       filelist.add(files[i].getName());                   }               }           }           return filelist;       }           // 测试从本地复制文件到远程目标目录,测试已通过       public static void main(String[] args) {           RemoteConfigUtil rc = new RemoteConfigUtil();           RemoteFileUtil util = new RemoteFileUtil();           util.copyFileToRemoteDir(rc.getSourcePath(), rc.getTargetPath());       }   }

看完上述内容,你们掌握Java中怎么对接远程文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


分享文章:Java中怎么对接远程文件
文章起源:http://bjjierui.cn/article/jphded.html

其他资讯