Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: An example of updating a textfile trough the net.  (Read 1991 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: August 27, 2007, 01:16:42 PM »

Code
GeSHi (java):
  1. package full_projects;
  2. /*--------------------------------------------------------------------------
  3. //Author List:
  4. //deAppel <Creator>
  5. //
  6. //Description:
  7. // An example of updating a textfile trough the net.
  8. //
  9. //Environment:
  10. //This software was developed using Eclipse and Java 1.6
  11. //
  12. //Copyright Information:
  13. //Copyright (C) 2007      <Institution><None>
  14. // Ark de Appel www.engineeringserver.com
  15. //
  16. //----------------------------------------------------------------------*/
  17. import java.io.*;
  18. import java.net.*;
  19.  
  20. import javax.swing.JOptionPane;
  21. public class UpdaterDemo {
  22. static Thread t = null;
  23. public static void main(String[] args) throws IOException {
  24. System.out.println("This demo will update a textFile from a server");
  25. UpdaterDemo gr = new UpdaterDemo();
  26. gr.Update();
  27.  
  28. }
  29. public void Update(){
  30. String currentVersion = "1.0";
  31.  
  32. Socket s;
  33. try {
  34. s = new Socket("127.0.0.1",80);
  35. PrintStream p = new PrintStream(s.getOutputStream());
  36. p.print("GET /update/file.txt HTTP/1.0\r\n");
  37.  
  38. p.print("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n");
  39. p.print("Connection: close\r\n\r\n");
  40. InputStreamReader in = new InputStreamReader(s.getInputStream());
  41. BufferedReader buffer = new BufferedReader(in);
  42.  
  43. String line;
  44. int lines = 0;
  45.  
  46. while ((line = buffer.readLine()) != null && lines < 9){
  47. System.out.println(line);
  48. lines++;
  49. }
  50.  
  51. System.out.println("");
  52. System.out.println("Current textfile version: " + currentVersion);
  53.  
  54. if ((line = buffer.readLine()).equals(currentVersion)){
  55. System.out.println("Textfile up to date! Version: " + line);
  56. }
  57.  
  58. else{
  59.  
  60. System.out.println("Updating to: " + line +  " please hold on.");
  61. currentVersion = line;
  62.  
  63. FileWriter fwUpdate = new FileWriter("c:\\updateFile.txt");
  64. BufferedWriter bwUpdate = new BufferedWriter(fwUpdate);
  65.  
  66.  
  67. while ((line = buffer.readLine()) != null ) {
  68. if(!(line.equals("") && lines == 9)){ //remove the spaces in the new textfile!
  69.  
  70. bwUpdate.write(line);
  71. bwUpdate.newLine();
  72. lines++;
  73. }
  74. }
  75.  
  76. bwUpdate.close();
  77. in.close();
  78. System.out.println("Updated to version: " + currentVersion +  "!");
  79. System.out.println("Happy reading!");
  80. }
  81.  
  82.  
  83. File fFF = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
  84. if (fFF.exists())
  85. try{
  86. Runtime.getRuntime().exec("C:\\Program Files\\Mozilla Firefox\\firefox.exe c:\\updateFile.txt http://www.engineeringserver.com/t3hc0d3z/index.php");
  87. //Runtime.getRuntime().exec("C:\\Program Files\\Mozilla Firefox\\firefox.exe http://www.engineeringserver.com/t3hc0d3z/index.php");
  88.  
  89. }
  90. catch(java.io.IOException IOE){
  91. //IOE.printStackTrace();
  92.  
  93. }
  94. else{
  95. //System.out.println("");
  96. }
  97.  
  98. File fIE = new File("C:\\Program Files\\Internet Explorer\\iexplore.exe");
  99. if (fIE.exists() && !fFF.exists())
  100. try{
  101. Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe c:\\updateFile.txt http://www.engineeringserver.com/t3hc0d3z/index.php");
  102.  
  103. //Runtime.getRuntime().exec("C:\\Program Files\\Internet Explorer\\iexplore.exe c:\\engineeringserverTMP.html");
  104. }
  105. catch(java.io.IOException IOE){
  106. IOE.printStackTrace();
  107.  
  108. }
  109. else{}
  110.  
  111.  
  112. } catch (UnknownHostException e) {
  113. System.out.println("Please connect to the internet first..");
  114.  
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. }
  120.  
  121.  
Created by GeSHI 1.0.7.20




Sample output:
This demo will update a textFile from a server
HTTP/1.1 200 OK
Date: Mon, 27 Aug 2007 17:11:27 GMT
Server: Apache/2.0.55 (Win32) PHP/4.4.2
Last-Modified: Mon, 27 Aug 2007 17:11:23 GMT
ETag: "2cf5d-60-7235a67d"
Accept-Ranges: bytes
Content-Length: 96
Connection: close
Content-Type: text/plain; charset=ISO-8859-1

Current textfile version: 1.0
Updating to: 1.1 please hold on.
Updated to version: 1.1!
Happy reading!
« Last Edit: August 27, 2007, 01:21:21 PM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here