简体   繁体   English

如何使用tomcat和java编写远程文件

[英]how to write a remote file using the tomcat and java

how to write a remote file using the tomcat and java. 如何使用tomcat和java编写远程文件。 I use this java code : 我使用以下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);

In the output I have this result : 在输出中,我得到以下结果:

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"]

and the server is stopped. 并且服务器已停止。 Please respond me 请回应我

In this code path, you are closing out , which is a PrintWriter wrapper around connextion's OutputStream, then calling connextion.disconnect() . 在此代码路径,你收out ,这大约是connextion的OutputStream的一个PrintWriter包装,然后调用connextion.disconnect() I suspect that the .disconnect() method attempts to close the stream again, causing an exception or some error you're not capturing. 我怀疑.disconnect()方法试图再次关闭流,从而导致异常或您未捕获的某些错误。 I suggest to remove the out.close() and try again. 我建议删除out.close()并再试一次。

I used this code : 我使用此代码:

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

But the file pervasiveSystemDescription1.txt is not modified, and the String ("okkkk") is not written in this file. 但是文件pervasiveSystemDescription1.txt未被修改,并且字符串(“ okkkk”)也未写入此文件中。

Thank you for your consideration. 谢谢您的考虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM