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));
        }
    
    }
  • 相关阅读:
    [写代码]处理一组lrc歌词文件
    [ubuntu]windows重装以后,恢复grub引导
    [HDOJ1878]欧拉回路
    [写代码]解析自定义数据库文件的思路
    [写代码]wordList——百词斩CLI版
    [HDOJ2544]最短路
    [HDOJ1285] 确定比赛名次
    [HDOJ1232]畅通工程
    [HDOJ2717]Catch That Cow
    jQuery实现点击开关图片切换
  • 原文地址:https://www.cnblogs.com/xiaolei2017/p/8743763.html
Copyright © 2011-2022 走看看