Jin_evan 2016-12-10 03:57 采纳率: 100%
浏览 1523
已采纳

java.lang.NumberFormatException

 package foodOnline.servlet;

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

import javax.servlet.ServletContext;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import foodOnline.dao.FoodDao;
import foodOnline.dao.impl.FoodDaoImpl;
import foodOnline.entity.Food;
//49行categoryId报错说转换异常,看了老半天也改了还是说错误
public class addServlet extends HttpServlet {


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            FileItemFactory factory=new DiskFileItemFactory();
            ServletFileUpload upload=new ServletFileUpload(factory);
            String chineseName=null;
            String englishName=null;
            String description=null;
            String filename=null;

            int categoryId=0;

        try {
            List<FileItem>list=upload.parseRequest(request);
            for(FileItem item:list){
                if(item.isFormField()){
                    if("chineseName".equals(item.getFieldName())){
                        chineseName=item.getString("UTF-8");
                    }else if("englishName".equals(item.getFieldName())){
                        englishName=item.getString("UTF-8");
                    }else if("description".equals(item.getFieldName())){
                        description=item.getString("UTF-8");
                    }else if("categoryId".equals(item.getFieldName())){
                        categoryId = Integer.parseInt(item.getString());
                    }
                }else{
                    String clientFilename=item.getName();
                    String ext = clientFilename.substring(clientFilename
                            .lastIndexOf("."));
                    filename = englishName + ext;
                    ServletContext application=request.getSession().getServletContext();
                    String filepath=application.getRealPath("images/menus/"+filename);
                    item.write(new File(filepath));
                }
            }
            if(chineseName!=null&&englishName!=null&&description!=null&&categoryId!=0){
                Food food=new Food(0,chineseName,englishName,categoryId,description);
                FoodDao fd=new FoodDaoImpl();
                fd.addFoods(food);
                response.sendRedirect("food/admin/foodsListServlet");
            }else{
                request.getRequestDispatcher("error.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.getRequestDispatcher("error.jsp").forward(request, response);

        }

    }

}

然后现在出现新的错误,
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '??…è’???—é??' for key 'ChineseName'

  • 写回答

5条回答 默认 最新

  • 当作看不见 2016-12-10 04:30
    关注

    categoryId = Integer.parseInt(item.getString());
    你把item.getString() 这个东西输出,看一下是不是纯数字的字符串

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错
  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口