我是Java的新手,我应该将XML文件发送到HTTP服务器。应首先将XML文件转换为字符串:
我从服务器收到错误:
[stdout] (http--127.0.0.1-8080-1) failed in postjava.lang.NullPointerException
[stdout] (http--127.0.0.1-8080-1) received post request :
我似乎没有向服务器发送任何内容。
以下是代码
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
public class try_post {
public static void main(String[] args) {
try{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("D:\\n.xml");
StringWriter stringWriter = new StringWriter();
Transformer transformer =TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(stringWriter));
String strFileContent = stringWriter.toString();
System.out.println(strFileContent);
System.out.println( strFileContent.getClass().getName());
System.out.println("xml file converted to string");
String param="param1=" + URLEncoder.encode(strFileContent ,"UTF-8");
URL url = new URL("http://localhost:8080/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
// connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setFixedLengthStreamingMode(param.getBytes().length);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(param);
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
writer.close();
reader.close();
String response= "";
StreamSource source = new StreamSource(fileReader);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);*/
DataOutputStream fos = new DataOutputStream(connection.getOutputStream ());
System.out.println("Response code: " + connection.getResponseCode());
connection.disconnect();}
catch (MalformedURLException ex) {
System.out.print("MalformedURLException");
} catch (Exception ex) {
System.out.print(" Exception: "+ex.getMessage());
}
}
}
答案 0 :(得分:3)
根据您发布的内容,没有人可以提供帮助。堆栈跟踪必须具有更多。
您的代码存在许多问题:
看起来您正在尝试从文件中读取XML并将其POST到侦听localhost的servlet上。您只需检查响应代码即可。我错过了什么?
根据我的口味,你几乎没有足够的防守能力。难怪你遇到了问题。
打印堆栈跟踪,如下所示:
catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
另一个聪明的尝试是在像IntelliJ这样的IDE中运行,并使用调试器逐步执行代码。你会明白为什么你在比这里等待答案的时间更短的时间内获得NPE。
答案 1 :(得分:2)
哦,我明白了。只需使用Ruby。
require "net/http"; require "uri"
params = { param1: File.read("D:\\n.xml") }
uri = URI.parse("http://localhost:8080/")
resp = Net::HTTP.post_form(uri, params)
puts "Response code: #{resp.code}"
抱歉,我必须这样做。 * downvotes的大括号*
答案 2 :(得分:1)
异常发生在服务器端,而不是代码中。是否在服务器上记录了异常堆栈跟踪?您可能需要查看服务器端代码以进行调试。
答案 3 :(得分:0)
从我所看到的,你的做法很难。尝试使用HTTPClient库。 这应该可以简化您的代码。
对于maven依赖项,请参阅: http://search.maven.org/#artifactdetails%7Ccommons-httpclient%7Ccommons-httpclient%7C3.1%7Cjar
有关工作代码示例,请参阅: 请参阅:http://www.kodejava.org/browse/47.html