使用Servlet上传多张图片——Servlet层(ProductServlet.java)

package orz.treeSquirrels.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
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.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import orz.treeSquirrels.entity.ProductInfo;
import orz.treeSquirrels.service.ProductInfoService;
import orz.treeSquirrels.service.impl.ProductInfoServiceImpl;

public class ProductServlet extends HttpServlet {

	
	ProductInfoService proService = new ProductInfoServiceImpl();
	
	/**
	 * (非 Javadoc)
	* <p>Description(描述):doget方法 </p>
	* <p>Title: doGet</p>
	* @param request
	* @param response
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String tag = request.getParameter("tag");
		if (tag.equals("add")) {
			// 添加商品
			addProduct(request, response);
		} 
	}
	
	
	
	
	/**
	 * 
	* @Title: addProduct
	* @Description: 添加食品信息
	* @param @param request
	* @param @param response
	* @param @throws IOException    设定文件
	* @return void    返回类型
	* @throws
	 */
	private void addProduct(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();

		request.setCharacterEncoding("utf-8");
		String produceName = "";			// 商品名称
		String details = "";				// 商品详情
		String price = "";					// 价格
		int stock = 0;						// 库存
		String uploadFileName = "";			// 上传的文件名(大图)
		String uploadFileName1 = "";		// 上传的文件名(小图1)
		String uploadFileName2 = "";		// 上传的文件名(小图2)
		String uploadFileName3 = "";		// 上传的文件名(小图3)
		String fileName = "";				// 表单字段元素的name属性值
		String filedName = "";				//获取文件名称
		String picPath = "";
		// 请求信息中的内容是否是multipart类型
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		// 上传文件的存储路径(服务器文件系统上的绝对文件路径)
		String uploadFilePath = request.getSession().getServletContext()
				.getRealPath("upload/");
		if (isMultipart) {
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			try {
				// 解析from表单中所有文件
				@SuppressWarnings("unchecked")
				List<FileItem> items = upload.parseRequest(request);
				Iterator<FileItem> iter = items.iterator();
				while (iter.hasNext()) {
					FileItem item = (FileItem) iter.next();
					if (item.isFormField()) {
						// 判断,是普通表单
						fileName = item.getFieldName();
						// 表单字段的name属性
						if (fileName.equals("produceName")) {
							produceName = item.getString("utf-8");
						} else if (fileName.equals("details")) {
							details = item.getString("utf-8");
						} else if (fileName.equals("price")) {
							price = item.getString("utf-8");
						} else if (fileName.equals("stock")) {
							stock = Integer.parseInt(item.getString("utf-8"));
						}
					} else {
						// 文件表单字段
						// 表单字段的name属性
						fileName = item.getFieldName();
						if (fileName.equals("bigfile")) {
							filedName = item.getName();
							if (fileName != null && !filedName.equals("")) {
								File fullFile = new File(item.getName());
								File saveFile = new File(uploadFilePath,
										fullFile.getName());
								item.write(saveFile);
								uploadFileName = fullFile.getName();
								picPath = saveFile.toString();
							}
						} else if (fileName.equals("smallfile1")) {
							// 小图1
							filedName = item.getName();
							if (fileName != null && !filedName.equals("")) {
								File fullFile = new File(item.getName());
								File saveFile = new File(uploadFilePath,
										fullFile.getName());
								item.write(saveFile);
								uploadFileName1 = fullFile.getName();
								picPath = saveFile.toString();
							}
						} else if (fileName.equals("smallfile2")) {
							// 小图2
							filedName = item.getName();
							if (fileName != null && !filedName.equals("")) {
								File fullFile = new File(item.getName());
								File saveFile = new File(uploadFilePath,
										fullFile.getName());
								item.write(saveFile);
								uploadFileName2 = fullFile.getName();
								picPath = saveFile.toString();
							}
						} else if (fileName.equals("smallfile3")) {

							// 小图3
							filedName = item.getName();
							if (fileName != null && !filedName.equals("")) {
								File fullFile = new File(item.getName());
								File saveFile = new File(uploadFilePath,
										fullFile.getName());
								item.write(saveFile);
								uploadFileName3 = fullFile.getName();
								picPath = saveFile.toString();
							}
						}
					}
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			ProductInfo product = new ProductInfo();
			product.setProductName(produceName);
			product.setPrice(Integer.parseInt(price));
			product.setDetails(details);
			product.setStock(stock);
			product.setFileName("upload/" + uploadFileName);
			product.setFileName_1("upload/" + uploadFileName1);
			product.setFileName_2("upload/" + uploadFileName2);
			product.setFileName_3("upload/" + uploadFileName3);
			int rel = proService.addProductInfo(product);
			if (rel > 0) {
				// 添加商品成功之后转发到查询所有的商品界面
				out.print("<script>alert('恭喜您,添加商品成功!');location.href='ProductInfoServlet?tag=show';</script>");
			} else {
				out.print("<script>alert('很抱歉,添加商品失败!');location.href='ProductInfoServlet?tag=show';</script>");

			}
		}

	}

	
	
	
	
	
	
	
	
	
	

	/**
	 * (非 Javadoc)
	* <p>Description(描述): dopost方法</p>
	* <p>Title: doPost</p>
	* @param request
	* @param response
	* @throws ServletException
	* @throws IOException
	* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doGet(request, response);
	}

}

原文地址:https://www.cnblogs.com/a1111/p/12816134.html