先是javaScript的处理
function dosubmit(){ var th=document.form1; th.action="<%=path%>/servlet/ProductAction?action_flag=add"; th.submit(); }
新建一个接口 来处理上传
package com.servlet.product.service; import java.util.List; public interface ProductService { public boolean addProduct(List<Object> params); }
新建一个类 继承接口 完成对数据的访问
package com.servlet.product.dao; import java.sql.SQLException; import java.util.List; import com.jdbc.util.MySqlConnect; import com.servlet.product.service.ProductService; public class ProductDao implements ProductService { MySqlConnect conn; public ProductDao() { conn = new MySqlConnect(); } public boolean addProduct(List<Object> params) { boolean flag=false; String sql = "insert into product(proid,proname,proprice,proaddress,proimage) values(?,?,?,?,?) "; conn.getConnect(); try { flag=conn.updataByPrepareStatement(sql, params); } catch (SQLException e) { e.printStackTrace(); }finally{ conn.releaseConnect(); } return flag; } }
还需要使用UUID来给数据添加主键
package com.servlet.product.util; import java.util.UUID; public class UUIDTools { public UUIDTools() { } public static String getUUID() { UUID uuid = UUID.randomUUID(); System.out.println("uuid "+uuid.toString()); return uuid.toString().replace("-", "").substring(0, 6); } }
再新建一个类 完成对业务逻辑的处理
这里使用apache的commons.fileupload的jar包 来进行文件的上传
package com.servlet.product.action; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; 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.servlet.product.dao.ProductDao; import com.servlet.product.service.ProductService; import com.servlet.product.util.UUIDTools; public class ProductAction extends HttpServlet { private ProductService service; public ProductAction() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String action_flag = request.getParameter("action_flag"); if (action_flag.equals("add")) { addProduct(request, response); } } private void addProduct(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getContextPath(); // 实例化工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); // 创建上传类 ServletFileUpload upload = new ServletFileUpload(factory);
//设置文件大小
upload.setFileSizeMax(3*1024*1024);
//设置总文件大小
upload.setSizeMax(6*1024*1024); // 接收返回集 List<FileItem> list = null; List<Object> params = new ArrayList<Object>(); params.add(UUIDTools.getUUID()); try { // 解析request请求 list = upload.parseRequest(request); // 分开文本数据 和 非文本数据 for (FileItem item : list) { if (item.isFormField()) { if (item.getFieldName().equals("proname")) { params.add(item.getString("utf-8")); } if (item.getFieldName().equals("proprice")) { params.add(item.getString("utf-8")); } if (item.getFieldName().equals("proaddress")) { params.add(item.getString("utf-8")); } } else { try { String imgName = item.getName(); params.add(imgName); String upload_path = request.getRealPath("/upload"); File file = new File(upload_path +"/" + imgName); item.write(file); boolean flag=service.addProduct(params); if(flag){ System.out.println("跳转"); response.sendRedirect(path+"/product/2_1_5.jsp"); } } catch (Exception e) { e.printStackTrace(); } } } } catch (FileUploadException e) { e.printStackTrace(); } } public void init() throws ServletException { service=new ProductDao(); } }
总结一下:
上传时,这里使用了fileupload.jar包,于是 HTTP 请求中的复杂表单元素都被看做一个 FileItem 对象;
FileItem 对象必须由 ServletFileUpload 类中的 parseRequest() 方法解析 HTTP 请求(即被包装之后的 HttpServletRequest 对象)出来。
即分离出具体的文本表单和上传文件;而 ServletFileUpload 对象的创建需要依赖于 FileItemFactory 工厂将获得的上传文件 FileItem 对象保存至服务器硬盘, 即 DiskFileItem 对象。
一般包含文本数据和非文本数据 ,这时需要对数据区分处理,使用 isFormField()可以进行区分。
DiskFileItemFactory 的 threshold 极限,即硬盘缓存 为1M; 使用setSizeThreshold()可以设置最大值。
使用 setRepository(new File(Path));可以设置临时存储文件