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) {
}
}
}

路径问题
上传到服务器上的不能用本机地址,例如e:\jjpg这种路径的在服务器上是不能显示的。
你应该在DW中新建一个站点,这样他会自动把e:\jjpg转换成 /jjpg
先了解下“绝对路径”和“相对路径” 吧。
注意: indexhtml要和相关文件夹一起上传,不然路径对也没用。
另外要注意域名是否绑定成功
空间是否支持你所上传文件的格式
到你域名管理后台把A记录解析到你空间对应的IP就可以了一般8-24小时内生效

不太清楚 用php把直接存在数据库里 的实际效果是什么?是读取文件并保存在数据库里吗?如果在数据库库里保存的是 的路径 ,请检查服务器地址url是否正确。如果在数据库库里保存的是 文件,需要注意输出时要加上header相关内容进行编码,比如:header("Content-type: image/JPEG",true);


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

原文地址: https://outofmemory.cn/zz/12872413.html

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

发表评论

登录后才能评论

评论列表(0条)

保存