1 7 package org.jboss.remoting.samples.stream; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.IOException ; 12 import java.net.URL ; 13 import org.jboss.remoting.Client; 14 import org.jboss.remoting.InvokerLocator; 15 16 22 public class StreamingClient 23 { 24 private static String transport = "socket"; 26 private static String host = "localhost"; 27 private static int port = 5400; 28 29 private String locatorURI = transport + "://" + host + ":" + port; 30 31 private String localFileName = "sample.txt"; 32 private String remoteFileName = "server_sample.txt"; 33 34 public void sendStream() throws Throwable 35 { 36 FileInputStream fileInput = null; 37 Client remotingClient = null; 38 39 try 40 { 41 InvokerLocator locator = new InvokerLocator(locatorURI); 42 System.out.println("Calling on remoting server with locator uri of: " + locatorURI); 43 44 remotingClient = new Client(locator); 45 URL fileURL = this.getClass().getResource(localFileName); 46 if(fileURL == null) 47 { 48 throw new Exception ("Can not find file " + localFileName); 49 } 50 File testFile = new File (fileURL.getFile()); 51 fileInput = new FileInputStream (testFile); 52 53 System.out.println("Sending input stream for file " + localFileName + " to server."); 54 Object ret = remotingClient.invoke(fileInput, remoteFileName); 55 56 long fileLength = testFile.length(); 57 System.out.println("Size of file sample.txt is " + fileLength); 58 System.out.println("Server returned " + ret + " as the size of the file read."); 59 60 } 61 finally 62 { 63 if(remotingClient != null) 64 { 65 remotingClient.disconnect(); 66 } 67 if(fileInput != null) 68 { 69 try 70 { 71 fileInput.close(); 72 } 73 catch(IOException e) 74 { 75 e.printStackTrace(); 76 } 77 } 78 } 79 } 80 81 public void setRemoteFileName(String remoteFileName) 82 { 83 this.remoteFileName = remoteFileName; 84 } 85 86 87 93 public static void main(String [] args) 94 { 95 String newFileName = null; 96 if(args != null && args.length == 1) 97 { 98 newFileName = args[0]; 99 } 100 if(args != null && args.length == 2) 101 { 102 transport = args[0]; 103 port = Integer.parseInt(args[1]); 104 } 105 if(args != null && args.length == 3) 106 { 107 transport = args[0]; 108 port = Integer.parseInt(args[1]); 109 newFileName = args[2]; 110 } 111 String locatorURI = transport + "://" + host + ":" + port; 112 StreamingClient client = new StreamingClient(); 113 if(newFileName != null) 114 { 115 client.setRemoteFileName(newFileName); 116 } 117 try 118 { 119 client.sendStream(); 120 } 121 catch(Throwable e) 122 { 123 e.printStackTrace(); 124 } 125 } 126 127 128 } | Popular Tags |