Java connection to a remote server

Source: Internet
Author: User
Tags ftp connection

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.