一只会飞的白菜 2017-04-18 12:55 采纳率: 0%
浏览 2911
已结题

用file上传的服务器,存到数据库中

jsp :代码

 <div class="main">
        <h2>添加商品</h2>
        <div class="manage">
            <form enctype="multipart/form-data" method="post"  action="/emw/XFL.do"  >
                <table class="form">
                    <tr>
                        <td class="field">商品名称:</td>
                        <td><input type="text" id="spmic" class="text" name="productName"  onblur="return check()"/></td>
                    </tr>
                    <tr>
                        <td class="field">所属分类:</td>
                        <td>
                            <select name="parentId">
                            <%
                              List<XLm>  li=new XLmBizimpl().getAll();
                              for(XLm n:li){
                             %>
                                <option value="<%=n.getXflid() %>"><%=n.getXflname() %></option>
                            <%
                            }
                             %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td class="field">商品图片:</td>
                        <td><input type="file" id="photo" class="text" name="filepath" /></td>
                    </tr>
                    <tr>
                        <td class="field">商品价格:</td>
                        <td><input type="text" id="jiage" class="text tiny" name="jiage" onblur="return moing()" /> 元</td>
                    </tr>
                    <tr>
                        <td class="field">库存:</td>
                        <td><input type="text" id="kucun" class="text tiny" name="kuchun" onblur="return moing1()" /></td>
                    </tr>
                    <tr>
                        <td class="field">条码号:</td>
                        <td><input type="text" id="tiaoma" class="text" name="tiaoma" onblur="return moing2()"/></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><label class="ui-blue"><input type="submit" name="submit" value="添加" /></label></td>
                    </tr>
                </table>
            </form>

sevlre:
package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.biz.impl.GoodsBizimpl;
import com.biz.impl.XLmBizimpl;
import com.entity.Goods;
import com.entity.XLm;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;

public class XFL extends HttpServlet{

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    super.doGet(req, resp);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
   // TODO Auto-generated method stub
      request.setCharacterEncoding("UTF-8");
      response.setCharacterEncoding("UTF-8");


       String productName=request.getParameter("productName");

       int parentId=Integer.parseInt(request.getParameter("parentId"));
       //String filepath=request.getParameter("filepath");
       //int jiage=Integer.parseInt( request.getParameter("jiage"));
       double jiage=Double.parseDouble(request.getParameter("jiage"));
       int kuchun=Integer.parseInt( request.getParameter("kuchun"));
       String tiaoma=request.getParameter("tiaoma");
    //创建一个上传的组件
    SmartUpload su = new SmartUpload();
    //初始化SmartUpload对象(重)
    su.initialize(JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8*1024, true));  //pageContext上下文  -- application

    com.jspsmart.upload.File file = null;  //非IO包下的File类

    String filepath = null; 


    com.jspsmart.upload.Request req  = null;
    //定义允许上传类型
    String allowed = "gif,jpg,png,txt";
    //定义不许上传类型
    String denied = "jsp,asp,php,aspx,html,htm,exe,bat";
    //文件上传类型,二选一,一般会选择允许上传类型
    //定义上传文件大小
    int file_size = 5*1024*1024;  //不能超过2M

    String exceptionMsg = null;  //异常信息对象

    //int i = 0;
    try {


            //定义允许上传文件类型   
            su.setAllowedFilesList(allowed);
            //不允许上传文件类型   
            su.setDeniedFilesList(denied);      
            //单个文件最大限制   
            su.setMaxFileSize(file_size);   
            //设置上传对象的编码                   
            su.setCharset("utf-8");  //GBK   GB2312
            //执行上传(重)
            su.upload();
            //路径


            //得到单个上传文件的信息
            //file = su.getFiles().getFile(0);  //su.getFiles()获得所有文件对象


            //su.getFiles()获得所有文件对象
            Files f= su.getFiles();
            //循环读取文件对象
            for(int i=0; i<f.getCount(); i++){
                //取文件对象
                file=f.getFile(i);

                //判断是否有对象
                if(!file.isMissing()){
                    //设置文件在服务器的保存位置
                    filepath = "upload\\";  //保存到一个upload的文件夹下
                    //filepath += file.getFileName();  //拼接文件名字

                    filepath += System.currentTimeMillis()+ file.getFileName().substring( file.getFileName().lastIndexOf("."));
                    //文件另存为   
                    file.setCharset("utf-8");

                    //保存(重)
                    file.saveAs(filepath, SmartUpload.SAVE_VIRTUAL);  //上传模式
                }
            }


        } catch (Exception e) {
            exceptionMsg = e.getMessage();
            e.printStackTrace();
        }   


       response.getWriter().print(filepath);

      System.out.println(productName+"  "+ parentId+"  "+ filepath+" "+ jiage +"  "+kuchun +"  "+tiaoma);



}

}

错误:SEVERE: Servlet.service() for servlet [XFL] in context with path [/emw] threw exception
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.servlet.XFL.doPost(XFL.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Unknown Source)

问题:获取的值是空值

  • 写回答

4条回答 默认 最新

  • threenewbee 2017-04-18 16:05
    关注

    int parentId=Integer.parseInt(request.getParameter("parentId"));
    //String filepath=request.getParameter("filepath");
    //int jiage=Integer.parseInt( request.getParameter("jiage"));
    double jiage=Double.parseDouble(request.getParameter("jiage"));
    int kuchun=Integer.parseInt( request.getParameter("kuchun"));

            这些参数中某个为空,""等,转换失败
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题