java 服务器与客户端的文件传输

java 服务器与客户端的文件传输,第1张

可以直接通过流的形式上传或者下载。
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javautilProperties;
import comjcraftjschChannel;
import comjcraftjschChannelSftp;
import comjcraftjschJSch;
import comjcraftjschSession;
import comjcraftjschSftpException;
import hkrtb2bviewutilLog;
import javautilVector;
import znccfccbutilCCFCCBUtil;
/
/
public class CCFCCBSftp {
/
连接sftp服务器

@return
/
public static ChannelSftp connect() {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jschgetSession(CCFCCBUtilCCFCCBHOSTNAME, CCFCCBUtilCCFCCBHOSTNAME, 22);
Session sshSession = jschgetSession(CCFCCBUtilCCFCCBLOGINNAME, CCFCCBUtilCCFCCBHOSTNAME, 22);
Systemoutprintln("Session created");
sshSessionsetPassword(CCFCCBUtilCCFCCBLOGINPASSWORD);
Properties sshConfig = new Properties();
sshConfigput("StrictHostKeyChecking", "no");
sshSessionsetConfig(sshConfig);
sshSessionconnect();
Systemoutprintln("Session connected");
Systemoutprintln("Opening Channel");
Channel channel = sshSessionopenChannel("sftp");
channelconnect();
sftp = (ChannelSftp) channel;
Systemoutprintln("Connected to " + CCFCCBUtilCCFCCBHOSTNAME + "");
} catch (Exception e) {
}
return sftp;
}
/
连接sftp服务器

@param host 主机
@param port 端口
@param username 用户名
@param password 密码
@return
/
public static ChannelSftp connect(String host, int port, String username,
String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jschgetSession(CCFCCBUtilCCFCCBHOSTNAME, CCFCCBUtilCCFCCBHOSTNAME, 22);
Session sshSession = jschgetSession(CCFCCBUtilCCFCCBLOGINNAME, host, port);
Systemoutprintln("Session created");
sshSessionsetPassword(CCFCCBUtilCCFCCBLOGINPASSWORD);
Properties sshConfig = new Properties();
sshConfigput("StrictHostKeyChecking", "no");
sshSessionsetConfig(sshConfig);
sshSessionconnect();
Systemoutprintln("Session connected");
Systemoutprintln("Opening Channel");
Channel channel = sshSessionopenChannel("sftp");
channelconnect();
sftp = (ChannelSftp) channel;
Systemoutprintln("Connected to " + host + "");
} catch (Exception e) {
}
return sftp;
}
/
上传文件

@param directory 上传的目录
@param uploadFile 要上传的文件
@param sftp
/
public void upload(String directory, String uploadFile, ChannelSftp sftp) {
try {
sftpcd(directory);
File file = new File(uploadFile);
sftpput(new FileInputStream(file), filegetName());
} catch (Exception e) {
eprintStackTrace();
}
}
/
下载文件

@param directory 下载目录
@param downloadFile 下载的文件
@param saveFile 存在本地的路径
@param sftp
@return
/
public static String download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {
try {
sftpcd(directory);
File file = new File(saveFile);
FileOutputStream fos = new FileOutputStream(file);
sftpget(downloadFile, fos);
sftpdisconnect();
fosclose();
} catch (Exception e) {
Loginfo("下载文件过程出错:" + egetMessage());
return "false";
}

return "true";
}
/
删除文件

@param directory 要删除文件所在目录
@param deleteFile 要删除的文件
@param sftp
/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftpcd(directory);
sftprm(deleteFile);
} catch (Exception e) {
}
}
/
列出目录下的文件

@param directory 要列出的目录
@param sftp
@return
@throws SftpException
/
public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException {
return sftpls(directory);
}
public static void main(String[] args) {
CCFCCBSftp sf = new CCFCCBSftp();
String host = CCFCCBUtilCCFCCBHOSTNAME;
int port = 22;
String username = CCFCCBUtilCCFCCBLOGINNAME;
String password = CCFCCBUtilCCFCCBLOGINPASSWORD;
String directory = "/ccfccb/904999900099/download/";
//String uploadFile = "D:\\tmp\\uploadtxt";
String downloadFile = "CCF_904999900099_20150317_0001zip";
String saveFile = CCFCCBUtilCCFCCBUploadFilePath + "//" + "CCF_904999900099_20150317_0001zip";
//String deleteFile = "deletetxt";
ChannelSftp sftp = CCFCCBSftpconnect(host, port, username, password);
//sfupload(directory, uploadFile, sftp);
CCFCCBSftpdownload(directory, downloadFile, saveFile, sftp);
//sfdelete(directory, deleteFile, sftp);
try {
sftpcd(directory);
// sftpmkdir("ss");
Systemoutprintln("finished");
} catch (Exception e) {
}
}
}

Web文件上传采用POST的方式,与POST提交表单不同的是,上传文件需要设置FORM的enctype属性为multipart/form-data由于上传的文件会比较大,因此需要设置该参数指定浏览器使用二进制上传。如果不设置,enctype属性默认为application/x->用socket代码传啊。
如果使用的是servlet服务器技术,直接用重定向传个你的URL就行。
如果是C/S结构的程序。
就是使用socket传了。
原理差不多,服务器开个SocketServer监听
客户端用Socket连接。
然后拿到SocketgetInputStream(),拿到读入或写出流然后传就可以了。
类似于管道流,代码很好写。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/13454796.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-08-10
下一篇 2023-08-10

发表评论

登录后才能评论

评论列表(0条)

保存