文件上传
来源:互联网 发布:nat端口复用的命令 编辑:程序博客网 时间:2024/06/04 17:58
判断是否是文件上传
public static final boolean isMultipartContent(HttpServletRequest request)
{
if(!"post".equals(request.getMethod().toLowerCase()))
return false;
String contentType = request.getContentType();
if(contentType == null)
return false;
return contentType.toLowerCase().startsWith("multipart/");
}
package cn.wuyou.controllerServlet;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 cn.wuyou.dobean.Shangpin;
import cn.wuyou.service.impl.ManagerService;
public class AddSPServlet extends HttpServlet {
private Map<String,String> fields = new HashMap<String,String>();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
try {
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List /* FileItem */items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
HttpSession session = request.getSession();
int cp_xiaoType = (Integer)session.getAttribute("xiaoTypeId");
int cp_daType = (Integer)session.getAttribute("daTypeId");
String cp_name = fields.get("hw_name");//产品名称
String cp_price = fields.get("hw_price1");//产品 市场价格
String cp_pp = fields.get("pinpai_id");//品牌
String cp_kucun = fields.get("hw_kucun");//库存
String cp_bianhao = fields.get("hw_sn");//产品编号
String cp_localPrice = fields.get("hw_price2");// 本站价格
String cp_px = fields.get("hw_xu");//排序号
String cp_select1 = fields.get("kxaa");//可选 项 1名称
String cp_select1_content = fields.get("kxab");//可选项1 内容
String cp_select2 = fields.get("kxba");//可选 项2名称
String cp_select2_content = fields.get("kxbb");//可选项2 内容
String cp_simple = fields.get("hw_content");//商品简介
String cp_xiangqi = fields.get("hw_content1");//商品详情
String cp_zx = fields.get("tjsort");//最新上架
String cp_jp = fields.get("tjindex");//精品推荐
String cp_tj = fields.get("tejia");//特价商品
String cp_path = fields.get("hw_pic");//商品图片的路径
//创建一个商品对象,将数据封装
Shangpin shangpin = new Shangpin();
shangpin.setSpId(Integer.parseInt(cp_bianhao));
shangpin.setXlbId(cp_xiaoType);
shangpin.setPpId(Integer.parseInt(cp_pp));
shangpin.setDlbId(cp_daType);
shangpin.setSpName(cp_name);
shangpin.setSpPrice(Float.parseFloat(cp_price));
shangpin.setSpBprice(Float.parseFloat(cp_localPrice));
shangpin.setSpCount(Integer.parseInt(cp_kucun));
shangpin.setSpInfo(cp_simple);
shangpin.setSpIntrocuce(cp_xiangqi);
shangpin.setSpUlr(cp_path);
shangpin.setSpJp("1".equals("cp_jp"));
shangpin.setSpTj("1".equals("cp_tj"));
shangpin.setSpZx("1".equals("cp_zx"));
shangpin.setSpPx(Integer.parseInt(cp_px));
shangpin.setSpTz1Lr(cp_select1_content);
shangpin.setSpTz1Name(cp_select1);
shangpin.setSpTz2Lr(cp_select2_content);
shangpin.setSpTz2Name(cp_select2);
ManagerService manager = new ManagerService();
int flag = manager.addShangPin(shangpin);
if(flag==1){
request.setAttribute("MSG", "添加成功");
}else{
request.setAttribute("MSG", "添加失败");
}
request.getRequestDispatcher("ad_addhw.jsp").forward(request, response);
}
private void processUploadedFile(FileItem item) throws Exception {
// Process a file upload
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String fileName = item.getName();
long sizeInBytes = item.getSize();
// Process a file upload
if (fileName != null && !(fileName=fileName.trim()).equals("")) {
int index = fileName.lastIndexOf("\\");
if (index != -1) {
fileName = fileName.substring(index);
}
String pic_path = this.getServletContext().getRealPath("images");
File uploadedFile = new File(pic_path +"\\"+ fileName);
item.write(uploadedFile);
}
}
}
public String getFieldValue(String fieldName) {
return (String) this.fields.get(fieldName);
}
private void processFormField(FileItem item) {
try {
String fieldName = item.getFieldName();
String fileName = item.getName();
String tValue = item.getString();
this.fields.put(fieldName, tValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 文件上传---文件上传
- 上传文件
- 文件上传
- 上传文件
- 文件上传
- 文件上传
- 上传文件
- 上传文件
- 文件上传
- 文件上传
- 文件上传
- 文件上传
- 文件上传
- 文件上传
- 文件上传
- 上传文件
- 文件上传
- 文件上传
- C++虚继承
- Linux中对MySQL优化
- Android 开源项目
- 博客搬家到csdn
- leetCode解题报告5道题(三)
- 文件上传
- zend_framework中需要注意的两个问题
- java中的Collections类
- Android利用Fiddler进行网络数据抓包
- Codeforces #239 div2
- 旋转式放缩消隐
- WIFI笔记
- 马来西亚闹剧该结束了,马航370波音飞机难道启动了自毁程序?
- tomcat serverl.xml