基于SpringMVC的文件上传和下载
来源:互联网 发布:jquery接收json数据 编辑:程序博客网 时间:2024/04/26 17:28
一 文件上传和下载需要的jar包
二 文件的上传:
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>陈二狗的妖孽人生</title> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.9.1.js"></script> </head> <body> <h1>SpringMVCDemo:${message}</h1> <!-- <script type="text/javascript"> $(document).ready(function() {alert("陈二狗的妖孽人生");}); </script> --> <form action="${pageContext.request.contextPath}/HelloWorld/uploadFile" method="POST" enctype="multipart/form-data"> <h2>文件上传</h2> 文件:<input type="file" name="uploadFile"/><br><br> <input type="submit" value="上传"> <a href="${pageContext.request.contextPath }/HelloWorld/fileDownLoad?123.jpg">下载</a> </form> </body></html>Controller里面的文件上传的方法:
/** * 文件上传 * @param uploadFile * @param session * @return * @throws IOException * @throws IllegalStateException */@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)public String uploadFile(MultipartFile uploadFile, HttpSession session) throws Exception{// 获取文件名称作为保存在服务器的文件的名称String filename = uploadFile.getOriginalFilename();// 前半部分路径, 目录,将WebRoot下一个名称为images文件夹 转换成绝对路径String leftPath = session.getServletContext().getRealPath("/images");// 进行路径拼接 = 前半部分路径 + 文件名称File file = new File(leftPath, filename);// 如果上传文件目录不存在 自动创建该文件夹if (!file.exists()){file.mkdirs();}uploadFile.transferTo(file);return "loadFileSuccess";}spingMVC配置文件配置如下:
<!-- 配置文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="50000000"></property> <!-- <property name="maxUploadSizePerFile" value="5000000"></property> --> <property name="defaultEncoding" value="utf-8"></property> </bean>
三 文件的下载
Controller里面的方法:
/** * 文件下载 * @return * @throws Exception */@RequestMapping(value = "/fileDownLoad", method = RequestMethod.GET)public ResponseEntity<byte[]> fileDownLoad()throws Exception{File file = new File("E:\\相册\\微博\\123.jpg");HttpHeaders headers = new HttpHeaders();String fileName = new String("123.jpg".getBytes("utf-8"), "iso-8859-1"); // 解决中文乱码问题headers.setContentDispositionFormData("attachment", fileName);headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);}
0 0
- 基于SpringMVC的文件上传和下载
- 基于springmvc的文件上传与下载
- 基于SpringMVC的上传和下载
- springMVC实现文件的上传和下载
- SpringMvc的文件上传和下载
- SpringMVC实现文件的上传和下载
- SpringMVC文件上传和下载
- SpringMvc文件上传和下载
- springmvc文件上传和下载
- SpringMVC文件上传和下载
- Springmvc文件上传和下载
- springMVC文件上传和下载
- SpringMvc文件上传和下载
- springMVC上传和下载文件
- springmvc学习记录----springmvc的文件上传和下载
- 基于java的文件上传和下载
- 基于SpringMVC的文件上传
- 基于SpringMVC的上传文件
- sudo命令速度慢解决方法
- CentOS系统下的数据盘挂载
- 4.6
- jvm垃圾回收学习总结2
- SpringBoot整合MyBatis
- 基于SpringMVC的文件上传和下载
- Python中亲和度分析时defaultdict的一种典型算法
- flaskweb开发这本书中的邮箱问题!
- Python函数式编程
- java 蓝桥杯 字串统计
- 算法提高 01背包 蓝桥杯训练
- Java中的this用法
- python scrapy 视频地址
- Machine Learning with Scikit-Learn and Tensorflow 7.9 Gradient Boosting