简体   繁体   中英

Servlet request.getParameter() always return null value

I'm working on simple client server communication using HttpPost. From the client side I'm setting a parameter(filename).

At the server side when I try to get the parameter value it's always showing null . I tried using MultiPartEntity but even that is not working.

Below is my client code:

            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();

And this is my servlet code:

    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)));

Kindly let me know any possible solution for this issue.

Thanks in advance!

Ur setting parameters wrong... at the client side, do this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM