[英]How to open a path in remote machine and write a file in that path using java
[英]how to write a remote file using the tomcat and java
如何使用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"]
並且服務器已停止。 請回應我
在此代碼路徑,你收out
,這大約是connextion的OutputStream的一個PrintWriter包裝,然后調用connextion.disconnect()
我懷疑.disconnect()
方法試圖再次關閉流,從而導致異常或您未捕獲的某些錯誤。 我建議刪除out.close()
並再試一次。
我使用此代碼:
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”)也未寫入此文件中。
謝謝您的考慮。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.