I use the SMB protocol share mode to connect a remote Windows Server can also use FTP but to ensure that the server is FTP connection Linux server can use SSH protocol
Create a new Res.properites connection
ip=10.61.28.56
Smb_mingcheng=smb://jichuzhongxin:[email protected]//server login password IP
Wenjian_name=/res.properties
Code
public class uploadfile{
Read server folder to local
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");
Folder path
Smbfile = new Smbfile (URL + "/web-based public/network system Support Center-demand/Official/") of the circuit of the joint/base station;
Folder path for backup
CopyFile = new Smbfile (URL + "/web-based public/network system Support Center-demand/backup of the circuit of the joint/Base station/");
Listfiles = Smbfile.listfiles ();
} catch (Exception e) {
E.printstacktrace ();
}
For loop gets each file
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")) {
Download to local folder path
OS = new FileOutputStream ("d:\\" + smbfile1.getname ());
Folder path for backup
so = new Smbfileoutputstream (Copyfile+smbfile1.getname ());
File name
Filenmae=smbfile1.getname (). toString ();
Name.append (filenmae+ ",");
in = new Smbfileinputstream (SMBFILE1);
SYSTEM.OUT.PRINTLN ("Exported filename" +smbfile1.getname ());
Import to Database
This.importexcel (in, Filenmae);
Hssfworkbook XWB = new Hssfworkbook (in);
INS = new Smbfileinputstream (SMBFILE1);
int len = 0;
byte[] bytes = new byte[1024];
System.out.println ("Download to local and backup to server another folder");
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 ("Backup and Export Database Success");
Call Storage
Jdbcutil jdbcutil=new jdbcutil ();
Boolean B=jdbcutil.queryall ();
System.out.println (Boo);
if (b== true) {
Export to Server
Exportshenqingdan export = new Exportshenqingdan ();
Export.exportexcel (name);
return b;
}else{
return b=false;
}
}
}
Java connection to a remote server