java根据接口方式获取linux服务器图片并返回前端(下载格式)

代码:

 /*
         * 根据参数路径获取图片并下载
         * */
        @GetMapping("/imgDownload")
        public void getPic(String ip, HttpServletResponse response, String path) {

        //服务器发送给浏览器的数据类型 (不写setHeadder直接打开)
        response.setContentType("image/png");
        //设置以下载方式打开文件
        response.setHeader("content-disposition", "attachment;filename=back.png");
        //连接linux服务器
        Connection conn = new Connection("49.92.153.134", 22);
        try {
            conn.connect();
            //输入连接密码
            boolean isAuthenticated = conn.authenticateWithPassword("root", "G1123");
            //校验密码是否正确
            if (isAuthenticated == false) {
                System.err.println("authentication failed");
            }
            //scp执行代码
            SCPClient client = new SCPClient(conn);
            client.get(path, response.getOutputStream());

            conn.close();
        } catch (IOException ex) {
            System.out.println("连接服务器失败");
        }
    }




所需依赖:

		<dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>build210</version>
        </dependency>

参考:
Javaweb学习笔记之HttpServletResponse(四):content-type 响应头的作用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Java中的ZipOutputStream类来实现将多个附件打包成Zip文件并返回前端。以下是一个简单的代码示例: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ZipServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取要打包的文件列表 String[] fileNames = request.getParameterValues("fileNames[]"); try { // 设置响应头 response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=\"archive.zip\""); // 创建ZipOutputStream ServletOutputStream out = response.getOutputStream(); ZipOutputStream zipOut = new ZipOutputStream(out); // 添加文件到ZipOutputStream for (String fileName : fileNames) { File file = new File(fileName); FileInputStream in = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(file.getName()); zipOut.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { zipOut.write(buffer, 0, len); } in.close(); zipOut.closeEntry(); } // 关闭ZipOutputStream zipOut.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在这个代码示例中,我们首先从请求中获取要打包的文件列表,然后设置响应头以告诉浏览器返回的是一个Zip文件,并将ZipOutputStream对象的输出流设置为响应的输出流。接着,我们遍历文件列表,将每个文件添加到ZipOutputStream中。最后,我们关闭ZipOutputStream以确保Zip文件正确地生成并返回前端
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值