如图所示,为啥会提示空指针异常还有taste为啥出现两次!
package com.imooc.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
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 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.imooc.bean.Food;
import com.imooc.utils.UploadUtils;
/**
* 保存菜品的Servlet
*/
public class SaveFoodServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//数据接收
//文件上传的基本操作
try{
//定义一个map集合用于保存接收到的数据
Map<String,String> map = new HashMap<String,String>();
//1创建一个磁盘文件项工厂对象
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
//2.创建一个核心解析类
ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
//3.解析request请求,返回的是list集合,list集合中存放的是FileItem对象;
List<FileItem> list = servletFileUpload.parseRequest(request);
//定义一个list集合,用与保存口味
List<String> tasteList = new ArrayList<String>();
String url = null;
//4.遍历集合
for (FileItem fileItem : list) {
//判断是表单项还是文件上传项
if(fileItem.isFormField()){
//普通表单项
//接收表单项参数值
String name = fileItem.getFieldName();
String value = fileItem.getString("utf-8");
System.out.println(name+" "+value);
//接收复选框数据
if("taste".equals(name)){
String tasteValue = fileItem.getString("utf-8");
//接收到一个值,将这个值存入到tastelist中去
tasteList.add(tasteValue);
tasteValue = tasteList.toString().substring(1, tasteList.toString().length()-1);
System.out.println(name+" "+tasteValue);
//将爱好的数据存入发哦map集合中去
map.put(name, tasteValue);
}else{
//将数据存入到map集合中
map.put(name,value);
}
}else{
//文件上传项
//获得文件上传的名称
String fileName = fileItem.getName();
if(fileName !=null && !"".equals(fileName)){
//通过工具类获得唯一文件名
String uuidFileName = UploadUtils.getUUIDFileName(fileName);
//获得文件上传数据
InputStream is = fileItem.getInputStream();
//获得文件的上传路径
String path = this.getServletContext().getRealPath("/upload");
//将输入流对接输出流
url = path+"\\"+uuidFileName;
OutputStream os = new FileOutputStream(url);
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b))!=-1){
os.write(b,0,len);
}
is.close();
os.close();
}
}
}
System.out.println(map);
//封装数据到food中
Food food = new Food();
food.setId(map.get("id"));
food.setName(map.get("name"));
food.setPrice(map.get("price"));
food.setTaste(map.get("taste"));
food.setDescription(map.get("description"));
food.setPath(url);
//将菜品信息存入到list集合中
List<Food> foodList = (List<Food>) this.getServletContext().getAttribute("list");
//检验用户名
for(Food f:foodList){
if(f.getName().equals(map.get("name"))){
request.setAttribute("mag", "该菜品已经注册!");
request.getRequestDispatcher("").forward(request, response);
}
}
foodList.add(food);
for (Food food2 : foodList) {
System.out.println(food2);;
}
this.getServletContext().setAttribute("list", foodList);
//注册成功后,跳转登录页面
request.getSession().setAttribute("foodName", food.getName());
response.sendRedirect(request.getContextPath()+"");
}catch(FileUploadException e){
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
id21
name2121
taste不辣
taste不辣
price222
description222
{id=21, price=222, description=222, name=2121, taste=不辣}
一月 20, 2018 7:31:46 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [SaveFoodServlet] in context with path [/food_management] threw exception
java.lang.NullPointerException
at com.imooc.servlet.SaveFoodServlet.doGet(SaveFoodServlet.java:106)
at com.imooc.servlet.SaveFoodServlet.doPost(SaveFoodServlet.java:128)
正在回答 回答被采纳积分+1
相似问题
登录后可查看更多问答,登录/注册
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星