spring mvc 文件下载、

 /**前台传过来一个文件名*/
    @RequestMapping("/download")
    public ResponseEntity<Resource> export(@RequestParam("strZipPath") String strZipPath) throws IOException {

        //filepath 为视频的的路径
        //strZipPath 为视频的名字
        return download(new File(filepath + "//" + strZipPath));
    }
/**
     * 下载文件
     * @param file 文件
     */
    protected ResponseEntity<Resource> download(File file) {
        String fileName = file.getName();
        return download(file, fileName);
    }

    /**
     * 下载
     * @param file 文件
     * @param fileName 生成的文件名
     * @return {ResponseEntity}
     */
    protected ResponseEntity<Resource> download(File file, String fileName) {
        Resource resource = new FileSystemResource(file);

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String header = request.getHeader("User-Agent");
        // 避免空指针
        header = header == null ? "" : header.toUpperCase();
        HttpStatus status;
        if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
            fileName = URLUtils.encodeURL(fileName, Charsets.UTF_8);
            status = HttpStatus.OK;
        } else {
            fileName = new String(fileName.getBytes(Charsets.UTF_8), Charsets.ISO_8859_1);
            status = HttpStatus.CREATED;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", fileName);
        return new ResponseEntity<Resource>(resource, headers, status);
    }

文件下载最好用form表单提交,不要用ajax 提交,因为ajax处理起来很麻烦,如果你想用ajax 可以参考

https://my.oschina.net/watcher/blog/1525962

ps:后台代码都是一样的,就是前台改一下

原文地址:https://www.cnblogs.com/liouzeshuen/p/10605615.html

时间: 2024-07-31 11:54:47

spring mvc 文件下载、的相关文章

Spring MVC 文件下载时候 发现IE不支持

@RequestMapping("download") public ResponseEntity<byte[]> download(Long fileKey) throws IOException { HttpHeaders headers = new HttpHeaders(); String fileName=new String(massMessage.getFileName().getBytes("UTF-8"),"iso-8859-

Spring MVC文件下载

方案一: // 文件下载 @RequestMapping(value = "/downloadFile") public ResponseEntity<byte[]> downloadFile() throws IOException { String basePath = "F:/testDir/"; String fileName = "ChromeStandaloneV45.0.2454.101.exe"; HttpHeader

spring mvc 文件下载

在controller中进行代码编写: @RequestMapping("/download") public ResponseEntity<byte[]> download(HttpServletRequest req) throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); //解决中

Spring MVC 的文件下载

在看Spring MVC文件下载之前请先看Spring MVC文件上传 地址:http://www.cnblogs.com/dj-blog/p/7535101.html 文件下载比较简单,在超链接中指定文件下载的文件名就可以了. springMVC提供了一个ResponseEntity类型,可以方便的定义返回的HttpHeads和HttpStatus. 在FileUploadController中加入下面这个controller @RequestMapping("/download")

Spring MVC 4 文件下载实例(带源码)

[本系列其他教程正在陆续翻译中,点击分类:spring 4 mvc 进行查看.源码下载地址在文章末尾.] [翻译 by 明明如月 QQ 605283073] 原文地址:http://websystique.com/springmvc/spring-mvc-4-file-download-example/ 上一篇:Spring MVC 4 使用常规的fileupload上传文件(带源码) 本文将为你展示通过Spring MVC 4实现文件下载. 下载一个文件比较简单,主要包括下面几个步骤. 创建下

Spring MVC文件上传和下载

在Spring MVC中有两种实现上传文件的办法,第一种是Servlet3.0以下的版本通过commons-fileupload与commons-io完成的通用上传,第二种是Servlet3.0以上的版本的Spring内置标准上传,不需借助第3方组件.通用上传也兼容Servlet3.0以上的版本 Servlet3.0以下的通过commons-fileupload上传 1.添加上传依赖包 一个是文件上传的jar包,一个是其所依赖的IO包.这两个jar包,均在Spring支持库的org.apache

Http请求中Content-Type讲解以及在Spring MVC中的应用

引言: 在Http请求中,我们每天都在使用Content-type来指定不同格式的请求信息,但是却很少有人去全面了解content-type中允许的值有多少,这里将讲解Content-Type的可用值,以及在Spring MVC中如何使用它们来映射请求信息. 1.  Content-Type MediaType,即是Internet Media Type,互联网媒体类型:也叫做MIME类型,在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息. [html] vie

Spring MVC 4使用Servlet 3 MultiPartConfigElement实现文件上传(带源码)

[本系列其他教程正在陆续翻译中,点击分类:spring 4 mvc 进行查看.源码下载地址在文章末尾.] [翻译 by 明明如月 QQ 605283073] 原文地址:http://websystique.com/springmvc/spring-mvc-4-file-upload-example-using-multipartconfigelement/ 上一篇:Spring MVC 4 使用常规的fileupload上传文件(带源码) 下一篇:Spring MVC 4 文件下载实例(带源码)

spring mvc velocity多视图

1.ViewResolverUrlBasedViewResolver 这个东西是根据url 进行路由的.网上搜了 1.order 排序,同名出现各种问题 2.XmlViewResolver,BeanNameViewResolver,ResourceBundleViewResolver 这个 根据配置文件去找不同的view 乱码...莫名,而且配置的起来比较麻烦,好处么,就是一个配置文件基本搞定所有页面位置 乱码据说WebApplicationContext 中可以设置某弄过 3.自己写个View