码迷,mamicode.com
首页 > Web开发 > 详细

易买网之smartupload实现文件上传

时间:2015-06-17 22:59:20      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

经过俩个星期的奋斗,易买网项目完工。在之前,实现图片的上传,走过许多弯路,原来是好多基础的知识忘记了,没把smartupload文件包添加组件jar包至WEB-INF/lib包中,在此特别重视,做下文件的上传。

第一步:创建商品实体类:

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) {
    
    //创建数据库的链接
     con=this.getConnection();

    
    //创建int值确认是否插入成功
    int i=0;
    
    //创建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);
        
        //开始执行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表单的提交类型为二进制类型
*/

//创建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>

 

易买网之smartupload实现文件上传

标签:

原文地址:http://www.cnblogs.com/wlx520/p/4579128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!