?? mcaddservlet.java
字號(hào):
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); // 設(shè)置每次做大上傳容量
dfu.setSizeThreshold(4096); // 每次讀取4K
String str = this.getServletContext().getRealPath("/mcimg"); // 獲取絕對(duì)路徑
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 + "本頁(yè)面將在5秒后自動(dòng)跳轉(zhuǎn)";
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);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -