0

我正在使用 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)));

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

提前致谢!

4

1 回答 1

-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);
于 2013-08-22T11:26:32.653 回答