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 程序哪有错误怎么改?
  • ¥15 交换机和交换机之间的链路带宽以及主机带宽的理解
  • ¥15 ai创想家对战模式代码
  • ¥15 集合A由3个2行4列二维数组构成,从集合A中任意取一个二维数组元素、如果该二维数组元素的对应列位置的上、下两数都是奇数,而且仅有2个列是奇数/奇数,则该数组有意义,并放入集合B中打印输出。
  • ¥15 电信IPV6 无法外网访问吗
  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式
  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM