前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springmvc 文件下载 VS resteasy 文件上传下载

springmvc 文件下载 VS resteasy 文件上传下载

作者头像
MickyInvQ
发布2020-09-27 10:22:04
9810
发布2020-09-27 10:22:04
举报
文章被收录于专栏:InvQ的专栏InvQ的专栏

直接上代码

代码语言:javascript
复制
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/evaluate")
public class EvaluateController {
    private static String templateFilePath = "D:\\temp\\templateFile.xlsx";
    private static String templateFileName = "批量导出模板.xlsx";

    @GetMapping("downloadPathExportTemplate")
    @ResponseBody
    public ResponseEntity<Object> downloadFile() throws FileNotFoundException, UnsupportedEncodingException {

        File file = new File( templateFilePath);
        InputStreamResource resource = new InputStreamResource ( new FileInputStream( file ) );

        HttpHeaders headers = new HttpHeaders();
        headers.add ( "Content-Disposition",String.format("attachment;filename=\"%s",new String(templateFileName.getBytes("GBK"), "ISO-8859-1")));
        headers.add ( "Cache-Control","no-cache,no-store,must-revalidate" );
        headers.add ( "Pragma","no-cache" );
        headers.add ( "Expires","0" );

        ResponseEntity<Object> responseEntity = ResponseEntity.ok()
                .headers ( headers )
                .contentLength ( file.length ())
                .contentType(MediaType.parseMediaType ( "application/txt" ))
                .body(resource);

        return responseEntity;
    }
}
在这里插入图片描述
在这里插入图片描述

浏览器输入: http://localhost:8080/evaluate/downloadPathExportTemplate

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

内容无变化

restEasy 文件下载如下:

https://blog.csdn.net/zzhongcy/article/details/19966965

restEasy 文件上传如下: https://blog.csdn.net/zhangzz1127/article/details/17428173

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档