简体   繁体   English

如何通过Java应用程序将数据传输到Servlet

[英]How to through a java application to transfer data to the servlet

I want to pass a java application sent a string that is a json,it isn't get this json in servlet, how do I do, who can help me? 我想通过一个Java应用程序发送一个字符串,该字符串是一个json,它在servlet中没有得到这个json,我该怎么办,谁可以帮助我? This is my servlet code: 这是我的servlet代码:

public void service(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException {
    BufferedInputStream in = new BufferedInputStream(req.getInputStream());
    byte[] data = null;
    byte[] bts = new byte[1024];
    int index;
    while ((index = in.read(bts)) >= 0) {
        if (data == null) {
            data = new byte[index];
            System.arraycopy(bts, 0, data, 0, index);
        }
        else {
            byte[] tmp = data;
            data = new byte[tmp.length + index];
            System.arraycopy(tmp, 0, data, 0, tmp.length);
            System.arraycopy(bts, 0, data, tmp.length, index);
        }
    }
    String json = new String(data);
            System.out.print(json);
}

and this is my java application: 这是我的Java应用程序:

String _url = "http://localhost:8080/jsf/test";
    URL url = null;
    HttpURLConnection urlconn = null;
    String json = URLEncoder.encode(JSONObject.fromObject(req)
            .toString(), "UTF-8");
    url = new URL(_url);
    urlconn = (HttpURLConnection) url.openConnection();
    urlconn.setRequestMethod("POST");
    urlconn.setDoOutput(true);
    urlconn.setDoInput(true);
    OutputStream out = urlconn.getOutputStream();
    out.write(json.getBytes("UTF-8"));
    out.flush();
    out.close();
    BufferedReader rd = new BufferedReader(new InputStreamReader(
            urlconn.getInputStream(), "UTF-8"));
    StringBuffer sb = new StringBuffer();
    int ch;
    while ((ch = rd.read()) > -1) {
        sb.append((char) ch);
    }
    System.out.println(sb);
    rd.close();

You should write json data to outputStream as out.write(json.getBytes()); 您应该将json数据作为out.write(json.getBytes())写入outputStream。 . See the complete code below: 请参阅下面的完整代码:

String _url = "http://localhost:8080/jsf/test";
URL url = new URL(_url);
String json = "[{\"id\":\"DFAB8108D69642EBBD461160B4519B0F\",\"name\":\"name1\"},{\"id\":\"B048B2521A5619DCE0440003BA11CFDA\",\"name\":\"name2\"}]";
HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
urlconn.setRequestMethod("POST");
urlconn.setDoOutput(true);

OutputStream out = urlconn.getOutputStream();
out.write(json.getBytes());
out.flush();
out.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(
        urlconn.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer();
int ch;
while ((ch = rd.read()) > -1) {
    sb.append((char) ch);
}
System.out.println(sb.toString());
rd.close();

Try something like this 试试这个

String _url = "http://localhost:8080/jsf/test";
URL url = new URL(_url);
String json = "[{\"id\":\"DFAB8108D69642EBBD461160B4519B0F\",\"name\":\"name1\"},{\"id\":\"B048B2521A5619DCE0440003BA11CFDA\",\"name\":\"name2\"}]";
HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();

    urlconn .setDoOutput(true);
    urlconn .setRequestProperty("Content-Type", "application/json");
    urlconn .setRequestProperty("Accept", "application/json");
    urlconn .setRequestMethod("POST");
    urlconn .connect();

    OutputStream os = httpcon.getOutputStream();
    os.write((json.getBytes("UTF-8"));

    os.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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