经过俩个星期的奋斗,易买网项目完工。在之前,实现图片的上传,走过许多弯路,原来是好多基础的知识忘记了,没把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实现文件上传的更多相关文章

  1. 基于Servlet+smartUpload的文件上传

    文件上传在web应用中是非常常见的,现在我就介绍下基于servlet的文件上传,基于Struts2的文件上传可以看: 页面端代码: <%@ page language="java&qu ...

  2. SmartUpload实现文件上传

    (一)SmartUpload组件简介 SmartUpload组件 专门用于实现文件上传及下载的免费组件   (二)SmartUpload组件特点 使用简单:编写少量代码,完成上传下载功能 能够控制上传 ...

  3. SmartUpload实现文件上传时file和表单文本同时提交的问题

    JSP页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...

  4. smartUpload组件文件上传

    public class SmartUploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, ...

  5. 文件上传之 MultipartFile

    利用MultipartFile(组件)实现文件上传 在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的Mult ...

  6. JSP文件上传--Smartupload组件

    把smartupload.jar copy到D:\apache-tomcat-7.0.57\lib下. 创建htm上传文件:smartupload_demo01.htm 由于是上传文件,需要对表单进行 ...

  7. 文件上传--基于Spring MVC框架+SmartUpload

    这篇文章是介绍文件上传的,由于在spring MVC上实现起来和直接在servlet中写有些不同,所以特地写了一下这篇文章,关于不同点,大家可以先阅读一下上一篇文章.好了,下面直接上代码. jab包是 ...

  8. SmartUpload文件上传组件的使用教程

    在项目中使用SmartUpload组件可以进行文件的上传和下载操作 使用步骤 1.导入jar包,进行build path操作 2.编写文件上传页面,代码如下 <form action=" ...

  9. java基础篇---文件上传(smartupload组件)

    文件上传几乎是所有网站都具有的功能,用户可以将文件上传到服务器的指定文件夹中,也可以保存在数据库中,本篇主要说明smartupload组件上传. 在讲解smartupload上传前,我们先来看看不使用 ...

随机推荐

  1. linux系统下安装R

    一.先通过ssh将R安装包R-3.2.2.tar.gz从本机复制到你的linux虚拟机上的/home下: 二.解压安装包 #tar -zxvf R-3.2.2.tar.gz 三.1).进入到解压后的R ...

  2. 今天開始慢下脚步,開始ios技术知识的查漏补缺。

    从2014.6.30 開始工作算起. 如今已经是第416天了.不止不觉.时间过的真快. 通过对之前工作的总结.发现,你的知识面.会决定你面对问题时的态度.过程和结果. 简单来讲.知识面拓展了,你才干有 ...

  3. [Vue @Component] Handle Errors and Loading with Vue Async Components

    Because async components are not bundled with your app, they need to be loaded when requested. This ...

  4. scikit-learn:4.7. Pairwise metrics, Affinities and Kernels

    參考:http://scikit-learn.org/stable/modules/metrics.html The sklearn.metrics.pairwise submodule implem ...

  5. Java 定时任务之Quartz

    使用CronTrigger CronTrigger 能够提供比 SimpleTrigger 更有具体实际意义的调度方案,调度规则基于 Cron 表达式,CronTrigger 支持日历相关的重复时间间 ...

  6. C++中的继承与虚函数各种概念

    虚继承与一般继承 虚继承和一般的继承不同,一般的继承,在目前大多数的C++编译器实现的对象模型中,派生类对象会直接包含基类对象的字段.而虚继承的情况,派生类对象不会直接包含基类对象的字段,而是通过一个 ...

  7. protected (C# Reference)

    https://msdn.microsoft.com/en-us/library/bcd5672a.aspx The protected keyword is a member access modi ...

  8. Hybrid 开发

    主讲人:吴彬 要学习某个东西之前,我们首先要了解这个东西是什么?然后我们要了解这东西有什么用,有什么好处和弊端?最后我们要知道这东西怎么用? 简单点就是 ——是什么?有什么用?怎么用? 那么进入正题 ...

  9. XAML实例教程系列 - 依赖属性和附加属性(四)

    XAML实例教程系列 - 依赖属性和附加属性 2012-06-07 13:11 by jv9, 1479 阅读, 5 评论, 收藏, 编辑 微软发布Visual Studio 2012 RC和Wind ...

  10. AJAX异步实现

    AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容. <head> <meta charset="utf-8"> & ...