java 远程连接_java连接远程服务器(示例代码)

我用的是smb协议 共享方式连接远程 Windows服务器  也可以用ftp 但要保证服务器是ftp的 连接Linux服务器可以用ssh 协议

新建一个res.properites连接

IP=10.61.28.56

SMB_MINGCHENG=smb://jichuzhongxin:[email protected] //服务器登录名 密码 IP

WENJIAN_NAME=/res.properties

//代码

public class UploadFile{

// 读取服务器文件夹到本地

public boolean upload() {

InputStream inStream = UploadFile.class

.getResourceAsStream("res.properties");

Properties p = new Properties();

FileOutputStream os = null;

SmbFileInputStream in = null;

SmbFileOutputStream so = null;

SmbFile smbFile = null;

SmbFile copyFile = null;

SmbFile[] listFiles = null;

String url = null;

SmbFileInputStream ins=null;

SmbFile smbFile1 = null;

//boolean b = true;

String fileNmae="";

StringBuilder name=new StringBuilder();

try {

p.load(inStream);

url = p.getProperty("SMB_MINGCHENG");

// 文件夹路径

smbFile = new SmbFile(url + "/网调公用/网络系统支撑中心-联信/拆基站电路的需求/正式/");

// 备份的文件夹路径

copyFile = new SmbFile(url + "/网调公用/网络系统支撑中心-联信/拆基站电路的需求/备份/");

listFiles = smbFile.listFiles();

} catch (Exception e) {

e.printStackTrace();

}

// for循环获取每一个文件

for (int i = 0; i < listFiles.length; i++) {

System.out.println(listFiles.length);

smbFile1 = listFiles[i];

try {

if (!smbFile1.isFile()) {

continue;

} else {

try {

if (smbFile1.getName().toString().endsWith(".xls")) {

// 下载到本地的文件夹路径

//os = new FileOutputStream("d:\\"+ smbFile1.getName());

// 备份的文件夹路径

so = new SmbFileOutputStream(copyFile+smbFile1.getName());

//文件名称

fileNmae=smbFile1.getName().toString();

name.append(fileNmae+",");

in = new SmbFileInputStream(smbFile1);

System.out.println("导出的文件名"+smbFile1.getName());

//导入到数据库

this.importExcel(in, fileNmae);

//HSSFWorkbook xwb = new HSSFWorkbook(in);

ins = new SmbFileInputStream(smbFile1);

int len = 0;

byte[] bytes = new byte[1024];

//System.out.println("下载到本地和备份到服务器另一个文件夹");

while ((len = ins.read(bytes)) > 0) {

//os.write(bytes, 0, len);

so.write(bytes, 0, len);

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

inStream.close();

in.close();

ins.close();

so.flush();

//os.flush();

//os.close();

so.close();

} catch (IOException e2) {

e2.printStackTrace();

}

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

System.out.println("备份和导出数据库成功");

//调用存储

JdbcUtil jdbcUtil=new JdbcUtil();

boolean b=jdbcUtil.queryAll();

//System.out.println(boo);

if(b== true){

//导出到服务器

ExportShenQingDan export = new ExportShenQingDan();

export.exportExcel(name);

return b;

}else{

return b=false;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值