【问题标题】:How to through a java application to transfer data to the servlet如何通过一个java应用程序向servlet传输数据
【发布时间】:2013-08-20 09:12:34
【问题描述】:

我想通过一个java应用程序发送一个json字符串,在servlet中没有得到这个json,我该怎么办,谁能帮助我? 这是我的 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);
}

这是我的 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();

【问题讨论】:

  • 您的代码从不发送 JSON。将其发送到连接的输出流,然后准确地告诉你发生了什么。
  • 我试过这样的输出流OutputStream out = urlconn.getOutputStream(); out.write(json.getBytes("UTF-8")); out.flush(); out.close();但是不行
  • “它不起作用”对问题的描述非常糟糕。什么不起作用?它怎么行不通?如果遇到异常,堆栈跟踪是什么?当你去看医生时,你不会只说“我病了,很痛”。你描述症状,你的感受,在哪里。这里也一样。
  • 我的意思是,我没有找到在我的 servlet 中获取这个 json 的方法
  • 那么,您的意思是您遇到的问题是您不知道如何获取servlet 中的JSON?那么为什么不明确地问这个问题,为什么不显示 servlet 代码而不是客户端代码呢? servlet 是否被调用? servlet 中有调试跟踪吗?

标签: java json jsp servlets


【解决方案1】:

您应该将 json 数据写入 outputStream 作为 out.write(json.getBytes()); .请看下面的完整代码:

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

【讨论】:

  • 虽然这里还可以,但更好json.getBytes("UTF-8")flush 不需要。 StringBuilderi.o. StringBuffer - 即使问题使用了这些。
  • @lee tan,您是否尝试过给定的解决方案?
【解决方案2】:

试试这样的

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

【讨论】:

    猜你喜欢
    • 2019-10-25
    • 2017-05-17
    • 2013-01-01
    • 1970-01-01
    • 2015-06-21
    • 2012-09-04
    • 1970-01-01
    • 2023-03-08
    • 2011-08-02
    相关资源
    最近更新 更多