java 通过网络图片得到流存入到zip流返回给前端

//请求方法加 HttpServletResponse response
    @GetMapping("/download/plan")
    public AjaxResult downLoadPlan( HttpServletResponse response) throws Exception {

}
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
List<String> pics2 = Arrays.asList("图片地址1","图片地址2");
//循环得到图片地址
 for (int i = 0; i < pics2.size(); i++) {
            String img = jsonObject.pics2.get(i);
            //得到下载链接
            String imgLinks = img.replaceAll("//", "");
            URL url3 = new URL("http://" + imgLinks);
            //得到图片的流
            InputStream stream = URLUtil.getStream(url3);
            FastByteArrayOutputStream read = IoUtil.read(stream);
            byte[] bytes = read.toByteArray();
            try {
                // 文件流添加到zip流
                zip.putNextEntry(new ZipEntry(FileUtil.getName(imgLinks)));
                zip.write(bytes);
                IOUtils.closeQuietly(read);
                zip.flush();
                zip.closeEntry();
            } catch (IOException e) {
                log.error("下载失败!", e);
            }
        }
        IOUtils.closeQuietly(zip);
        byte[] bytes = outputStream.toByteArray();
//由响应response直接输入到前端
        response.reset();
        response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("1.zip", "UTF-8"));
        response.addHeader("Content-Length", "" + bytes.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(bytes, response.getOutputStream());

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:以下是一个简单的Java接口代码示例,该接口可以将token存储在session中,然后将其返回前端: ``` @RestController public class TokenController { @PostMapping("/login") public String login(@RequestBody User user, HttpSession session) { // Check if user credentials are valid if (isValidUser(user)) { // Generate a unique token for the user String token = generateToken(user); // Save token in the session session.setAttribute("token", token); // Return the token to the client return token; } else { // Return an error message if user credentials are invalid return "Invalid credentials"; } } } ``` 在此代码示例中,我们使用了Spring Boot框架编写了一个REST接口`/login`,该接口接收一个`User`对象作为参数和一个`HttpSession`对象。在接口中,我们首先使用`isValidUser()`方法验证用户凭据是否有效。如果用户凭据有效,我们调用`generateToken()`方法生成一个令牌,并将其存储在`session`中。 最后,我们向客户端返回令牌。客户端可以使用该令牌来进行后续请求,以验证其身份。 需要注意的是,代码示例中的`isValidUser()`和`generateToken()`方法均是示例方法,需要根据实际情况进行修改或替换。此外,还需要确保在Spring Boot项目中启用`HttpSession`的支持,具体可以在配置类中添加以下代码: ``` @Configuration @EnableWebMvc @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800) public class AppConfig { // Configuration code here } ``` 其中`maxInactiveIntervalInSeconds`是会话过期时间,单位为秒。在此示例中,我们将其设置为30分钟。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值