⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mcaddservlet.java

📁 电子商城
💻 JAVA
字号:
package com.lmh.servlet.admin;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import com.lmh.dao.impl.OracleMcDAO;
import com.lmh.dao.impl.OracleMcTypeDAO;
import com.lmh.dao.vo.McBean;
import com.lmh.pub.EncodingTool;

public class McAddServlet extends HttpServlet {

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String title = "";
		String url = "";
		String content = "";
		
		OracleMcDAO mcDao = new OracleMcDAO();
		McBean mcBean = new McBean();

		// 上传图片处理
		DiskFileUpload dfu = new DiskFileUpload();
		dfu.setSizeMax(1024 * 1024 * 10); // 设置每次做大上传容量
		dfu.setSizeThreshold(4096); // 每次读取4K
		String str = this.getServletContext().getRealPath("/mcimg"); // 获取绝对路径
		dfu.setRepositoryPath(str);
		try {
			List list = dfu.parseRequest(request);
			for (Object obj : list) {
				FileItem fi = (FileItem) obj;
				if (fi.isFormField()) {
					if (fi.getFieldName().equals("sname")) {
						mcBean.setSname(fi.getString());
					} else if (fi.getFieldName().equals("nmaxid")) {
						String nmaxid_str = fi.getString();
						try {
							mcBean.setNmaxid(Integer.parseInt(nmaxid_str));
						} catch (Exception e) {
						}
					} else if (fi.getFieldName().equals("nminid")) {
						String nminid_str = fi.getString();
						try {
							mcBean.setNminid(Integer.parseInt(nminid_str));
						} catch (Exception e) {
						}
					} else if (fi.getFieldName().equals("nprice")) {
						String nprice_str = fi.getString();
						try {
							mcBean.setNprice(Double.parseDouble(nprice_str));
						} catch (Exception e) {
						}
					} else if (fi.getFieldName().equals("sdescription")) {
						mcBean.setSdescription(fi.getString());
					} else if (fi.getFieldName().equals("smctag")) {
						mcBean.setSmctag(fi.getString());
					}

				} else {
					String imgName = fi.getName(); // 得到文件域的值
					fi.write(new File(str + "/" + getFileName(imgName)));
					mcBean.setSimg(getFileName(imgName));
				}
				
			}
		} catch (Exception e) {
		}
		mcDao.insertMc(mcBean);
		
		content ="商品添加成功!";
		title = "添加成功";
		url="/lmhshop/admin/mcManager/mcInfo.jsp";
		
		content = content + "本页面将在5秒后自动跳转";

		title = EncodingTool.encoder(title);
		content = EncodingTool.encoder(content);
		url = EncodingTool.encoder(url);

		response.sendRedirect("/lmhshop/publicInfo.jsp?title=" + title
				+ "&url=" + url + "&content=" + content);
	}

	private String getFileName(String imgName) {
		String fileName = null;
		String exStr = imgName.substring(imgName.lastIndexOf("."));
		java.util.Date myDate = new java.util.Date();
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyyMMddHHmmss");
		fileName = sdf.format(myDate) + exStr;
		return fileName;
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		OracleMcTypeDAO mcTypeDao = new OracleMcTypeDAO();

		List mcTypeList = mcTypeDao.searchMcType(-1, null);
		request.setAttribute("mcTypeList", mcTypeList);

		RequestDispatcher rd = null;
		rd = request.getRequestDispatcher("/admin/mcManager/mcManager.jsp");
		rd.forward(request, response);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -