經(jīng)過倆個星期的奮斗,易買網(wǎng)項目完工。在之前,實現(xiàn)圖片的上傳,走過許多彎路,原來是好多基礎的知識忘記了,沒把smartupload文件包添加組件jar包至WEB-INF/lib包中,在此特別重視,做下文件的上傳。
第一步:創(chuàng)建商品實體類:
package entity;public class easybuy_PRoductn { private int ep_id ;//編號 private String ep_name ;//名字 private String ep_description ;//描述 private int ep_price;//價格 private int ep_stock ;//庫存 private int epc_id ;//父類id private int epc_child_id ;//二級id private String ep_file_name ;//文件名 private int tiaoma ;//條碼號 public int getEp_id() { return ep_id; } public void setEp_id(int epId) { ep_id = epId; } public String getEp_name() { return ep_name; } public void setEp_name(String epName) { ep_name = epName; } public String getEp_description() { return ep_description; } public void setEp_description(String epDescription) { ep_description = epDescription; } public int getEp_price() { return ep_price; } public void setEp_price(int epPrice) { ep_price = epPrice; } public int getEp_stock() { return ep_stock; } public void setEp_stock(int epStock) { ep_stock = epStock; } public int getEpc_id() { return epc_id; } public void setEpc_id(int epcId) { epc_id = epcId; } public int getEpc_child_id() { return epc_child_id; } public void setEpc_child_id(int epcChildId) { epc_child_id = epcChildId; } public String getEp_file_name() { return ep_file_name; } public void setEp_file_name(String epFileName) { ep_file_name = epFileName; } public int getTiaoma() { return tiaoma; } public void setTiaoma(int tiaoma) { this.tiaoma = tiaoma; }}
第二步:編寫sql:
//添加商品public int Produt(String epName, String epDescription, int epPrice, int epStock, int epcId, String epFileName,String ep_name_tiaoma) { //創(chuàng)建數(shù)據(jù)庫的鏈接 con=this.getConnection(); //創(chuàng)建int值確認是否插入成功 int i=0; //創(chuàng)建sql語句 String sql="insert into easybuy_productn values(easybuy_productn_seq.nextval,?,?,?,?,?,'',?,?)"; try { //寫入sql st=con.prepareStatement(sql); st.setString(1, epName); st.setString(2, epDescription); st.setInt(3, epPrice); st.setInt(4, epStock); st.setInt(5, epcId); st.setString(6, epFileName); st.setString(7, ep_name_tiaoma); //開始執(zhí)行sql i=st.executeUpdate(); //開始判斷 if(i>0){ i=1; }else{ i=0; } //釋放鏈接 this.ShiFang(rs, st, con); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return i;}添加商品jsp頁面
<h2>添加商品</h2> <div class="manage"> <form action="manage/MyJsp.jsp" method="post" enctype="multipart/form-data"> <table class="form"> <tr> <td class="field">描述描述:</td> <td><input type="text" class="text" name="productName" id="productscript" value="鐵三角 Audio-Technica ATH-EQ300M-SV 銀色 掛耳式耳機" /></td> </tr> <tr> <td class="field">所屬分類:</td> <td> <select name="parentId"> <c:forEach var="news" items="${requestScope.flist1}" > <option value="1">${news.epc_name}</option> <c:forEach var="ck" items="${flist}" > <c:if test="${ck.epc_parent_id == news.epc_id}"> <option value="3">${ck.epc_name}</option> </c:if> </c:forEach> </c:forEach> </select> </td> <tr> <td class="field">頭像:</td> <td><input type="file" name="fileg" id="imgOne" onchange="preImg(this.id,'imgPre');"/></td> </tr> <tr> <td>頭像預覽:</td> <td><img id="imgPre" src="" style="width:150px;height:150px;display:block;"/></td> </tr> <tr> <td class="field">商品價格:</td> <td><input type="text" class="text tiny" name="productPrice" id="productPrice" /> 元</td> </tr> <tr> <td class="field">名稱:</td> <td><input type="text" class="text" name="productName2" id="productName" /></td> </tr> <tr> <td class="field">庫存:</td> <td><input type="text" class="text tiny" name="productName3" id="producstock"/></td> </tr> <tr> <td class="field">條號碼:</td> <td><input type="text" class="text tiny" name="productName4" id="producstock"/></td> </tr> <tr> <td></td> <td><label class="ui-blue"><input type="submit" name="submit" value="添加" /></label></td> </tr> </table> </form>處理頁面:
<%//----處理上傳文件/* 使用smartupload 1、添加組件jar包至WEB-INF/lib包中 2、設置form表單的提交類型為二進制類型*///創(chuàng)建smartupload組件SmartUpload su = new SmartUpload();//初始化組件上下文su.initialize(pageContext);//設置編碼su.setCharset("utf-8");try{ //上傳文件 su.upload();}catch(Exception e){ %> <script type="text/javascript"> alert("不允許上傳:bat,exe,jsp,html,htm,java,class文件類型"); location.href="index.jsp"; </script> <%}//獲取單個文件File file = su.getFiles().getFile(0);//設置上傳服務器的位置String path2 = "upload//";//得到上傳文件的名稱String filename = file.getFileName();//上傳文件的路徑path2 = path2 + filename;//保存文件file.saveAs(path2,SmartUpload.SAVE_VIRTUAL);//獲取圖片顯示路徑String path3="upload//"+filename;//獲取用戶的名稱String productName = su.getRequest().getParameter("productName");//獲取用戶的分類int parentId = Integer.parseInt(su.getRequest().getParameter("parentId"));//獲取用戶價格int productPrice=Integer.parseInt(su.getRequest().getParameter("productPrice"));//獲取用戶的品牌String productName2=su.getRequest().getParameter("productName2");//獲取用戶的庫存int productPrice3=Integer.parseInt(su.getRequest().getParameter("productName3"));//獲取用戶的條碼號String productPrice5=su.getRequest().getParameter("productName4");//實例化添加對象productnBiz product=new productnBizImpl();//調用查詢方法int i=product.Produt(productName2,productName,productPrice,productPrice3,parentId,path3,productPrice5);//判斷添加新聞是否成功if(i>0){ out.print("<script>"); out.print("alert('添加成功');"); out.print("location.href='product.jsp';"); out.print("</script>");}else{ request.getRequestDispatcher("product-add.jsp").forward(request,response);}%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>My JSP 'Product_Add.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/CSS" href="styles.css"> --> </head> <body> This is my JSP page. <br> </body></html>新聞熱點
疑難解答