我正在尝试使用Jsch
库从SFTP下载文件(文件类型没有限制)。我正在使用spring-mvc
来做同样的事情。我成功地将响应数据提供给我的AngularJS控制器。但是,下载的文件中没有任何内容。下面是我的AngularJS代码和弹簧控制器代码。
@RequestMapping(value = "/downloadAttachment", method = RequestMethod.POST,
produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void downloadAttachment(
@RequestParam(value = "fileName") String fileName,
@RequestParam(value = "filePath") String filePath,
@RequestParam(value = "fileId") String fileId,
HttpServletResponse response, Locale locale) throws Exception {
InputStream is = null;
if(!fileName.isEmpty() && !filePath.isEmpty() && !fileId.isEmpty()){
String directory = filePath;
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch ssh = new JSch();
Session session = ssh.getSession(sftpLogin, sftpAddress, 22);
session.setConfig(config);
session.setPassword(sftpPassword);
session.connect();
logger.info("Connection to server established");
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
sftp.cd(directory);
is = sftp.get(fileName);
IOUtils.copy(is, response.getOutputStream());
is.close();
channel.disconnect();
session.disconnect();
}
}
以下是我的AngularJS承诺函数。
function downloadAttachment(file){
if(file!= null){
var promise = myService.downloadAttachment(file);
promise.then(function(downloadResponseData){
if(downloadResponseData != null){
var blob = new Blob([downloadResponseData], {type: 'application/octet-stream', disposition: 'attachment'});
var objectUrl = URL.createObjectURL(blob);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = objectUrl;
a.download = row.entity._file.name;
a.click();
}
}),function(errorPayload){
}
}
}
我无法在上面的代码中找到错误。 我只需要下载文件及其内容