AddProductAction.java
上传用户:zhc3n3
上传日期:2022-07-30
资源大小:2750k
文件大小:4k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

JavaScript

  1. package com.t11.web.action;
  2. import java.io.File;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.List;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.apache.commons.fileupload.FileItem;
  8. import org.apache.commons.fileupload.FileUploadException;
  9. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  10. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  11. import com.t11.biz.ProductService;
  12. import com.t11.entity.Product;
  13. import com.t11.web.Action;
  14. import com.t11.web.ActionForward;
  15. import com.t11.web.ActionMapping;
  16. /**
  17.  * 商品添加(fileupload上传)
  18.  * @author T11
  19.  *
  20.  */
  21. public class AddProductAction implements Action {
  22. String name="";
  23. double price=0;
  24. String imagefile="";
  25. String author="";
  26. String publishing="";
  27. String introduction="";
  28. ActionForward actionForward = null;
  29. //实例化业务逻辑类
  30. ProductService productService=new ProductService();
  31. public ActionForward execute(ActionMapping actionMapping,
  32. HttpServletRequest request, HttpServletResponse response) {
  33. String filename="";
  34. //文件的上传
  35. //磁盘工厂
  36. DiskFileItemFactory factory=new DiskFileItemFactory();
  37. //FileUpload对象
  38. ServletFileUpload upload=new ServletFileUpload(factory);
  39. //是否包含Multipart
  40. if(ServletFileUpload.isMultipartContent(request)){
  41. //最大上传
  42. upload.setFileSizeMax(4098*4098);
  43. //将请求分为几个部分
  44. try {
  45. List<FileItem> list=upload.parseRequest(request);
  46. //遍历
  47. for (FileItem fileItem : list) {
  48. //是表单元素
  49. if(fileItem.isFormField())
  50. {
  51. //表单元素的名称
  52. String fieldName=fileItem.getFieldName();
  53. //表单元素的值
  54. String fieldValue=fileItem.getString();
  55. byte[] bytes;
  56. try {
  57. bytes = fieldValue.getBytes("iso-8859-1");
  58. fieldValue = new String(bytes,"GBK");
  59. } catch (Exception e) {
  60. // TODO Auto-generated catch block
  61. e.printStackTrace();
  62. }
  63. if(fieldName.equals("productname"))
  64. {
  65. name=fieldValue;
  66. }else if(fieldName.equals("productprice")){
  67. price=Double.parseDouble(fieldValue);
  68. }else if(fieldName.equals("productauthor")){
  69. author=fieldValue;
  70. }else if(fieldName.equals("productpublishing")){
  71. publishing=fieldValue;
  72. }else if(fieldName.equals("productintroduction")){
  73. introduction=fieldValue;
  74. }
  75. }else{
  76. //文件名 c:abc123.jpg
  77. filename=fileItem.getName();
  78. //只取文件名
  79. filename=filename.substring(filename.lastIndexOf("\")+1);
  80. //将虚拟路径转为物理路径
  81. String path=request.getSession().getServletContext().getRealPath("/");
  82. //上传
  83. String serverfilename=path+"/uploads/"+filename;
  84. File file=new File(serverfilename);
  85. try {
  86. fileItem.write(file);//真正上传
  87. } catch (Exception e) {
  88. // TODO Auto-generated catch block
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. } catch (FileUploadException e) {
  94. // TODO Auto-generated catch block
  95. e.printStackTrace();
  96. }
  97. }
  98. //封装 ,插入数据库
  99. Product product=new Product();
  100. product.setProductname(name);
  101. product.setProductprice(price);
  102. product.setProductimage("uploads/"+filename);
  103. product.setProductauthor(author);
  104. product.setProductpublishing(publishing);
  105. product.setProductintroduction(introduction);
  106. //调用业务逻辑类
  107. productService.addProduct(product);
  108. return actionForward=actionMapping.getForward().get("success");
  109. }
  110. }