What is the best way for a program which retrieves files from ftp server to check if the file to be downloaded is an ongoing transfer (someone already uploading this file during we decide to download). Do ftp client apis handle this ? (e.g. apache commons ftp client).
i think it's not really possible. a couple years ago i had a similar problem and i've got 2 options. (unfortunatelly it was C#, not Java)
you can check if the file's still growing (implies that you're gonna have a small delay because you need to check twice) or if you're using windows (i don't know how linux works) you can try to access this file and you should get an exception that the file is in use by another process.
just two possibilities and a starter for you to think about your problem. maybe someone else'
s coming up with a really good solution, but for now that might be a little workaround for you
Ftp was not designed to tell you if a file is in use, the most the ftp daemon can do is deny the transfer, and that is configurable in some servers. There may be a server that renames files temporarily or offer a script to do so , but you'd have to find one.
Do not know if it is sufficient for you, but if you need only some "dum" check, I would try System.getSecurityManager().checkDelete(). File can be deleted only if no streams are opened.
Related
I’m refactoring a Java EE application that uses apache commons net FTP library to facilitate FTP
transfers between two servers. The code is almost identical to the code posted as an example on the web
page, http://commons.apache.org/proper/commons-net/examples/ftp/ServerToServerFTP.java.
The files that are being transferred sometimes exceed 60 gb, and even though the timeout is set quite high,
and the largest transfers are over a LAN, I’m still seeing some exceptions.
I’ve been trying to figure out how to implement the REST function in FTP, i.e. resume of transfers. The
servers support it so it only needs to be implemented using commons.
So far I’ve gathered that I need to use getRestartOffset and setRestartOffset.
I have not been able to locate any resources or examples online of how this can be implemented in a
server-to-server transfer and was wondering whether anybody has any pointers or examples?
Edit:
Solution
Using the solution suggested by user270349 I was able to implement the desired functionality, although it was not possible by using the REST command. I got the amount of bytes written from the destination, set the offset on both destination and source and then used the remoteAppend(String filename) method provided by the library instead of remoteStore(String filename) which is used in the example linked above.
The only difference between Server->Client download resume and Server->Server transfer resume is how you get the restarOffset. You need to list the files in the destination directory (remote in your case) and use the partial file size as offset for the next attempt.
I have been working on an android project in which I have to download/upload few files via HTTP. I was wondering if there is a way to have resumable downloads/uploads for the files. As in, if my file is being downloaded or uploaded and there is a subtle internet choke for very minimal time (this sometimes corrupts the file and the process is stopped and next time it starts from 0 ) the downloading/uploading is paused and once the internet is back again on my device, the downloading/uploading starts from the same point where it was stopped at so that the file does not get corrupted and the process does not start from 0.
Is there any way to achieve this functionality in android/Java ? Please do let me know. Thanks in advance.
Html itself doesn't provide such ability to load file in chunks. FileUpload is simple object which works with file as whole and so sends it from scratch. To fulfill your requirements you need more sophisticated client/server relations. Java Applet is good candidate to do so on the client side and server side is trivial. However you need to implement some protocol (like handshake, start to send file, continue from some location, validation) and this is not an easy task. Even most commonly protocols (for example ftp) don't provide such ability. And even when you create all this stuff it will be compatible only with itself. Is it really worth all the efforts? Common answer is - no. That's the reason why we don't see such approach in the wild.
Any one knows what .ptn file is and how to open that, just got the file from client and couldnt able to figure out what is this , searched a lot but no luck .
Also have the requirement from client that
my application should read this .ptn file from Java
is this possible ?
thanks
This seems rather obvious, but if a client is paying you to write code to read a file, you're entirely within your rights to ask what the file is, and where it comes from. If it's some internal or proprietary format, you can ask for documentation.
Of course, you might start by simply opening the file in a text editor, to see if it's just something trivial.
This wonderful web site is a repository of information about file formats; searching "PTN" brings up nothing, unfortunately, which suggests that it's nothing very common.
I am using Java language
What I want is that Can any one help me to write a code that
When i click on delete option of any file or folder I get notify before delete that I ma deleting a file Whether I want to continue ?
I have seen many examples that notify after the file is deleted.
One thing I want to make clear is as I click on file placed Desktop or My document directory I must get notify that
You are deleting a file .do you want to continue ?
What I really need is I want the exact answer or code
Please help
I shall be very thankful to you
No, this is not possible in Java. The operating system handles the file access, and another process is not capable of preventing the system denying access to those files. The only way you could do this is by having a file system written in Java (say, a loopback mounted WebDAV share) to which you could intercept the file requests with this kind of information. But not only would this be difficult to achieve, it also would only work if all of the access you are doing is via your loopback mounted system; it wouldn't work for files located on the disk or from other network shares.
So, in summary, you cannot do this with any programming language without writing your own filesystem and using that to intercept requests.
I'm looking for code, or a library, which I can use to copy files between Windows servers using Java. Picture a primary server that runs a background thread, so that whenever a transaction is completed, it backs up the database files to the backup server. (This is required protocol, so no need to discuss the pros/cons of this action). In other words, transaction completes, Java code gets executed which copies one directory to the back-up server.
The way the Windows machines are set up, the primary server has the back-up server's C: drive mapped as it's own Z: drive. Both machines running Windows 2003 or 2008 Server. Java 1.6.
Found the correct answer on another forum and from messing around a little with the settings. The problem with copying files from one machine to another in Windows (using either a .bat file or using straight-up Java code) is the user permissions. On the primary server, you MUST set the Tomcat process to run as the administrator, using that administrator's username and password. (Right-click on the Tomcat service, select "Log On" tab, enter administrator's username/password). The default user that Tomcat runs on (local user), isn't sufficient to copy files between networked drives on Windows. When I set that correctly, both the .bat file solution I had tried previous to this post, and a straight-Java solution suggested here worked just fine.
Hope that helps someone else, and thanks for the suggestions.
Obtain the files by File#listFiles() on a directory from one disk, iterate over each of them, create a new File on the other disk, read from a FileInputStream from the file from one disk and write it to a FileOutputStream on the file on the other disk.
In a nut:
for (File file : new File("C:/path/to/files").listFiles()) {
if (!file.isDirectory()) {
File destination = new File("Z:/path/to/files", file.getName());
// Do the usual read InputStream, write OutputStream job here.
}
}
See also:
Java IO tutorial
By the way, if you were using Java 7, you would have used Path#copyTo() for this.
Paths.get("C:/path/to/files").copyTo(Paths.get("Z:/path/to/files"));
I would really recommend using Apache Commons IO for this.
The FileUtils class provides methods to iterate over directories and copy files from one directory to another. You don't have to worry about reading and writing files yourself because it's all done by the library for you.