springmvc 文件上传与下载
来源:互联网 发布:9008端口刷机工具 编辑:程序博客网 时间:2024/04/28 16:15
1. 准备jar包 : commons-fileupload-1.3.2.jar
commons-io-2.2.jar
2.
spring-mvc.xml 添加
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8"/> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000"/> <!-- 内存中的最大值 --> <property name="maxInMemorySize" value="40960"/> <!-- 上传文件的临时路径 --> <property name="uploadTempDir" value="fileUpload/temp"/> </bean>
3.controller.java
@RequestMapping("/upload") public String upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception{// 判断文件是否为空 if (!file.isEmpty()) { try { // 文件保存路径 String filePath = request.getSession().getServletContext().getRealPath("//")+"/upload/" + file.getOriginalFilename(); // 转存文件 file.transferTo(new File(filePath)); System.out.println("**********"+filePath); } catch (Exception e) { e.printStackTrace(); } } return "success"; }
4.upload.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=UTF-8"><title>Insert title here</title><link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.min.css"><link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap-theme.min.css"><script src="${pageContext.request.contextPath}/js/jquery-1.11.2.min.js"></script><script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script></head><body style="padding: 20px"> <form action="${pageContext.request.contextPath}/user/upload.do" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="file"> <input type="submit" value="提交"> </form> </body></html>
下载:
controller.java
@RequestMapping("download") public ResponseEntity<byte[]> download(String path) throws IOException { File file=new File(path); HttpHeaders headers = new HttpHeaders(); String fileName=path.substring(path.lastIndexOf("/")+1); fileName=new String(fileName.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); }
download.jsp
<a class="tooltip-test" data-toggle="tooltip" title="下载文件" href="download.do?path=${wfDeployment.path }">
阅读全文
0 0
- SpringMVC文件下载与上传
- SpringMVC文件上传与下载
- springmvc 文件上传与下载
- SpringMVC文件上传与下载
- SpringMVC文件上传与下载
- springmvc文件上传与下载
- SpringMVC文件上传与下载
- 文件上传与下载--springmvc版本上传
- ExtJS+SpringMVC文件上传与下载
- SpringMVC文件的上传与下载
- 13. SpringMVC 文件上传与下载
- SpringMVC文件的上传与下载
- springMVC文件的上传与下载
- SpringMVC---实现文件上传与下载
- SpringMVC之文件上传与下载
- SpringMVC实现文件上传与下载
- SpringMVC的文件上传与下载
- SpringMVC的文件上传与下载
- javascript 中,function 方法前面一个分号是啥意思
- struts2面试题
- HDU题目分类
- codeforces 845C Two TVs
- hdu 1401 Solitaire(双向宽搜)
- springmvc 文件上传与下载
- windows10下cuda8.0+cudnn6.0+PYthon3.5.3+tensorflow安装
- 关于post数据服务器端接收不全
- 新路程------hi3516a在menuconfig加ko编译
- TOPGP5.3:p_link增加编译、链接功能
- 定位中获取地址位置的两种方式
- vi命令模式下的一些技巧
- 【python 爬虫】全国失信被执行人名单爬虫
- android多段seekbar