Spring mvc 下载
来源:互联网 发布:好玩的钢琴软件 编辑:程序博客网 时间:2024/06/03 20:07
Spring mvc 实现文件下载
@RequestMapping(value ="/down.action")public void down(HttpServletRequest request,HttpServletResponse response,@RequestParam("id")String id) throws IOException{// 通过前段jsp页面 传过来的id 得到对象 再查出文件下载的pathUserInfoPO po =new UserInfoPO();po.setId(id);// 上传的时候 将文件的path记录在数据库中。
po=userInfoService.query(po);String fileName =po.getFileNamePath();// 得到文件的全路径加名字if(fileName.isEmpty()){response.getWriter().write("文件不存在");}File file = new File(fileName);InputStream inputstream =new FileInputStream(file);String destFileName = file.getName();download(response, inputstream, destFileName);}
download 方法
protected void download(HttpServletResponse response,InputStream inputStream, String destFileName) throws IOException {response.setContentType("application/octet-stream");response.setCharacterEncoding("UTF-8");String downLoad = "";if (destFileName != null) {downLoad = new String(destFileName.getBytes("GBK"), "ISO-8859-1");}response.setHeader("Content-disposition", "attachment;filename=\""+ downLoad + "\"");OutputStream output = null;try {byte[] bytes = new byte[1024];output = response.getOutputStream();int byteRead;while ((byteRead = inputStream.read(bytes)) != -1) {output.write(bytes, 0, byteRead);}output.flush();}catch (Exception ex) {throw new IOException(ex.getMessage());}finally {if (inputStream != null) {try {inputStream.close();}catch (Exception e) {}}if (output != null) {try {output.close();}catch (Exception e) {}}response.flushBuffer();}}
1 0
- Spring MVC 文件下载
- Spring mvc 文件下载
- spring mvc 文件下载
- Spring MVC下载文件
- spring mvc 文件下载
- Spring mvc 文件下载
- Spring MVC文件下载
- spring mvc下载文件
- Spring mvc 下载
- Spring MVC 文件下载
- spring mvc文件下载
- spring mvc 下载
- spring-mvc框架下载
- Spring MVC PDF下载
- Spring mvc 下载文件
- spring MVC文件下载
- Spring MVC 文件下载
- spring mvc 文件的下载
- 【慕课笔记】第五章 阶段练习 第2节 针对第1节练习的优化
- iOS个人整理14-界面间传值:属性传值---代理传值---block传值---单例传值
- UML概述
- mcrypt.h not found. Please reinstall libmcrypt”的解决方法
- 使用libsvm实现文本分类(问题总结)
- Spring mvc 下载
- bzoj3894: 文理分科
- swift函数学习
- c++ 封装 (下)
- 关于ADC的采样率和UART的波特率
- 深入剖析之通过IAR MAP文件查看目标文件内存分配
- hadoop2配置文件详细解释
- jquery easyui datagrid纯js导出到excel
- 【JEECG示例文档】使用Kettle从mysql向oracle中抽取数据