📄 ftpclientthread.java
字号:
package com.socket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.util.ResourceBundle;
import merp.sc.server.dba.common.DBA_Common;
public class FtpClientThread extends Thread {
private Socket _client = null;//socket连接
private String _serverHost = "192.1.1.72";//服务器IP
private int _serverPort = 6030;//服务器端口
private String _serverDir = "/tmp";//服务器存放文件的目录
private String _localDir = "/tmp";//本地存放文件的目录
private DataInputStream _dis;//输入信息流
private DataOutputStream _dos;//输出信息流
private FtpFileProps _file = null;//本次传输的文件名
private String _threadid = "unKnown";//线程编号
private DBA_Common _dba_Common = null;
private Connection _conn = null;
public FtpClientThread(ResourceBundle config, FtpFileProps file,
String threadid) {
try {
//读配置文件
_serverHost = config.getString("serverHost");
_serverPort = Integer.parseInt(config.getString("serverPort"));
_serverDir = config.getString("serverDir");
_localDir = config.getString("localDir");
_file = file;
_threadid = threadid;
//建立Socket链接
_client = new Socket(_serverHost, _serverPort);
_dis = new DataInputStream(new BufferedInputStream(_client
.getInputStream()));//初始化输入流
_dos = new DataOutputStream(new BufferedOutputStream(_client
.getOutputStream()));//初始化输出流
start();
} catch (Exception e) {
System.out.println("[SYS] Thread " + _threadid
+ " cann't connect to sync fileserver " + _serverHost + ":"
+ _serverPort);
}
}
// 连接数据库
private void connDBA() {
try {
if (_conn == null || _conn.isClosed()) {
_dba_Common = new DBA_Common();
_conn = _dba_Common.initDBA(null, "1");
}
_dba_Common.setConnection(_conn);
} catch (Exception e) {
System.out.println("[SYS] SMS ReplyTask._connDBA() Exception: "
+ e.toString());
}
}
//释放资源
private void freeDBA() {
try {
if (_conn != null || !_conn.isClosed()) {
_dba_Common.closeDBA(_conn);
_conn = null;
_dba_Common = null;
}
} catch (Exception e) {
System.out.println("[SYS] SMS ReplyTask.freeDBA() Exception: "
+ e.toString());
}
}
public int updateStatus(String fileid) {
connDBA();
String strSql = "UPDATE JCMS_SYNC_ATTACH SET B_ISSYNC=1 WHERE I_ID="
+ fileid;
int res = _dba_Common.submitSqlToDB(null, strSql);
connDBA();
return res;
}
public void run() {
try {
//发送命令
String cmd = "GET " + _serverDir + "/" + _file.getFilename();
_dos.writeBytes(cmd + "\n");
_dos.flush();
//创建文件
String localfile = _localDir + "/" + _file.getFilename();
System.out.println("[SYS] Thread " + _threadid + " \"" + cmd
+ "\" TO \"" + localfile + "\"");
File file = new File(localfile);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
//保存内容
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[2048];
int num = 0;
while ((num = _dis.read(buf)) > -1) {//是否读完文件
fos.write(buf, 0, num);//把文件数据写出网络缓冲区
fos.flush();//刷新缓冲区把数据写往客户端
}
fos.flush();
fos.close();
if (Long.toString(file.length()).equals(_file.getFilesize())) {
updateStatus(_file.getFileid());
System.out.println("[SYS] Thread " + _threadid + " save \""
+ localfile + "\" OK.");
} else {
file.delete();
System.out.println("[SYS] Thread " + _threadid + " save \""
+ localfile + "\" occur error,deleted.");
}
} catch (Exception e) {
System.out.println("[SYS] Thread " + _threadid + " run fail.");
e.printStackTrace();
} finally {
try {
_dis.close();
_dos.close();
_client.close();
} catch (Exception ex) {
System.out.println("[SYS] Thread " + _threadid
+ " close stream or socket fail.");
ex.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -