Advertisement
Guest User

Untitled

a guest
Aug 13th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. try
  2. {
  3. uri = new URI("ftp://localhost/myTest/test.mid");
  4. File midiFile = new File(uri);
  5. }
  6. catch (Exception ex)
  7. {
  8. }
  9.  
  10. URL url = new URL("http://q.com/test.mid");
  11. InputStream is = url.openStream();
  12. // Read from is
  13.  
  14. URL u = new URL("http://www.java2s.com/binary.dat");
  15. URLConnection uc = u.openConnection();
  16. String contentType = uc.getContentType();
  17. int contentLength = uc.getContentLength();
  18. if (contentType.startsWith("text/") || contentLength == -1) {
  19. throw new IOException("This is not a binary file.");
  20. }
  21. InputStream raw = uc.getInputStream();
  22. InputStream in = new BufferedInputStream(raw);
  23. byte[] data = new byte[contentLength];
  24. int bytesRead = 0;
  25. int offset = 0;
  26. while (offset < contentLength) {
  27. bytesRead = in.read(data, offset, data.length - offset);
  28. if (bytesRead == -1)
  29. break;
  30. offset += bytesRead;
  31. }
  32. in.close();
  33.  
  34. if (offset != contentLength) {
  35. throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
  36. }
  37.  
  38. String filename = u.getFile().substring(filename.lastIndexOf('/') + 1);
  39. FileOutputStream out = new FileOutputStream(filename);
  40. out.write(data);
  41. out.flush();
  42. out.close();
  43.  
  44. try {
  45. URL url = new URL("ftp://localhost/myTest/test.mid");
  46. InputStream is = url.openStream();
  47. ByteArrayOutputStream os = new ByteArrayOutputStream();
  48. byte[] buf = new byte[4096];
  49. int n;
  50. while ((n = is.read(buf)) >= 0)
  51. os.write(buf, 0, n);
  52. os.close();
  53. is.close();
  54. byte[] data = os.toByteArray();
  55. } catch (MalformedURLException e) {
  56. e.printStackTrace();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60.  
  61. String domain = "xyz.company.com";
  62. String userName = "GDD";
  63. String password = "fjsdfks";
  64.  
  65. String remoteFilePathTransfer = "\\13.3.2.33\c$\FileUploadVerify\testFileUpload.txt";
  66.  
  67. String fileTransferDestinationTransfer = "D:/FileUploadVerification/TransferredFromRemote/testFileUploadTransferred.txt";
  68.  
  69. import java.io.File;
  70. import java.io.IOException;
  71.  
  72. import org.apache.commons.vfs.FileObject;
  73. import org.apache.commons.vfs.FileSystemException;
  74. import org.apache.commons.vfs.FileSystemManager;
  75. import org.apache.commons.vfs.FileSystemOptions;
  76. import org.apache.commons.vfs.Selectors;
  77. import org.apache.commons.vfs.UserAuthenticator;
  78. import org.apache.commons.vfs.VFS;
  79. import org.apache.commons.vfs.auth.StaticUserAuthenticator;
  80. import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
  81.  
  82. public class FileTransferUtility {
  83.  
  84. public void transferFileFromRemote(String domain, String userName, String password, String remoteFileLocation,
  85. String fileDestinationLocation) {
  86.  
  87. File f = new File(fileDestinationLocation);
  88. FileObject destn;
  89. try {
  90. FileSystemManager fm = VFS.getManager();
  91.  
  92. destn = VFS.getManager().resolveFile(f.getAbsolutePath());
  93.  
  94. if(!f.exists())
  95. {
  96. System.out.println("File : "+fileDestinationLocation +" does not exist. transferring file from : "+ remoteFileLocation+" to: "+fileDestinationLocation);
  97. }
  98. else
  99. System.out.println("File : "+fileDestinationLocation +" exists. Transferring(override) file from : "+ remoteFileLocation+" to: "+fileDestinationLocation);
  100.  
  101. UserAuthenticator auth = new StaticUserAuthenticator(domain, userName, password);
  102. FileSystemOptions opts = new FileSystemOptions();
  103. DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
  104. FileObject fo = VFS.getManager().resolveFile(remoteFileLocation, opts);
  105. System.out.println(fo.exists());
  106. destn.copyFrom(fo, Selectors.SELECT_SELF);
  107. destn.close();
  108. if(f.exists())
  109. {
  110. System.out.println("File transfer from : "+ remoteFileLocation+" to: "+fileDestinationLocation+" is successful");
  111. }
  112. }
  113.  
  114. catch (FileSystemException e) {
  115. e.printStackTrace();
  116. }
  117.  
  118. }
  119.  
  120. }
  121.  
  122. JRFServer srv = JRFServer.get(new InetSocketAddress(2205));
  123. srv.start();
  124.  
  125. JRFClient cli = new JRFClient(new InetSocketAddress("jrfserver-hostname", 2205));
  126.  
  127. URL oracle = new URL("http://www.oracle.com/");
  128. BufferedReader in = new BufferedReader(
  129. new InputStreamReader(oracle.openStream()));
  130.  
  131. String inputLine;
  132. while ((inputLine = in.readLine()) != null)
  133. System.out.println(inputLine);
  134. in.close();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement