AddProductAction.java
上传用户:zhc3n3
上传日期:2022-07-30
资源大小:2750k
文件大小:4k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
JavaScript
- package com.t11.web.action;
- import java.io.File;
- import java.io.UnsupportedEncodingException;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.commons.fileupload.FileItem;
- import org.apache.commons.fileupload.FileUploadException;
- import org.apache.commons.fileupload.disk.DiskFileItemFactory;
- import org.apache.commons.fileupload.servlet.ServletFileUpload;
- import com.t11.biz.ProductService;
- import com.t11.entity.Product;
- import com.t11.web.Action;
- import com.t11.web.ActionForward;
- import com.t11.web.ActionMapping;
- /**
- * 商品添加(fileupload上传)
- * @author T11
- *
- */
- public class AddProductAction implements Action {
- String name="";
- double price=0;
- String imagefile="";
- String author="";
- String publishing="";
- String introduction="";
- ActionForward actionForward = null;
- //实例化业务逻辑类
- ProductService productService=new ProductService();
- public ActionForward execute(ActionMapping actionMapping,
- HttpServletRequest request, HttpServletResponse response) {
- String filename="";
- //文件的上传
- //磁盘工厂
- DiskFileItemFactory factory=new DiskFileItemFactory();
- //FileUpload对象
- ServletFileUpload upload=new ServletFileUpload(factory);
- //是否包含Multipart
- if(ServletFileUpload.isMultipartContent(request)){
- //最大上传
- upload.setFileSizeMax(4098*4098);
- //将请求分为几个部分
- try {
- List<FileItem> list=upload.parseRequest(request);
- //遍历
- for (FileItem fileItem : list) {
- //是表单元素
- if(fileItem.isFormField())
- {
- //表单元素的名称
- String fieldName=fileItem.getFieldName();
- //表单元素的值
- String fieldValue=fileItem.getString();
- byte[] bytes;
- try {
- bytes = fieldValue.getBytes("iso-8859-1");
- fieldValue = new String(bytes,"GBK");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- if(fieldName.equals("productname"))
- {
- name=fieldValue;
- }else if(fieldName.equals("productprice")){
- price=Double.parseDouble(fieldValue);
- }else if(fieldName.equals("productauthor")){
- author=fieldValue;
- }else if(fieldName.equals("productpublishing")){
- publishing=fieldValue;
- }else if(fieldName.equals("productintroduction")){
- introduction=fieldValue;
- }
- }else{
- //文件名 c:abc123.jpg
- filename=fileItem.getName();
- //只取文件名
- filename=filename.substring(filename.lastIndexOf("\")+1);
- //将虚拟路径转为物理路径
- String path=request.getSession().getServletContext().getRealPath("/");
- //上传
- String serverfilename=path+"/uploads/"+filename;
- File file=new File(serverfilename);
- try {
- fileItem.write(file);//真正上传
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- } catch (FileUploadException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //封装 ,插入数据库
- Product product=new Product();
- product.setProductname(name);
- product.setProductprice(price);
- product.setProductimage("uploads/"+filename);
- product.setProductauthor(author);
- product.setProductpublishing(publishing);
- product.setProductintroduction(introduction);
- //调用业务逻辑类
- productService.addProduct(product);
- return actionForward=actionMapping.getForward().get("success");
- }
- }