vue⽂件下载,java读取SFTP⽂件通过流的⽅式输出到前端java后台Controller层代码
@RequestMapping("downloadFile")
public void downLoadFile(HttpServletRequest req, HttpServletResponse response) {
String fileName = Parameter("fileName");
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
byte[] buffer = new byte[1024 * 10];
InputStream fis = null;
BufferedInputStream bis = null;
SFTPUtil sftp = null;
try {
sftp = new SFTPUtil(FtpUserName, FtpPassword, FtpServer,FtpPort);
sftp.login();
if (!sftp.RemoteDir(), fileName)) {
throw new ServiceException("回单⽂件已被清理,请重新回单申请");
}
fis = sftp.RemoteDir() + fileName);
bis = new BufferedInputStream(fis);
ServletOutputStream out = OutputStream();
//读取⽂件流
int len = 0;
while ((len = ad(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} catch (Exception e) {
<(e);
} finally {
sftp.logout();
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
bis = null;
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
fis = null;
}
}
}
}
SFTP⼯具类
public class SFTPUtil {
private transient Logger log = Class());
private ChannelSftp sftp;
private Session session;
/**
* SFTP 登录⽤户名
*/
private String username;
/**
* SFTP 登录密码
*/
private String password;
private String password;
/**
* 私钥
*/
private String privateKey;
/**
* SFTP 服务器地址IP地址
*/
private String host;
/**
* SFTP 端⼝
*/
private int port;
/**
* 构造基于密码认证的sftp对象
*/
public SFTPUtil(String username, String password, String host, int port) {        this.username = username;
this.password = password;
this.host = host;
this.port = port;
}
/**
* 构造基于秘钥认证的sftp对象
*/
public SFTPUtil(String username, String host, int port, String privateKey) {        this.username = username;
this.host = host;
this.port = port;
this.privateKey = privateKey;
}
public SFTPUtil() {
}
/**
* 连接sftp服务器
*/
public void login() {
try {
JSch jsch = new JSch();
if (privateKey != null) {
jsch.addIdentity(privateKey);// 设置私钥
}
session = Session(username, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
Channel channel = session.openChannel("sftp");
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
}
}
/**
* 关闭连接 server
*/
public void logout() {
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
public boolean isExist(String serverPath, String folderName) {
try {
sftp.cd(serverPath);
/
/ 判断⼦⽬录⽂件夹是否存在,不存在即创建
SftpATTRS attrs = null;
attrs = sftp.stat(folderName);
if (attrs != null) {
return true;
}
} catch (Exception e) {
return false;
}
return false;
}
public boolean isConnect() {
if (null != session) {
return session.isConnected();
}
return false;
}
public InputStream download(String directory) throws SftpException {
(directory);
}
前端vue部分代码
window.v.BASE_API+"/member/downloadFile?fileName="+fileName ; v.BASE_API+"/member/downloadFile?fileName="+fileName :服务端⽂件地址v.BASE_API :服务端IP地址
sult:服务端返回的⽂件名
receiptDownLoad(this.listQuery).then(response =>{
if(de ===1){
window.v.BASE_API+"/member/downloadFile?fileName="+sult;
}else{
this.$message({title:'失败', message: sult, type:'error', duration:2000});
}
this.listLoading =false;
});
前端页⾯显⽰结果:
之前是通过读取本地⽂件然后通过流的⽅式输出,后⾯因不需要存储本地就改为直接读sftp 读取本地⽂件代码:
String fileName = Parameter("fileName");
File file = new File(LocalDir+File.separator+fileName);
if (ists()) {
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
byte[] buffer = new byte[1024 * 10];
FileInputStream fis = null;
BufferedInputStream bis = null;
ServletOutputStream out = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
out = OutputStream();
//读取⽂件流
int len = 0;
while ((len = ad(buffer)) != -1) {
connect下载
out.write(buffer, 0, len);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
bis = null;
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
fis = null;
}
}
}
}
该⽅法返回的是⼀个InputStream可以直接读;
fis = sftp.RemoteDir()+ fileName);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。