上传图片

来源:互联网 发布:网络摄像机监控平台 编辑:程序博客网 时间:2024/04/28 17:04

页面addProductBrand.vm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../style.css" rel="stylesheet" type="text/css">
<title></title>
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="10">
<table width="100%" height="1" border="0" cellpadding="0" cellspacing="2">
<form name="form1" method="post" action="productBrand.shtml?method=addProductBrand" enctype="multipart/form-data">
<input type="hidden" name="isSubmit" value="true">
  <tr bgcolor="#CBD6E7">
    <td colspan=4 align=center height=18>添加品牌</td>
  </tr>
  <tr>
    <td class="xz">
     <tr bgcolor="#F0F0F0" >
    <td width="20%">品牌名称:</td>
    <td colspan=3>
       <input type="text" name="brandName" value=""/>
    </td>
     </tr>
  <tr bgcolor="#F0F0F0">
    <td>网址:</td>
    <td colspan=3><input type="text" name="url" value=""/></td>
  </tr>
  <tr bgcolor="#F0F0F0">
    <td>图片:</td>
    <td colspan=3><input type="file" name="image" value=""/></br>上传图片大小最佳为:80*27</td>
  </tr>
  <tr bgcolor="#F0F0F0">
  <td >是否推荐:</td>
      <td colspan=3>
   <select name="re">
        <option value="1" >是</option>
    <option value="2" >否</option>
      </select></td>   
  </tr>
  <tr bgcolor="#CBD6E7">
    <td>&nbsp;</td>
    <td colspan=3>
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr bgcolor="#CBD6E7">
       <td align="left" width="68" height="18"><input type="submit" class="products_set" value="保 存" ></td>    
       <td width="30"></td>
       <td align="left" width="68" height="18"><input type="button" class="products_set" value="关 闭" onclick="window.close();"></td>
     </tr>
      </table>
    </td>
  </tr>
 </td>
 <td>&nbsp;</td>
  </tr>
</form>
</table>   
</body>
</html>

Action:ProductBrandAction

 /**
  * 添加品牌的操作
  *
  * @param request
  * @param response
  * @return
  * @throws Exception
  */
 public ModelAndView addProductBrand(HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  String brandName = "";
  String url = "";
  int recommend = 0;
  FileItem imgItem = null;
  String submit = "";

  FileItemFactory factory = new DiskFileItemFactory();
  ServletFileUpload upload = new ServletFileUpload(factory);

  if (upload.isMultipartContent(request)) {
   List<?> items = upload.parseRequest(request);

   for (Object o : items) {
    FileItem item = (FileItem) o;
    if (item.getFieldName().equals("brandName")) {
     brandName = item.getString();
    }
    if (item.getFieldName().equals("url")) {
     url = item.getString();
    }
    if (item.getFieldName().equals("re")) {
     try {
      recommend = new Integer(item.getString());
     } catch (Exception e) {
     }
    }
    if (item.getFieldName().equals("image")) {
     imgItem = item;
    }
    if (item.getFieldName().equals("isSubmit")) {
     submit = item.getString();
    }
   }
  }

  if ("true".equals(submit)) {
   ProductBrand pb = new ProductBrand();
   pb.setName(brandName);
   pb.setUrl(url);
   pb.setRecommand(recommend);
   pm.save(pb);
   //当上传图片不为空时,保存图片
   if (imgItem.getName() != "") {
    String path = request.getSession().getServletContext()
      .getRealPath("/")
      + ProductBrand.PRODUCTBRAND_IMG_PATH + File.separator;
    File f = new File(path);
    if (!f.exists()) {
     f.mkdir();
    }
    String fileName = pb.getId() + "."
      + imgItem.getContentType().substring(7);

    // 把图片以流的形式保存为另一个图片
    InputStream is = null;
    OutputStream os = null;
    try {
     is = imgItem.getInputStream();
     os = new FileOutputStream(path + fileName);
     int bytesRead = 0;
     byte[] buffer = new byte[8192];
     while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
      os.write(buffer, 0, bytesRead);
     }
    } catch (Exception e) {
     e.printStackTrace();
    } finally {
     os.close();
     is.close();
    }
    pb.setLogoFile(fileName);
    pm.update(pb);
   }
   return new ModelAndView("/workspace/refresh");
  }
  return new ModelAndView("/product/addProductBrand");
 }

 

原创粉丝点击