图片上传

jsp页面

<%@ page language="java" pageEncoding="GBK"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
  <head>
    <html:base />
   
    <title>添加商品</title>
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 
 <link rel="stylesheet" type="text/css" href="styles.css" /> 
 <link type="text/css" rel="stylesheet" href="cal/calendar-system.css"/>
 
  <script>
    function doSubmit(){
     if (Validator.Validate(document.forms[0],3)) {
      document.forms[0].submit();
     }
  }
  function setPic(a)
  {
   var oImg = document.getElementById('goodsPic');
   oImg.src = a;
   oImg.height = 200;
  }
   </script> 
  <script type="text/javascript" src="js/Calendar3.js"></script>
  </head>   
  <body>
   <html:form action="goods.do?op=doAddGoods" method="post"  enctype="multipart/form-data">
      <input type="hidden" name="op" value="addGoods" />
   <div id="header" class="center">
    <img src="images/epai_logo.gif" style="width:110px;margin:10px;"></img> 
   </div>
   <div id="main" class="center">
    <div id="left">
      <div id="search">
       <div class="dia_title">查找商品</div>
       <div class="dia_content" style="height:150px;">
        &nbsp;
       </div>
       <div class="dia_foot"></div>
      </div>
      <div id="menu">
     <div class="dia_title">正在热拍</div>
       <div class="dia_content2" style="height:359px;">
        <!-- div class="list_item">
         <img src="images/arr.gif">&nbsp; <a href="#" class="menu">Computers</a>
        </div -->
        
       </div>
       <div class="dia_foot2">&nbsp;</div>
      </div>
     </div>
     <div id="context">
      <div class="c_title">添加商品</div>
      <div class="c_content">       
  <ul style="color:red;list-style-type:circle;">
   ${msg}
  </ul>
  <table border="0">
        <tr>
          <th>商品名称</th>
          <td> <html:text property="goods.goodsName"></html:text> </td>
        </tr>
        <tr>
          <th>起拍价</th>
          <td><html:text property="goods.goodsPrice"></html:text> </td>
        </tr>
        <tr>
          <th>图片</th>
          <td><html:file property="pic" onchange="setPic(this.value);"></html:file> </td>
        </tr>
        <tr>
          <th>描述</th>
          <td><html:textarea property="goods.goodsDesc"></html:textarea></td>
        </tr>
        <tr>
          <th>开始拍卖时间</th>
          <td>
           <html:text property="beginTime" onclick="new Calendar().show(this);"></html:text>
          </td>
        </tr>
        <tr>
          <th>截至时间</th>
          <td>
           <html:text property="endTime" onclick="new Calendar().show(this);"></html:text>  
          </td>
        </tr>
      </table>
  
       <div class="button_bar">
        <html:submit value=" 提 交 "></html:submit>
              </div>
              <p align="center">
               <img id="goodsPic" src="images/preview.jpg" style="border:solid 1px green;" />
              </p>
      </div>
      <div class="c_foot">&nbsp;</div>
     </div>
   </div>
   <div id="footer" class="center">
   </div>
   <script>
//    build_validate("item.userName","用户名不能为空","Limit","1","50");

   </script>
    </html:form>
  </body>
</html:html>

 

 

 

action代码

 

public ActionForward doAddGoods(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  GoodsForm gf = (GoodsForm) form;
  FormFile file = gf.getPic();
  //取得相对于服务器的一个资源对象
  String myPic = request.getSession().getServletContext().getRealPath("/goodsPic");
  try {
   //文件输出流对象
   FileOutputStream out = new FileOutputStream(new File(myPic+"/"+file.getFileName()));
   //将上传过来的文件写入到指定文件
   out.write(file.getFileData());
   out.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  Goods goods = gf.getGoods();
  String begin = gf.getBeginTime();
  String end = gf.getEndTime();
  goods.setBeginTime(DateConvert.toDate(begin));
  goods.setEndTime(DateConvert.toDate(end));
  TUser user = (TUser) request.getSession().getAttribute("user");
  System.out.println(user.getUserId());
  goods.setSalerId(user);
  goods.setGoodsPic("goodsPic/"+file.getFileName());
  System.out.println(file.getFileName());
  goods.setBuyerId(null);
  goods.setGoodsStatus(1);
  
  this.goodsBiz.addgoods((goods));
 
  Goods good2=null;
  List<Goods> list=this.goodsBiz.seach(good2);
  request.setAttribute("goods", list);
  return mapping.findForward("index");
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值