ssm 中文件的上传下载
来源:互联网 发布:java用什么软件编写 编辑:程序博客网 时间:2024/05/22 02:12
版权声明:本文为博主原创文章,未经博主允许不得转载。
springmvc文件上传下载实现起来非常简单,此springmvc上传下载案例适合已经搭建好的ssm框架(spring+springmvc+mybatis)使用,ssm框架项目的搭建我相信你们已经搭建好了,这里不再赘述,下面就开始吧!
ssm框架整合详情请看:http://www.tpyyes.com/a/javaweb/2016/1103/23.html
1.首先我们创建一个测试用的jsp页面,代码如下。
- <%@ page language="java" contentType="text/html; charset=utf-8"
- pageEncoding="utf-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>文件上传下载</title>
- </head>
- <body>
- <form action="http://localhost:8080/uploadDemo/rest/file/upload" method="post" enctype="multipart/form-data">
- 选择文件:<input type="file" name="file" width="120px">
- <input type="submit" value="上传">
- </form>
- <hr>
- <form action="http://localhost:8080/uploadDemo/rest/file/down" method="get">
- <input type="submit" value="下载">
- </form>
- </body>
- </html>
- <!-- 文件上传 -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3</version>
- </dependency>
- <!-- 定义文件解释器 -->
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!-- 设置默认编码 -->
- <property name="defaultEncoding" value="utf-8"></property>
- <!-- 上传图片最大大小5M-->
- <property name="maxUploadSize" value="5242440"></property>
- </bean>
- package com.baidu;
- @RequestMapping("file")
- @Controller
- public class FileController {
- /**
- * 文件上传功能
- * @param file
- * @return
- * @throws IOException
- */
- @RequestMapping(value="/upload",method=RequestMethod.POST)
- @ResponseBody
- public String upload(MultipartFile file,HttpServletRequest request) throws IOException{
- String path = request.getSession().getServletContext().getRealPath("upload");
- String fileName = file.getOriginalFilename();
//如果更改文件的名称:
String originalFilename = file.getOriginalFilename();String fileType = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);String fileName = System.currentTimeMillis() + "." + fileType;以时间锉为文件名
//- File dir = new File(path,fileName);
- if(!dir.exists()){
- dir.mkdirs();
- }
- //MultipartFile自带的解析方法
- file.transferTo(dir);
- return "ok!";
- }
- /**
- * 文件下载功能
- * @param request
- * @param response
- * @throws Exception
- */
- @RequestMapping("/down")
- public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{
- //模拟文件,myfile.txt为需要下载的文件
- String fileName = request.getSession().getServletContext().getRealPath("upload")+"/myfile.txt";
- //获取输入流
- InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
- //假如以中文名下载的话
- String filename = "下载文件.txt";
- //转码,免得文件名中文乱码
- filename = URLEncoder.encode(filename,"UTF-8");
- //设置文件下载头
- response.addHeader("Content-Disposition", "attachment;filename=" + filename);
- //1.设置文件ContentType类型,这样设置,会自动判断下载文件类型
- response.setContentType("multipart/form-data");
- BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
- int len = 0;
- while((len = bis.read()) != -1){
- out.write(len);
- out.flush();
- }
- out.close();
- }
- }
阅读全文
0 0
- ssm 中文件的上传下载
- Flex中文件的上传下载
- Flex中文件的上传下载
- ASP.NET 中文件的上传下载
- iOS中文件的上传下载功能
- SpringMVC中文件上传下载
- struts2中文件上传下载
- JAVA中文件上传下载
- web中文件上传下载
- javaweb中文件上传下载示例
- Struts 2 中文件上传下载
- jspsmart上传下载中文件中文乱码处理
- ASP.NET中文件上传下载方法集合
- ASP.NET中文件上传下载方法集合
- ASP.NET中文件上传下载方法集合
- ASP.NET中文件上传下载方法集合
- ASP.NET中文件上传下载方法集合
- ASP.NET中文件上传下载方法集合
- js闭包
- oracle行转列
- Linker feedback
- Intent和IntentFilter的简单介绍
- spring中解决CROS跨域请求的方案
- ssm 中文件的上传下载
- 2017西安交大ACM小学期 选择困难症[折半枚举+二分查找]
- JDK环境变量配置方法
- OpenCV3 Python语言实现 笔记3
- mysql的大量的sleep进程解决办法
- R 语言 调用自己写的其他函数(.R文档间调用)
- python之字典和函数参数
- struts.xml中action与forward的重要属性
- PHP中9大缓存技术总结