springmvc 单个文件上传
来源:互联网 发布:雨滴桌面上的软件 编辑:程序博客网 时间:2024/06/09 06:02
一、前台页面
<form name="uploadFile2" method="post" enctype="multipart/form-data" action="${pageContext.request.contextPath}/upload/uploadFile3.do"> <label>文件名称:</label> <input type="text" name="userName3"> <input type="file" name="fileupload3"/> <input type="submit" value="提交2"/> </form>
前台采用form表单同步提交方式。
二、在web-info下建立上传文件夹upload
三、后台java
@RequestMapping(value="uploadFile3.do") public String uploadFile3(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="fileupload3") MultipartFile file, ModelMap map) { String userName = request.getParameter("userName3"); System.out.println("userName:"+userName); if(!file.isEmpty()) { //上传文件路径 String path = request.getSession().getServletContext().getRealPath("/upload/"); //上传文件名 String filename = file.getOriginalFilename(); File filepath = new File(path,filename); //判断路径是否存在,如果不存在就创建一个 if (!filepath.getParentFile().exists()) { filepath.getParentFile().mkdirs(); } //将上传文件保存到一个目标文件当中 try {file.transferTo(new File(path + File.separator + filename));map.addAttribute("message", "上传成功哈哈");} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();map.addAttribute("message", "上传异常");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();map.addAttribute("message", "上传异常");} } else { map.addAttribute("message", "参数异常"); } return "message"; }
四、springmvc.xml配置文件加入配置信息
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 上传文件大小上限,单位为字节(10MB) --> <property name="maxUploadSize"> <value>10485760</value> </property> <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 --> <property name="defaultEncoding"> <value>UTF-8</value> </property></bean>
五、建立message.jsp页面:
<%@ page language="java" pageEncoding="UTF-8"%><!DOCTYPE HTML><html><head> <title>消息提示</title></head> <body> ${message}</body></html>
tomcat下上传实际路径通过打印可知:D:\testPro\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\mySSM2\upload
参考文章:http://www.cnblogs.com/WJ-163/p/6269409.html
阅读全文
0 0
- springmvc 单个文件上传
- SpringMVC实现单个文件上传
- springMVC easyUI filebox 单个文件上传
- SpringMvc上传单个文件以及多个文件
- SpringMVC:用MultipartFile上传单个文件,多个文件
- JQuery与springmvc实现单个文件上传操作
- Flex上传单个文件
- struts2单个文件上传
- Struts2单个文件上传
- struts2 单个文件上传
- struts2 上传单个文件
- struts单个文件上传
- Struts2:上传单个文件
- struts2--单个文件上传
- php--单个文件上传
- struts2文件上传--单个文件
- 使用html+ajax+formdata+SpringMVC实现单个文件,和多个文件上传功能
- 使用html+ajax+formdata+SpringMVC实现单个文件,和多个文件上传功能
- hello.dex 详细结构分析
- python 爬取博客访问量并且统计数据成图
- php 常用函数方法
- tyvj 1305(单调队列)
- C++虚继承的意义和使用
- springmvc 单个文件上传
- ntp时间服务器 时间同步
- LNMP Wordpress更换主机恢复记
- HDU 5179 beautiful number 数位DP(水)
- Android面试题总结(一)
- 第七周项目四—队列数组
- 关于AssetBundle打包策略
- 15算法课程 69. Sqrt(x)
- redis高可用之sentinel哨兵集群详解与搭建