如何将文件从servlet传递到存在于不同服务器上的servlet?

时间:2016-03-01 14:26:26

标签: java servlets

现在我尝试使用HttpURLConnection但它不起作用,我不确定这是否正确。请建议谢谢。

// RequestDispatcher rd =
// request.getRequestDispatcher("http://192.168.1.2:8080/EEXAM/WindowsWordToHtmlServlet");
// rd.forward(request, response);

HttpURLConnection connection = null;
OutputStream os = null;
BufferedWriter writer = null;
try {
    connection = (HttpURLConnection) new URL("/Servlet").openConnection();
    connection.setReadTimeout(0);
    connection.setConnectTimeout(0);
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestProperty("Accept-Charset", CommonConstants.CHAR_ENCODING);
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + CommonConstants.CHAR_ENCODING);
    os = connection.getOutputStream();
    writer = new BufferedWriter(new OutputStreamWriter(os, CommonConstants.CHAR_ENCODING));
    writer.flush();
    connection.getResponseCode();

} catch (Exception e) {
    CustomLogger.error(e.getMessage(), e, request);
    e.printStackTrace();
}

0 个答案:

没有答案