SpringMVC文件上传
来源:互联网 发布:水电上门维修软件 编辑:程序博客网 时间:2024/06/05 07:01
SpringMVC文件上传
SpringMVC会根据请求方法签名不同,将请求消息中的信息以一定的方式转换并绑定到其你去方法的参数中。
今天我们来说说文件上传
文件上传
文件上传是Web项目中最基础的功能了。首先我们来看看文件上传在Spring里是怎么运行的。
首先将表单的method设置为POST,并将enctype设置为multipart/form-data,浏览器就会采用二进制的防治来处理表单上的数据,而对于文件上传的处理则设计在服务器端解析原始的HTTP响应。
需要的jar包
普通的springmvc的jar包除外,还需要
笔者在写测试项目的时候有出现servlet的jar缺失,这个也是需要加进去的。
首先我们先来写jsp代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312" %> <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <title>文件上传</title> <body> <h2>注册界面</h2> <br> <form action="upload" enctype="mulipart/form-data" method="post"></form> <table> <tr> <td><label>文件描述:</label></td> <td><input type="text" name="descirption"></td> </tr> <tr> <td><label>密码:</label></td> <td><input type="password" id="password" name="password"></td> </tr> <tr> <td><input type="submit" value="上传"></td> </tr> </table> </body></html>
控制器代码:
import java.io.File;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.MultipartFile;public class FileUploadController {@RequestMapping(value="/upload",method=RequestMethod.POST) public String uploadPosdetailFile(@RequestParam("description") String descrtption, @RequestParam("file") MultipartFile file,HttpServletRequest request,HttpServletResponse response) throws Exception{System.out.println("description");//文件不能为空if(!file.isEmpty()){//上传文件路径String path = request.getServletContext().getRealPath("/images");//上传文件名String filename = file.getOriginalFilename();File filepath = new File(path,filename);//判断路径是否存在,如果不存在就创建一个if(!filepath.getParentFile().exists()){filepath.getParentFile().mkdirs();}//将上传文件保存到一个目标文件中file.transferTo(new File(path+File.separator+filename));return "success";}return "error";}}
Springmvc配置文件代码需要加上:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 上传文件大小上线,单位为字节 --> <property name="maxUploadSize"><value>100000</value></property> <!-- 请求的编码格式,必须和JSP的怕个Encoding属性一致,以便正确读取表单的内容 --> <property name="defaultEncoding"><value>UTF-8</value></property> </bean>
Web.xml配置文件就是最基本的,这里不再多加叙述。
这里既然说到文件上传,我就顺便提一提文件下载,SpringMVC提供了一个RespouseEntity类型,使用它可以嫩方便地定义返回的HttpHeaders和HttpStatus。
到这里就经过部署就可以使用浏览器进行测试了,
浏览器输入
http://localhost:8080/FileUploadTest/registerForm
阅读全文
1 0
- springMVC(11) springMVC文件上传
- SpringMVC-----8、SpringMVC上传文件
- springmvc文件上传总结
- SpringMVC multipart文件上传
- SpringMVC中的文件上传
- springmvc的文件上传
- springMVC上传文件
- springMVC 文件上传
- SpringMVC - 文件上传篇
- springMVC文件上传
- springMVC文件上传
- springMVC文件上传
- SpringMVC 上传文件
- SpringMVC文件上传
- springMVC上传文件
- springMVC上传文件例子
- springMVC上传文件
- springmvc文件上传错误
- 2017年/7月/10日 随手记_php文件line0出现错误
- Shell封装linux命令
- JZOJ__Day 5:【普及模拟】num
- python的全局变量(属性的用法),私有属性,计算对象引用数
- Android 单位转换工具
- SpringMVC文件上传
- (38)finally和堆catch的理解
- 利用微信企业号实现发红包功能
- RecyclerView 添加 Header 和 Footer
- 常用的正则表达式
- Angular 4 学习笔记 从入门到实战 打造在线竞拍网站 基础知识 快速入门 个人感悟
- eclipse-无法切换中文输入法
- Apache http Server相关的安装与配置
- 传递一个字符串,将原字符串重新排序,实现cookie 浏览效果的 字符串util测试类