导入jspSmartUpload.jar包
servlet代码
package product;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import dao.ProductDao;
import pojo.Product;
@WebServlet("/manager/admin_doproductadd")
public class DoProductAdd extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建smartupload对象
SmartUpload su = new SmartUpload();
//初始化
su.initialize(this.getServletConfig(), request, response);
//上传过程
try {
su.upload();
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//获取上传的文件对象
Files fs = su.getFiles();
File f = fs.getFile(0);//由于只上传了一个,所以直接获取0号元素
String fname = f.getFilePathName();//xx.jpg
try {
//此处由于是上传到发布路径下,所以开发路径下没有该图片,项目目录下有图片
su.save("/images/product");
} catch (SmartUploadException e) {
e.printStackTrace();
}
//原来的request获取不到原来表单的参数,需要SmartUpload对象的request获取表单中的参数
Request req1 = su.getRequest();
String productname = req1.getParameter("pruductname");
String picture = req1.getParameter("picture");
String price = req1.getParameter("price");
String desc = req1.getParameter("desc");
String stock = req1.getParameter("stock");
Product p = new Product();
p.setProduct_filename(productname);
p.setProduct_describe(desc);
p.setProduct_filename(fname);
p.setProduct_price(Double.parseDouble(price));
p.setProduct_stock(Integer.parseInt(stock));
p.setProduct_name(productname);
int flag = ProductDao.insert(p);
if(flag>0) {
response.sendRedirect("admin_doproductselect");
}else {
PrintWriter out = response.getWriter();
out.print("<script>");
out.print("alert('增加失败');");
out.print("location.href='admin_toproductadd';");
out.print("</script>");
}
}
}
HTML
<form action="/shopproject/manager/admin_doproductadd" enctype="multipart/form-data" method="post" name="myform">
<table>
<tr>
<td>图书名字:<input class="common-text required" id="" name="pruductname" size="50" value="" type="text">
</td>
</tr>
<tr>
<td>图书图片:<input class="common-text required" id="" name="picture" size="50" value="" type="file">
</td>
</tr>
<tr>
<td> 图书价格: <input class="common-text required" id="" name="price" size="50" value="" type="text">
</td>
</tr>
<tr>
<td> 图书描述:<input class="common-text required" id="" name="desc" size="120" value="" type="text">
</td>
</tr>
<tr>
<td> 图书库存:<input class="common-text required" id="" name="stock" size="50" value="" type="text">
</td>
</tr>
</form>
总结:
需要导入jspSmartUpload.jar
表单form加上属性enctype="multipart/form-data",提交方式为post
需要用SmartUpload重新获取request对象才能获取表单对应的内容
文章转载自JAVA不归路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




