如何使用tomcat和java编写远程文件。我使用这个java代码:
HttpURLConnection connexion = null;
try {
URL("http://localhost:8080/GeneralSemanticWebService/Models/pervasiveSystemDescription1.txt");
System.out.println("Connexion a l'url ...");
connexion = (HttpURLConnection) url.openConnection();
connexion.setAllowUserInteraction(true);
connexion.setDoOutput(true);
connexion.setRequestProperty("Content-Length", (""+ texte.length()));
connexion.setRequestProperty("Content-Type", "RDF/XML-ABBREV");
PrintWriter out = new PrintWriter(connexion.getOutputStream());
if (connexion.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println(connexion.getResponseMessage());
} else {
System.out.println("Ecriture ...");
out.write("okkkk");
out.close();}
} catch (Exception e) {
e.printStackTrace();
} finally {
connexion.disconnect();
}
System.exit(0);
在输出中我得到了这个结果:
INFOS: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
janv. 06, 2015 6:27:32 PM org.apache.coyote.AbstractProtocol start
INFOS: Starting ProtocolHandler ["http-nio-8080"]
janv. 06, 2015 6:27:32 PM org.apache.coyote.AbstractProtocol start
INFOS: Starting ProtocolHandler ["ajp-nio-8009"]
janv. 06, 2015 6:27:32 PM org.apache.catalina.startup.Catalina start
INFOS: Server startup in 54713 ms
Connexion a l'url ...
Ecriture ...
janv. 06, 2015 6:27:44 PM org.apache.coyote.AbstractProtocol pause
INFOS: Pausing ProtocolHandler ["http-nio-8080"]
,服务器已停止。 请回复我
答案 0 :(得分:1)
在此代码路径中,您将关闭out
,这是一个围绕连接的OutputStream的PrintWriter包装器,然后调用connextion.disconnect()
。我怀疑.disconnect()
方法会再次尝试关闭流,导致异常或您未捕获的错误。我建议删除out.close()
,然后重试。
答案 1 :(得分:-1)
我使用了这段代码:
HttpURLConnection connexion = null;
try {
URL url = new URL("http://localhost:8080/GeneralSemanticWebService/Models/pervasiveSystemDescription1.txt");
System.out.println("Connexion a l'url ...");
connexion = (HttpURLConnection) url.openConnection();
connexion.setAllowUserInteraction(true);
connexion.setDoOutput(true);
connexion.setRequestProperty("Content-Length", (""+ texte.length()));
connexion.setRequestProperty("Content-Type", "RDF/XML-ABBREV");
PrintWriter out = new PrintWriter(connexion.getOutputStream());
if (connexion.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println(connexion.getResponseMessage());
} else {
System.out.println("Ecriture ...");
out.write("okkkk");
}
} catch (Exception e) {
e.printStackTrace();
}
但文件pervasiveSystemDescription1.txt未被修改,并且字符串(" okkkk")未写入此文件。
感谢您的考虑。