SpringBoot接口内部从sftp服务器获取文件流实现文件下载

SpringBoot接口内部从sftp服务器获取文件流实现文件下载

    @GetMapping("/voice/file/download")
    @ApiOperation("语音文件--按id下载录音文件")
    @RequestLog(businessType = BusinessType.SELECT)
    public void downloadFile(@RequestParam("recfilename") String recfilename, HttpServletResponse response) throws IOException {
        ChannelSftp channel = (ChannelSftp) SftpUtil.initialChannel(null, sftpCtiConfiguration.getAccountSftp(), sftpCtiConfiguration.getEntranceTicketSftp(), sftpCtiConfiguration.getServerIp(), sftpCtiConfiguration.getPortSftp(), "180000");

        if (channel == null) {
            log.error(this.getClass().getSimpleName() + "#callVoiceFileList, step0:初始化连接sftp服务器失败!");
            emailUtil.sendMail("获取语音文件失败,初始化连接sftp服务器失败!请联系相关人员进行人工处理,谢谢!", "Connect sftp server failed");
            throw new BusinessException("连接cti服务器失败,请排查配置信息!");
        }
        InputStream inputStream;
        try {
            inputStream = channel.get(recfilename);
        } catch (SftpException e) {
            log.error("获取文件失败", e);
            emailUtil.sendMail("获取语音文件失败,请联系相关人员进行人工处理,谢谢!", "Get file failed");
            throw new BusinessException("获取文件失败");
        }

        String filename = recfilename.substring(recfilename.lastIndexOf("/") + 1);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
        // 对真实文件名进行百分号编码
        String percentEncodedFileName = URLEncoder.encode(filename, "utf-8")
                .replaceAll("\\+", "%20");
        // 组装contentDisposition的值
        StringBuilder contentDispositionValue = new StringBuilder();
        contentDispositionValue.append("attachment; filename=")
                .append(percentEncodedFileName)
                .append(";")
                .append("filename*=")
                .append("utf-8''")
                .append(percentEncodedFileName);
        response.setHeader("Content-disposition",
                contentDispositionValue.toString());
        response.setHeader("filename", java.net.URLEncoder.encode(filename, "UTF-8"));
        // 将文件流写到response中
        try (
             OutputStream outputStream = response.getOutputStream()) {
            IOUtils.copy(inputStream, outputStream);
        } catch (IOException e) {
            log.error("文件下载失败!", e);
            throw new BusinessException("文件下载失败!");
        }
        log.info(this.getClass().getSimpleName() + "#downloadFile(),文件下载完毕!");
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用程序中从SFTP服务器下载文件,您可以使用JSch库来实现。以下是一个简单的示例代码片段,演示了如何进行SFTP文件下载: ```java import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class SftpDownloader { private static final String HOST = "your_sftp_host"; private static final int PORT = 22; private static final String USERNAME = "your_sftp_username"; private static final String PASSWORD = "your_sftp_password"; private static final String REMOTE_FILE_PATH = "/path/to/remote/file"; private static final String LOCAL_FILE_PATH = "/path/to/local/file"; public static void downloadFile() { JSch ssh = new JSch(); Session session = null; try { session = ssh.getSession(USERNAME, HOST, PORT); session.setPassword(PASSWORD); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; InputStream inputStream = sftpChannel.get(REMOTE_FILE_PATH); OutputStream outputStream = new FileOutputStream(LOCAL_FILE_PATH); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } inputStream.close(); outputStream.close(); sftpChannel.disconnect(); channel.disconnect(); session.disconnect(); System.out.println("File downloaded successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` 请确保将`your_sftp_host`,`your_sftp_username`,`your_sftp_password`,`/path/to/remote/file`和`/path/to/local/file`替换为实际的SFTP服务器主机,用户名,密码以及远程和本地文件的路径。 以上代码将从SFTP服务器下载文件并保存在本地位置。您可以根据您的需求进行调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值