java spring mvc 上传

Spring mvc  实现上传功能


spring mvc Controller 中的代码

@RequestMapping("/userAddGo")
	public ModelAndView userAddGo(HttpServletRequest request,
			@ModelAttribute("user") User user) throws IllegalStateException,
			IOException {
		ModelAndView mav = new ModelAndView("redirect:/user/userList");
		userService.insertUser(user);
		System.out.println("文件上传。。");
		// 创建一个通用的多部分解析器.
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		// 设置编码
		multipartResolver.setDefaultEncoding("utf-8");
		// 判断 request 是否有文件上传,即多部分请求...
		if (multipartResolver.isMultipart(request)) {
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {
				MultipartFile file = multiRequest.getFile(iter.next());
				// 判断文件是否为空
				if (file.isEmpty()) {
					continue;
				}
				String path = request.getSession().getServletContext()
						.getRealPath("/");
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
				sdf.format(new Date());
				// 获取文件的名字
				String originalFilename = file.getOriginalFilename();
				String filepath = request.getSession().getServletContext()
						.getRealPath("/")
						+ "upload/" + file.getOriginalFilename();
				this.createParentDirectory(filepath);
				File uploadFile = new File(filepath);
				file.transferTo(uploadFile);
			}
		}
		return mav;

	}

private void createParentDirectory(String path) {
		Assert.notNull(path);
		File file = new File(path);
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
			file.getParentFile().setWritable(true, false);
			file.getParentFile().setExecutable(true, false);
		}

	}


JSP中的代码
 <tr>
    <td>
      <input type="file" id="file" name="file">上传</ipput>
    </td>
  </tr>
      
      <tr>
      <td>
         <input type="button" οnclick="submit();" value="确认"/>
      </td>
      </tr>
      
 </table>

</form>
<script type="text/javascript">
   function submit(){
		   $('#ff').submit();
   }

</script>


form 标签里面必须有 

enctype="multipart/form-data" 

<form action="${pageContext.request.contextPath}/user/userAddGo" id="ff" enctype="multipart/form-data" method="post">


spring mvc 中配置参数
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
	<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" />  
    </bean> 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值