求救啊!!!为什么我用post方法向服务器传数据的时候取到的总是为NULL在线等
这个是html代码
<HTML>
<HEAD>
<TITLE> Image File </TITLE>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</HEAD>
<FORM METHOD=post ACTION= "tryagain1" ENCTYPE="multipart/form-data">
<INPUT TYPE= "file" NAME= "image"> <BR>
<INPUT TYPE= "text" NAME= "try"> <BR>
<INPUT TYPE= "submit"> </FORM>
<BODY>
</BODY>
</HTML>
这个是服务器的post方法的代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(this.getServletConfig(),request,response);
request.setCharacterEncoding("gb2312");
String s=mySmartUpload.getRequest().getParameter("try");//这里得到的总是为空,为什么
System.out.println(s); try { mySmartUpload.upload();
} catch (SmartUploadException e) {
e.printStackTrace();
}
System.out.println(mySmartUpload.getFiles().getCount());
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++)
{
SmartFile myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
//取得上载的文件的文件名
String allpath=myFile.getFilePathName()
java.io.File file = new java.io.File(allpath);
java.io.FileInputStream fis = new java.io.FileInputStream(file);
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:try","sa","sa");
String sql= "insert into teyimage(id,content,image1) values(5,?,?) ";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1,"5555");
pstmt.setBinaryStream(2,fis,fis.available());
pstmt.execute();
con.close();
System.out.println( "Success,You Have Insert an Image Successfully ");
System.out.println(file.length());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
------解决方案--------------------String s=reqeust.getParameter("try");//直接这样不行吗?
------解决方案--------------------先保证了String s=reqeust.getParameter("try");能获取到再试你的那种
涉及的地方很少的,很快能试出来
------解决方案--------------------<FORM METHOD=post ACTION= "tryagain1" ENCTYPE="multipart/form-data">
请再post的左右加引号
------解决方案--------------------应该是这个POST没加双引号的原因了,还有直接用reqeust.getParameter("try");应该就可以了,不需要NEW了的。
------解决方案--------------------ENCTYPE="multipart/form-data"
你用了文件上传的方式提交,servlet里肯定是拿不到request.getparameter()
------解决方案--------------------
我是想说吧mySmartUpload.upload()放在mySmartUpload.initialize(this.getServletConfig(),request,response)之后mySmartUpload.getRequest().getParameter("try");之前,表述有点问题....