代码片 -文件下载

此处为代码片,演示使用 IO 流实现 File 的创建与导出。

  1. Java 后端代码 DownController.java
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.net.URLEncoder;

/**
 * @className: DownController
 * @description: 文件下载代码块
 * @author: niaonao
 * @date: 2019/9/25
 **/
@Slf4j
@RestController
@RequestMapping("/down")
public class DownController {

    @PostMapping(value = "/downloadJsonFile")
    public void downloadJsonFile(HttpServletResponse response) {

        // 准备 JSON 数据
        JSONArray tagJsonArray = JSON.parseArray("[{\"TAG_050_01\":\"购买力很低\"},{\"TAG_050_02\":\"购买力较低\"},{\"TAG_050_03\":\"购买力一般\"},{\"TAG_050_04\":\"购买力较高\"},{\"TAG_050_05\":\"购买力很高\"}]");
        String tagJsonPretty = JSON.toJSONString(tagJsonArray, SerializerFeature.PrettyFormat);
        String fileName = "TAG_LIST.json";
        String fileUrl = "doc/" + fileName;
        try {
            // (1)创建文件
            File file = new File(fileUrl);
            //创建目录;创建文件
            if(!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }

            // (2)写入数据
            FileWriter fw = new FileWriter(file);
            fw.write(tagJsonPretty);
            fw.flush();
            fw.close();

            // (3)读取文件转化为字节数组输出流
            // 字节数组输出流在内存中创建一个字节数组缓冲区,所有发送到输出流的数据保存在该字节数组缓冲区中
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            byte[] data = bos.toByteArray();
            bos.close();
            response.reset();
            // text/html   text/plain  text/javascript  application/xml  application/msexcel
            response.setContentType("application/json;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;filename= " + URLEncoder.encode(fileName, "UTF-8"));
            response.addHeader("Cache-Control", "no-cache");
            response.getOutputStream().write(data);
            response.getOutputStream().close();
        } catch (Exception e) {
            log.warn("[标签管理]:下载标签资源失败,异常捕获{} .", e.getMessage());
        }
    }
}
  1. Html普通表单下载实现
<form 
  action="http://127.0.0.1:8080/down/downloadJsonFile"
  enctype="multipart/form-data"
  method="post">
    <input type="submit" value="ExcelDown">
</form>
  1. Vue前端下载实现
    可以结合查看:模拟浏览器下载Excel 到本地
©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页