springmvc文件上传和文件下载
来源:互联网 发布:云计算一般薪资多少 编辑:程序博客网 时间:2024/05/21 18:10
1.首先springMVC的配置文件:
<!-- 配置 MultipartResolver 文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="1024000000"/> </bean> <!-- SpringMVC在超出上传文件限制时,会抛出org.springframework.web.multipart.MaxUploadSizeExceededException --> <!-- 该异常是SpringMVC在检查上传的文件信息时抛出来的,而且此时还没有进入到Controller方法中 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <!-- 遇到MaxUploadSizeExceededException异常时,自动跳转到/WEB-INF/jsp/error_fileupload.jsp页面 --> <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop> </props> </property> </bean>
导需要的入包
<!-- commons包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
2.前端页面
<form name="form4" action="/fileupload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="upload"/></form>
3.后端接收代码:
/** * 上传文件 */ @RequestMapping(value = "/fileupload",method = RequestMethod.POST) public ModelAndView fileUpload(@RequestParam(value = "file") MultipartFile multipartFile) throws IOException { String tempPath = "d://temp" + File.separator; File dir = new File(tempPath); if (!dir.exists()) { dir.mkdirs(); } String storageFilePath = null;//文件路径 String originalFilename = null;//原文件 if (multipartFile.isEmpty()) { throw new RuntimeException("文件不存在"); } else { originalFilename = multipartFile.getOriginalFilename(); if (null != originalFilename && originalFilename.length() > 0) { storageFilePath = tempPath + originalFilename; try { Files.copy(multipartFile.getInputStream(), Paths.get(storageFilePath)); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("文件上传失败,请重试!!"); } } } return new ModelAndView("success"); }4.后端下载代码:
/** * 文件下载 * @param response * @throws Exception */ @RequestMapping(value = "/downFile") public void downFile(HttpServletResponse response)throws Exception{ String pdfPath = "d:"+File.separator+ "temp" + File.separator + "aa.pdf"; response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=aa.pdf"); ServletOutputStream output = response.getOutputStream(); BufferedOutputStream bufferOut = new BufferedOutputStream(output); InputStream inputStream = new FileInputStream(pdfPath); byte[] buffer = new byte[5 * 1024]; int length = 0; while ((length = inputStream.read(buffer, 0, buffer.length)) != -1) { bufferOut.write(buffer, 0, length); } bufferOut.flush(); bufferOut.close(); output.close(); inputStream.close(); }
0 0
- springmvc文件上传和文件下载
- 文件上传和文件下载
- SpringMVC文件上传和文件过大处理
- python ftp 文件上传和文件下载
- Struts2中的文件上传和文件下载
- NSURLSession实现文件下载和文件上传
- struts2的文件上传和文件下载
- struts2文件下载和文件上传
- 文件上传和文件压缩下载
- SpringMVC简单入门 源码jar包下载和文件上传
- SpringMVC简单入门 源码jar包下载和文件上传
- 简单的Ext文件上传,文件下载和文件删除
- 文件上传allowedTypes和文件下载contentType(mimeType)
- 文件上传allowedTypes和文件下载contentType(mimeType)
- 利用Socket 实现 FTP 文件上传和文件下载
- 文件上传allowedTypes和文件下载contentType(mimeType)
- linux 通过crt直接上传和下载文件和文件
- Springmvc国际化和文件上传(八)
- Python Numpy Tutorials: 数组--2
- tshark抓包工具的使用
- Mybatis 批量插入
- 【Redis笔记-2】Redis内部数据结构
- MySQL基本语法
- springmvc文件上传和文件下载
- am335x开发板的疑问以及解答
- 树和二叉树(一)
- CGI,FastCGI,PHP-CGI与PHP-FPM
- 正则表达式(1)
- [bigdata-068] centos 安装python3和pip3
- CTF 这个看起来有点简单
- svn出错后 的sqlite3使用方法和lock清理
- math_functions