form表单提交数据 同时提交图片 jsp+contrller


jsp 页面

 <form   id= "form1" action="${pageContext.request.contextPath}/information/addInformation.shtml"  method="post"  οnsubmit="check2()" enctype="multipart/form-data"   >
            <input  name="loginId"  type="hidden"   value="${userInfo.loginId}"/>
            <input  name="schschoolId" type="hidden"  value="${userInfo.schschoolId}"  />
            
	        <span class="left">文章标题:</span>
	        <input id="title"  type="text" class="text left w350 mr20" name="title"/>
	        <span class="left">是否仅限本校学生查看:</span>
	        <div class="dropdown text w80 mr20">
	        <select  id="select_k1"   class="shadow" name="display" >
				<option value="1">是</option>
				<option value="0">否</option>
 	  		 </select>
 	  		 
 	  		       
       	<!-- 登陆id -->
    
        </div> 
        <span class="left">类型:</span>
        <div class="dropdown text w80 mr20">
        <select name="type" id="select_k1"   class="shadow">
		<option value="1">高校专版</option>
		<option value="2">发现</option>
 	   </select>
        </div> 
        
        
          <span class="left">是否为轮播图:</span>
        <div class="dropdown text w80 mr20">
        <select name="isFigure" id="select_k1"   class="shadow">
		<option value="0">否</option>
		<option value="1">是</option>
 	   </select>
        </div> 
        <span class="left"  style="width:80px; height:15px;"></span>
        <span class="left"  >封面图:</span>
        <input id="pic"  type="file" class="text left w200 mr10" name="pic"   />
   
       <textarea id="contents" name="contents" style="width:98%; height:185px;visibility:hidden;"></textarea> 
  
           <div class="text-box pr30">
		<div class="btn-box">
          <!--part-->
          <div class="part tr">
            <!--  <a title="发布" class="blue-btn btn-big mr10" οnclick="subForm()" href="#">发布</a>-->
            <a title="发布"  id="addsubmit" href="javascript:;" class="blue-btn btn-big mr10">发布</a>
            <a title="取消" class="btn gray-btn btn-big mr10" href="informationList.shtml">取消</a> 
          </div>
          <!--part end-->
        </div>  
      </div>    
        </form>



controller页面


/**
	 *
	 * 添加资讯
	 */
	@RequestMapping(value = "addInformation")
	public String logout(HttpServletRequest req, Model model, MultipartFile pic) {
		logger.info("添加资讯");
		String title = (String) req.getParameter("title");
		logger.info(title);
		int loginId = Integer.parseInt(req.getParameter("loginId"));
		int schschoolId = Integer.parseInt(req.getParameter("schschoolId"));
		int display = Integer.parseInt(req.getParameter("display"));
		int type = Integer.parseInt(req.getParameter("type"));
		int isFigure = Integer.parseInt(req.getParameter("isFigure"));
		String content = (String) req.getParameter("content");
		/**
		 * 添加封面图
		 */
		// 扩展名
		String ext = FilenameUtils.getExtension(pic.getOriginalFilename());
		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String name = df.format(new Date());
		Random r = new Random();
		for (int i = 0; i < 3; i++) {
			name += r.nextInt(10);
		}
		String p = req.getSession().getServletContext().getRealPath("");
		String p2=	req.getContextPath();
		String path =  "/images/" + name + "." + ext;
		String url = p + path;//存到服务服务器的地址
		String url2 = p2 + path;//保存到数据库地址
		try {
			pic.transferTo(new File(url));
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 获取资讯内容的最大id
		logger.info("获取资讯最大id数");
		int maxCountId = informationService.getInformationContentId();
		maxCountId += 1;
		Information information = new Information();
		information.setSchoolId(schschoolId);
		information.setLoginId(loginId);
		information.setTitle(title);
		information.setDisplay(display);
		information.setType(type);
		information.setIsFigure(isFigure);
		information.setHit(0);
		information.setLastTime(new Date());
		information.setSort(0);
		information.setStatus(1);
		information.setId(maxCountId);
		information.setCoverImgUrl(url2);
		// 添加资讯
		informationService.addInformation(information);
		// 添加资讯内容
		int contentId = informationService.getContentId();//获取内容最大的id
		informationService.addcontent(contentId + 1, maxCountId, content);
		return "view/information/success";
	}








  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学海无涯苦做java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值