Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try
- {
- uri = new URI("ftp://localhost/myTest/test.mid");
- File midiFile = new File(uri);
- }
- catch (Exception ex)
- {
- }
- URL url = new URL("http://q.com/test.mid");
- InputStream is = url.openStream();
- // Read from is
- URL u = new URL("http://www.java2s.com/binary.dat");
- URLConnection uc = u.openConnection();
- String contentType = uc.getContentType();
- int contentLength = uc.getContentLength();
- if (contentType.startsWith("text/") || contentLength == -1) {
- throw new IOException("This is not a binary file.");
- }
- InputStream raw = uc.getInputStream();
- InputStream in = new BufferedInputStream(raw);
- byte[] data = new byte[contentLength];
- int bytesRead = 0;
- int offset = 0;
- while (offset < contentLength) {
- bytesRead = in.read(data, offset, data.length - offset);
- if (bytesRead == -1)
- break;
- offset += bytesRead;
- }
- in.close();
- if (offset != contentLength) {
- throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
- }
- String filename = u.getFile().substring(filename.lastIndexOf('/') + 1);
- FileOutputStream out = new FileOutputStream(filename);
- out.write(data);
- out.flush();
- out.close();
- try {
- URL url = new URL("ftp://localhost/myTest/test.mid");
- InputStream is = url.openStream();
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- byte[] buf = new byte[4096];
- int n;
- while ((n = is.read(buf)) >= 0)
- os.write(buf, 0, n);
- os.close();
- is.close();
- byte[] data = os.toByteArray();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- This worked for me, while trying to bring the file from a remote machine onto my machine.
- NOTE - These are the parameters passed to the function mentioned in the code below:
- String domain = "xyz.company.com";
- String userName = "GDD";
- String password = "fjsdfks";
- (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 \\ )
- String remoteFilePathTransfer = "\\13.3.2.33\c$\FileUploadVerify\testFileUpload.txt";
- (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)
- String fileTransferDestinationTransfer = "D:/FileUploadVerification/TransferredFromRemote/testFileUploadTransferred.txt";
- __________________________________________
- import java.io.File;
- import java.io.IOException;
- import org.apache.commons.vfs.FileObject;
- import org.apache.commons.vfs.FileSystemException;
- import org.apache.commons.vfs.FileSystemManager;
- import org.apache.commons.vfs.FileSystemOptions;
- import org.apache.commons.vfs.Selectors;
- import org.apache.commons.vfs.UserAuthenticator;
- import org.apache.commons.vfs.VFS;
- import org.apache.commons.vfs.auth.StaticUserAuthenticator;
- import org.apache.commons.vfs.impl.DefaultFileSystemConfigBuilder;
- public class FileTransferUtility {
- public void transferFileFromRemote(String domain, String userName, String password, String remoteFileLocation,
- String fileDestinationLocation) {
- File f = new File(fileDestinationLocation);
- FileObject destn;
- try {
- FileSystemManager fm = VFS.getManager();
- destn = VFS.getManager().resolveFile(f.getAbsolutePath());
- if(!f.exists())
- {
- System.out.println("File : "+fileDestinationLocation +" does not exist. transferring file from : "+ remoteFileLocation+" to: "+fileDestinationLocation);
- }
- else
- System.out.println("File : "+fileDestinationLocation +" exists. Transferring(override) file from : "+ remoteFileLocation+" to: "+fileDestinationLocation);
- UserAuthenticator auth = new StaticUserAuthenticator(domain, userName, password);
- FileSystemOptions opts = new FileSystemOptions();
- DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
- FileObject fo = VFS.getManager().resolveFile(remoteFileLocation, opts);
- System.out.println(fo.exists());
- destn.copyFrom(fo, Selectors.SELECT_SELF);
- destn.close();
- if(f.exists())
- {
- System.out.println("File transfer from : "+ remoteFileLocation+" to: "+fileDestinationLocation+" is successful");
- }
- }
- catch (FileSystemException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement