Java download file to temporary directory






















The above code will create a temporary directory and print its absolute path on the console. Here is how it looks like on a Linux machine:. To create a temporary folder in another location, you can pass an instance of Path as a first argument to Files. In Java 6 and below, you can use File. In this short blogpost I will show you how you can download a file from remote onto you local filesystem. We will use Java SE only.

You can always add more error handling like Apache StringUtils if you like. In our case the file will be stored in a the Java temp directory of the filesystem and the path to the file returned. Over 2 million developers have joined DZone. Like Join the DZone community and get the full member experience. Join For Free. Opinions expressed by DZone contributors are their own. Java Partner Resources. Historically you could think carefully about race conditions and write it yourself, or you could use a third-party library like Google's Guava as Spina's answer suggested.

Or you could write buggy code. But as of JDK 7, there is good news! The Java standard library itself now provides a properly working non-racy solution to this problem. You want java. Files createTempDirectory. Creates a new directory in the specified directory, using the given prefix to generate its name. The resulting Path is associated with the same FileSystem as the given directory. The details as to how the name of the directory is constructed is implementation dependent and therefore not specified.

Where possible the prefix is used to construct candidate names. This effectively resolves the embarrassingly ancient bug report in the Sun bug tracker which asked for just such a function. This is the source code to the Guava library's Files. It's nowhere as complex as you might think:.

See here. Google 'deleteonexit is evil' for more info, but the gist of the problem is:. The most evil of all - deleteOnExit consumes memory for each temp file entry. If your process is running for months, or creates a lot of temp files in a short time, you consume memory and never release it until the JVM shuts down.

As of Java 1. Well, "createTempFile" actually creates the file. So why not just delete it first, and then do the mkdir on it? As discussed in this RFE and its comments, you could call tempDir. Or you could use System. Either way, you should remember to call tempDir. Just for completion, this is the code from google guava library.

It is not my code, but I think it is valueable to show it here in this thread. I got the same problem so this is just another answer for those who are interested, and it's similar to one of the above:. And for my application, I decided that to add in a option to clear the temp on exit so I added in a shut-down hook:. The method delete all subdirs and files before deleting the temp , without using the callstack which is totally optional and you could do it with recursion at this point , but I want to be on the safe side.

As you can see in the other answers, no standard approach has arisen. This is preferred since apache commons the library that comes as closest to the asked "standard" and works with both JDK 7 and older versions.

Imports: java. IOException java. Files java. Comment: Files. Note: Read the following for deleting directories recursively: Delete directories recursively in Java. I like the multiple attempts at creating a unique name but even this solution does not rule out a race condition. Another process can slip in after the test for exists and the if newTempDir.

I have no idea how to completely make this safe without resorting to native code, which I presume is what's buried inside File. Using File createTempFile and delete to create a unique name for the directory seems ok. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.



0コメント

  • 1000 / 1000