servlet使用二进制流传送请求参数,request.getPart("")取不到值
这是JSP页面里的一个表单:
<div id="main_content02" style="display:">
<table >
<form name="product" id="product" action="InsertProductServlet" method="post" enctype="multipart/form-data">
<tr>
<td id="tit">物品类别:</td>
<td><select name="product_class" id="text">
<option value="0">数码产品</option>
<option value="1">家庭电器</option>
<option value="2">鞋包配饰</option>
<option value="3">汽车摩托</option>
<option value="4">护肤彩妆</option>
<option value="5">珠宝手表</option>
</select></td>
</tr>
<tr>
<td id="tit">物品名称:</td>
<td><input name="productName" type="text" id="text"/></td>
</tr>
<tr>
<td id="tit">底价:</td>
<td><input name="productPrice" type="text" id="text"/></td>
</tr>
<tr>
<td id="tit">价值:</td>
<td><input name="productCost" type="text" id="text"/></td>
</tr>
<tr>
<td id="tit">物品图片:</td>
<td ><input name="upfile" type="file" id="text" value="上传" style="width:228px;"/>
<input type="button" style="background-color:#900; width: 50px; height: 20px;
color: #FFF; border: none; line-height: 20px" value="上传" onclick="userIsExists()"/>
<input type="hidden" name="path" value="${sessionScope.image}"/>
</td>
</tr>
<tr>
<td id="tit" align="left">截止日期:</td>
<td><input name="productCloseTime" type="text" id="text"/></td>
</tr>
<tr>
<td id="tit" valign="top">物品描述:</td>
<td><textarea name="productDescribe" cols="" rows="" id="Describe"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="hidden" name="user" value="${sessionScope.client.clientId}"/>
<input type="submit" id="client_btn" value="提交"/>
</td>
</tr>
</form>
</table>
用servlet来获取值:
import auction.dao.ProductDao;
import auction.dao.impl.ProductDaoImpl;
import auction.entity.Product;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet(name = "InsertProductServlet", urlPatterns = {"/InsertProductServlet", "/html/InsertProductServlet"})
public class InsertProductServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//获取用户输入的数据
PrintWriter out = response.getWriter();
String productName=getParameter(request,"productName");
String classId=getParameter(request,"product_class");
String clientId=getParameter(request,"user");
String productPrice=getParameter(request,"productPrice");
String productDescribe=getParameter(request,"productDescribe");
String productCost=getParameter(request,"productCost");
String productCloseTime=getParameter(request,"productCloseTime");
String productImage=getParameter(request,"path");
String msg="";
//验证数据的有效性
if(classId.isEmpty()){
msg="数据提取遇到错误,请重试";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
if(clientId.isEmpty()){
msg="数据提取遇到错误,请重试";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
if(productName.isEmpty()){
msg="请输入商品名称";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
if(productPrice.isEmpty()){
msg="请输入商品单价";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
if(productDescribe.isEmpty()){
msg="请描述一下商品";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
if(productCost.isEmpty()){
msg="请输入商品价值";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
if(productCloseTime.isEmpty()){
msg="请输入截止时间";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
if(productImage.isEmpty()){
msg="图片地址获取失败,请检查是否成功上传图片";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
Product product=new Product();
product.setClassId(Integer.valueOf(classId));
product.setClientId(clientId);
product.setProductName(productName);
product.setProductPrice(Double.valueOf(productPrice));
product.setProductDescribe(productDescribe);
product.setProductCost(Double.valueOf(productCost));
product.setProductCloseTime(productCloseTime);
product.setProductImage(productImage);
ProductDao productDao=new ProductDaoImpl();
int ret=productDao.insertProduct(product);
if(ret>0){
response.sendRedirect("back_client_index.jsp");
}else{
msg="商品信息添加失败";
request.getRequestDispatcher("err.jsp?msg="+msg).forward(request, response);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
private String getParameter(HttpServletRequest req,String name) throws IOException, IllegalStateException, ServletException{
String retValue=null;
Part part=req.getPart(name);
BufferedReader br=new BufferedReader(new InputStreamReader(part.getInputStream()));
retValue=br.readLine();//读取请求参数值
return retValue;
}
}
可是getParameter方法里面的那个Part part=req.getPart(name);返回的是个NULL值,所以BufferedReader br=new BufferedReader(new InputStreamReader(part.getInputStream()));抛出了空指针异常:我用的服务器是Tomcat,IDE是netbeans7.3.1
SEVERE: Servlet.service() for servlet [InsertProductServlet] in context with path [/auction] threw exception
java.lang.NullPointerException
at auction.servlet.InsertProductServlet.getParameter(InsertProductServlet.java:153)
at auction.servlet.InsertProductServlet.processRequest(InsertProductServlet.java:46)
at auction.servlet.InsertProductServlet.doPost(InsertProductServlet.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
[解决办法]
req.getPart(name)?
HttpServletRequest 有这个方法吗??
[解决办法]
HttpServletRequest 有getPart方法?!