下载resources目录下文件

下载resources目录下文件

目录结构

在这里插入图片描述

代码
    public void downLoadTemplate(String type, HttpServletResponse response, HttpServletRequest request) {
        String fileName = "";
        InputStream in = null;
        if (type.equals("1")) {
        fileName = "终端内置信息导入模板.xls";
            in = this.getClass().getClassLoader().getResourceAsStream("template/" + fileName);
            
        } else if (type.equals("2")) {
            fileName = "终端实时数据导入模板.xls";
            in = this.getClass().getClassLoader().getResourceAsStream("template/" + "fileName");
           
        }

        getFileContent(in, fileName, response, request);

    }
public void getFileContent(InputStream inputStream, String name, HttpServletResponse response, HttpServletRequest request) {
       	// 清空response
        response.reset();
        try {
            InputStream in = inputStream;
            response.setHeader("Content-Length", String.valueOf(in.available()));
            byte[] buffer = new byte[in.available()];
            //不加打开会文件损坏
            in.read(buffer);
            in.close();
            String userAgent = request.getHeader("User-Agent");
            String fileName;
            if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
                fileName = URLEncoder.encode(name, "UTF-8");
            } else {
                // 非IE浏览器的处理:
                fileName = new String(name.getBytes("UTF-8"), "ISO-8859-1");
            }
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
是的,SpringBoot可以提供接口让前端下载后端`resources`目录下的文件。可以通过以下步骤实现: 1. 在`application.properties`或`application.yml`中配置`spring.resources.static-locations`属性,指定资源文件的路径,例如: ``` spring.resources.static-locations=classpath:/static/,file:/path/to/resources/ ``` 这里配置了两个路径,一个是`classpath:/static/`,表示在`src/main/resources/static/`目录下查找静态资源;另一个是`file:/path/to/resources/`,表示在指定的`/path/to/resources/`目录下查找资源。 2. 创建一个Controller,用于处理下载请求,例如: ```java @RestController public class DownloadController { @GetMapping("/download") public ResponseEntity<Resource> downloadFile() throws IOException { Resource resource = new ClassPathResource("/static/test.txt"); HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.txt"); return ResponseEntity.ok() .headers(headers) .contentLength(resource.contentLength()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(resource); } } ``` 这里假设要下载文件为`test.txt`,位于`src/main/resources/static/test.txt`。 3. 在前端页面中添加下载链接,例如: ```html <a href="/download">Download</a> ``` 当用户点击该链接时,会向后端发送一个GET请求,后端会返回一个`test.txt`文件供用户下载。 注意:在实际开发中,需要根据具体需求进行配置和实现。此处只是提供一个示例,具体实现方式可能会有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W先生'

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值