首先强调,需要下载的文件只能放在项目中的webapp下

1、页面的一个超链接,链接到controller

<a href="<%=path%>/download">点击下载文件</a>

2、controller中的代码:

@RequestMapping("/download")
@ResponseBody
public void downLoadExcelModel(HttpServletRequest request,HttpServletResponse response) throws Exception {
String download = request.getSession().getServletContext().getRealPath("/upload/"); //获取下载路劲
ExcelAndCsvDownload.downLoadFile(moban.csv,csv,download, response);//依次传入需要下载的文件名,文件格式,路径,response参数
}

3、工具类:

package com.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletResponse;
public class ExcelAndCsvDownload{
public static boolean downLoadFile(String name,String type,String path,HttpServletResponse response)
throws Exception {
String fileName = name;
String fileType = type;
File file = new File(path+fileName); //根据文件路径获得File文件
//设置文件类型(这样设置就不止是下Excel文件了,一举多得)
if("pdf".equals(fileType)){
response.setContentType("application/pdf;charset=GBK");
}else if("csv".equals(fileType)){
response.setContentType("application/msexcel;charset=GBK");
}else if("doc".equals(fileType)){
response.setContentType("application/msword;charset=GBK");
}else if("xls".equals(fileType)){
response.setContentType("application/msexcel;charset=GBK");
}
//文件名
response.setHeader("Content-Disposition", "attachment;filename=\""
+ new String(fileName.getBytes(), "ISO8859-1") + "\"");
response.setContentLength((int) file.length());
byte[] buffer = new byte[4096];// 缓冲区
BufferedOutputStream output = null;
BufferedInputStream input = null;
try {
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file));
int n = -1;
//遍历,开始下载
while ((n = input.read(buffer, 0, 4096)) > -1) {
output.write(buffer, 0, n);
}
output.flush(); //不可少
response.flushBuffer();//不可少
} catch (Exception e) {
//异常自己捕捉
} finally {
//关闭流,不可少
if (input != null)
input.close();
if (output != null)
output.close();
}
return false;
}
}

SpringMvc之java文件下载的更多相关文章

  1. JAVA文件下载功能问题解决日志

    今天给报告系统做了个下载功能,遇到了挺多问题,通过查资料一一解决了. 1.首先遇到的问题是:java后台的输出流输出之后,没有任何报错,浏览器端不弹出保存文件的对话框,原本是ajax请求到后台的con ...

  2. IDEA+Tomcat+Maven+SpringMVC基于Java注解配置web工程

    1.在IDEA中新建Maven工程,使用archetype. 2.添加Maven依赖 <dependencies> <dependency> <groupId>ju ...

  3. 关于java文件下载文件名乱码问题解决方案

    JAVA文件下载时乱码有两种情况: 1,下载时中文文件名乱码 2,下载时因为路径中包含中文文件名乱码,提示找不到文件 解决方法见下面部分代码 response.setContentType(" ...

  4. idea调试springmvc出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    idea调试springmvc出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderList ...

  5. Java 文件下载工具类

    Java 文件下载工具类 import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static Logger logger = ...

  6. springmvc中使用文件下载功能

    项目代码:https://github.com/PeiranZhang/springmvc-fileupload 使用文件下载步骤 对请求处理方法使用void或null作为返回类型,并在方法中添加Ht ...

  7. SpringMVC的Java API(五)

    1. HttpMessageConverter消息转换器 (1) HttpMessageConverter接口源码: public interface HttpMessageConverter< ...

  8. springmvc基于java配置的实现

    该案例的github地址:https://github.com/zhouyanger/demo/tree/master/springmvc-noxml-demo 1.介绍 之前搭建SpringMvc项 ...

  9. java文件下载,上传,解压方法

    1.文件下载(亲测可用) private static final int BUFFER = 2 * 1024;// 缓冲区大小(2k)private boolean isSuccess = true ...

随机推荐

  1. APP模板框架

    HTML页面 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF- ...

  2. 关于mac地址的一点感想

    因为怕mac地址冲突导致环路影响,所以修改了本地设备的mac地址.地址修改为 77:77:77:00:22:11, 结果导致 wlan0 下发不下来. 查看配置选项/etc/config/wirele ...

  3. Swift中GCD与NSOperation相关

    GCD Swift 3必看:从使用场景了解GCD新API 常用写法: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_ ...

  4. 用 monitorix 开启linux图形化监控

    # yum install monitorix # service monitorix starthttp://host:8080/monitorix/

  5. Scala内部类

    注意:Java内部类从属于外部类,而Scala内部类从属于对象(外部类的实例本身).

  6. swift 2 选择头像图片

    一句话选择单个头像图片 新建ImagePickerViewController类: /* let imagePicker = ImagePickerViewController() imagePick ...

  7. ecshop 后台添加新的设置

    1.ecs_shop_config 表添加新的值. 2.language/zh_cn/admin/shop_config.php 设置字段后台字段名和提示信息($_LANG['cfg_name'][' ...

  8. CVE-2014-1767 利用分析(2015.2)

    CVE-2014-1767利用分析 参考这篇文章利用思路,重现利用,主要说明自己在实现的时候遇到的坑. 利用思路 1. 第一次 IoControl,释放 MDL,我们通过 VirtualAddress ...

  9. 连续多个git提交发生了冲突时

    git checkout -b test 创建并切换到分支test git clone git branch master git merge test 合并test到master (git merg ...

  10. 新建HomeController控制器 继承BaseController

    CRC这个缩略词,在我看来可以有两种理解:一种是Cyclic Redundancy Check,即循环冗余检错技术:另一种则是Cyclic Redundance Code,即循环冗余校检码.在计算机网 ...