Servlet request.getParameter()始终返回null值

时间:2013-08-22 11:12:01

标签: java servlets

我正在使用HttpPost进行简单的客户端服务器通信。从客户端我正在设置一个参数(文件名)。

在服务器端,当我尝试获取参数值时,它始终显示 null 。我尝试使用 MultiPartEntity ,但即使这样也无效。

以下是我的客户代码:

            HttpPost httppost = new HttpPost("http://xxx.xxx.xxx.xxx:yyyy");
            InputStreamEntity reqEntity = new InputStreamEntity(
                    new FileInputStream(dataFile), -1);
            reqEntity.setContentType("binary/octet-stream");

            // Send in multiple parts if needed
            reqEntity.setChunked(true);
            httppost.setEntity(reqEntity);

            //setting the parameter
            httppost.getParams().setParameter("filename", "xxxx.xml");
            HttpResponse response = httpclient.execute(httppost);
            int respcode =  response.getStatusLine().getStatusCode();

这是我的servlet代码:

    response.setContentType("binary/octet-stream");
    Scanner scanner = new Scanner(request.getInputStream());

    // reading the parameter
    String filename = request.getParameter("filename");
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\" + filename)));

请让我知道这个问题的任何可能的解决方案。

提前致谢!

1 个答案:

答案 0 :(得分:-1)

Ur设置参数错误...在客户端,执行此操作:

ArrayList<NameValuePair> postParameters = postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("filename", "xxxx.xml");
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);