为什么java getParameter()获取的值是null
我发送的请求localhost:8080/1009servlet01/form1.jsp这是form1.jsp中的表单<formaction="t"method="pos...
我发送的请求 localhost:8080/10 09servlet01/form1.jsp
这是form1.jsp中的表单
<form action="t" method="post" enctype="multipart/form-data">
name:<input name="name" /><br/>
photo:<input type="file" name="file1" /><br/>
<input type="submit" value="提交" />
</form>
比如,我在name中 输入 xj
photo在servlet中是可以获取的,只要是 xj怎么获取不到啊
XML 请求对应的servlet代码是:------------------------------------------
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
InputStream ips = req.getInputStream();
DiskFileItemFactory dfif = new DiskFileItemFactory();
ServletFileUpload sful = new ServletFileUpload(dfif);
String s = req.getParameter("name");
System.out.println(s);//这里获取的一直是null为什么啊
try {
List<FileItem> fileitem = sful.parseRequest(req);
for(FileItem e : fileitem)
{
if(!e.isFormField())
{
ServletContext sct = getServletContext();
File file = new File(sct.getRealPath("upload")+"\\"+e.getName());
e.write(file);
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} 展开
这是form1.jsp中的表单
<form action="t" method="post" enctype="multipart/form-data">
name:<input name="name" /><br/>
photo:<input type="file" name="file1" /><br/>
<input type="submit" value="提交" />
</form>
比如,我在name中 输入 xj
photo在servlet中是可以获取的,只要是 xj怎么获取不到啊
XML 请求对应的servlet代码是:------------------------------------------
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
InputStream ips = req.getInputStream();
DiskFileItemFactory dfif = new DiskFileItemFactory();
ServletFileUpload sful = new ServletFileUpload(dfif);
String s = req.getParameter("name");
System.out.println(s);//这里获取的一直是null为什么啊
try {
List<FileItem> fileitem = sful.parseRequest(req);
for(FileItem e : fileitem)
{
if(!e.isFormField())
{
ServletContext sct = getServletContext();
File file = new File(sct.getRealPath("upload")+"\\"+e.getName());
e.write(file);
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} 展开
2个回答
2013-10-07
展开全部
你现在的表单上传格式是:enctype=“multipart/form-data”
在这种格式下不起作用request.getParameter()
for(FileItem e : fileitem) {
if(e.isFormField()){
if(e.getFieldName().equals("name")){
name =e.getString("UTF-8"); //注意,你从这里才能取到值
//UTF-8是你上传页面的编码格式,防止中文乱码,
//对也这种类型的表单提交数据,setCharacterEncoding也是不起作用的
} else if(!e.isFormField()){
ServletContext sct = getServletContext();
File file = new File(sct.getRealPath("upload")+"\\"+e.getName());
e.write(file);
}
在这种格式下不起作用request.getParameter()
for(FileItem e : fileitem) {
if(e.isFormField()){
if(e.getFieldName().equals("name")){
name =e.getString("UTF-8"); //注意,你从这里才能取到值
//UTF-8是你上传页面的编码格式,防止中文乱码,
//对也这种类型的表单提交数据,setCharacterEncoding也是不起作用的
} else if(!e.isFormField()){
ServletContext sct = getServletContext();
File file = new File(sct.getRealPath("upload")+"\\"+e.getName());
e.write(file);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询