java获取远程文件

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

新建一个res.properites连接

IP=10.61.28.56

SMB_MINGCHENG=smb://jichuzhongxin:jichuzhongxin@10.61.28.56 //服务器登录名 密码 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;
}


}

}

posted @ 2017-04-10 09:57  放羊的猩猩  阅读(2228)  评论(0编辑  收藏  举报