zoukankan      html  css  js  c++  java
  • spring-boot下载

    // Spring-boot 下载
    
    package com.csf.executor.word.controller;
    
    import com.aug3.sys.rs.response.RespObj;
    import com.csf.executor.word.common.enums.DICTTYPE;
    import com.csf.executor.word.service.WordService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.File;
    import java.io.IOException;
    
    @RestController
    @RequestMapping("/word")
    public class WordController extends BaseController {
        private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);
        @Autowired
        private WordService wordService;
    
        @GetMapping("/record/list")
        public RespObj listRecord() {
            return build(wordService.listRecord());
        }
    
        @GetMapping("/download")
        public ResponseEntity download(String type) {
            LOGGER.info("Param type is {}", type);
            if (!DICTTYPE.contains(type)) {
                LOGGER.error("Param type is invalid");
                return null;
            }
            File file = null;
            try {
                file = wordService.getDictFile(type);  // zip包
            } catch (Exception e) {
                LOGGER.error("Download zip file is error:", e);
                return null;
            }
            HttpHeaders headers = new HttpHeaders();
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            String fileName = "dict_db_" + System.currentTimeMillis() + ".zip";
            headers.add("Content-Disposition", "attachment; filename=" + fileName);
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");
            return ResponseEntity
                    .ok()
                    .headers(headers)
                    .contentLength(file.length())
                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    .body(new FileSystemResource(file));
        }
    
    }
  • 相关阅读:
    leetCode 61.Rotate List (旋转链表) 解题思路和方法
    aar格式
    hadoop生态系统学习之路(六)hive的简单使用
    centOS 7中上网以及网卡的一些设置
    Codeforces 223C Partial Sums 数论+组合数学
    项目管理:怎样让例会高效
    Web用户的身份验证及WebApi权限验证流程的设计和实现
    IIS7 经典模式和集成模式的区别分析
    JS实现密码加密
    discuz !NT 3.5 论坛整合 .net 网站用户登录,退出
  • 原文地址:https://www.cnblogs.com/xiaolei2017/p/8743763.html
Copyright © 2011-2022 走看看