Advertisement
Guest User

Untitled

a guest
Jun 7th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 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. This worked for me, while trying to bring the file from a remote machine onto my machine.
  62.  
  63. NOTE - These are the parameters passed to the function mentioned in the code below:
  64. String domain = "xyz.company.com";
  65. String userName = "GDD";
  66. String password = "fjsdfks";
  67.  
  68. (here you have to give your machine ip address of the remote system , then the path of the text file (testFileUpload.txt) on the remote machine, here C$ means C drive of the remote system. Also the ip address starts with \ , but in order to escape the two backslashes we start it \\ )
  69.  
  70. String remoteFilePathTransfer = "\\13.3.2.33\c$\FileUploadVerify\testFileUpload.txt";
  71.  
  72. (here this is the path on the local machine at which the file has to be transferred, it will create this new text file - testFileUploadTransferred.txt, with the contents on the remote file - testFileUpload.txt which is on the remote system)
  73.  
  74. String fileTransferDestinationTransfer = "D:/FileUploadVerification/TransferredFromRemote/testFileUploadTransferred.txt";
  75. __________________________________________
  76.  
  77. import java.io.File;
  78. import java.io.IOException;
  79.  
  80. import org.apache.commons.vfs.FileObject;
  81. import org.apache.commons.vfs.FileSystemException;
  82. import org.apache.commons.vfs.FileSystemManager;
  83. import org.apache.commons.vfs.FileSystemOptions;
  84. import org.apache.commons.vfs.Selectors;
  85. import org.apache.commons.vfs.UserAuthenticator;
  86. import org.apache.commons.vfs.VFS;
  87. import org.apache.commons.vfs.auth.StaticUserAuthenticator;
  88. import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
  89.  
  90. public class FileTransferUtility {
  91.  
  92. public void transferFileFromRemote(String domain, String userName, String password, String remoteFileLocation,
  93. String fileDestinationLocation) {
  94.  
  95. File f = new File(fileDestinationLocation);
  96. FileObject destn;
  97. try {
  98. FileSystemManager fm = VFS.getManager();
  99.  
  100. destn = VFS.getManager().resolveFile(f.getAbsolutePath());
  101.  
  102. if(!f.exists())
  103. {
  104. System.out.println("File : "+fileDestinationLocation +" does not exist. transferring file from : "+ remoteFileLocation+" to: "+fileDestinationLocation);
  105. }
  106. else
  107. System.out.println("File : "+fileDestinationLocation +" exists. Transferring(override) file from : "+ remoteFileLocation+" to: "+fileDestinationLocation);
  108.  
  109. UserAuthenticator auth = new StaticUserAuthenticator(domain, userName, password);
  110. FileSystemOptions opts = new FileSystemOptions();
  111. DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
  112. FileObject fo = VFS.getManager().resolveFile(remoteFileLocation, opts);
  113. System.out.println(fo.exists());
  114. destn.copyFrom(fo, Selectors.SELECT_SELF);
  115. destn.close();
  116. if(f.exists())
  117. {
  118. System.out.println("File transfer from : "+ remoteFileLocation+" to: "+fileDestinationLocation+" is successful");
  119. }
  120. }
  121.  
  122. catch (FileSystemException e) {
  123. e.printStackTrace();
  124. }
  125.  
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement