Pass a local file in to URL in Java
new File(path).toURI().toURL();
URL wont open file:\C filepath in Java
file:/C:/
is not a valid file url. Try starting your URLs with file://C:/
.
Additionally, the File(String)
constructor does not take a URL, it takes a local file path. If you have a URL as a string that you want to parse, use the URL(String)
constructor:
URL fileURL = new URL("file://C:/RAdev/Basic/src/test/resources/xml Data/test dir/app-config-seed-data.xml");
is = fileURL.openStream();
Creating a URL object with a relative path
The solution is to find an absolute path to url1.html
make an object of java.io.File
on it, and then use toURI().toURL()
combination:
URL url1 = (new java.io.File(absolutePathToHTMLFile)).toURI().toURL();
Assuming if the current directory is the root of page
, you can pass a relative path to File
:
URL url1 = (new java.io.File("page/url1.html")).toURI().toURL();
or
URL url1 = (new java.io.File(new java.io.File("page"), "url1.html")).toURI().toURL();
But this will depend on where you run the application from. I would make it taking the root directory as a command-line argument if it is the only configurable option for the app, or from a configuration file, if it has one.
The another solution is to put the html file as a resource into the jar file of your application.
How to give url path in spring boot refer to local file
Try this
URL url = Thread.currentThread().getContextClassLoader().getResource("wsdl/outbound.wsdl");
This should work
How can I download and save a file from the Internet using Java?
Give Java NIO a try:
URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
Using transferFrom()
is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.
Check more about it here.
Note: The third parameter in transferFrom is the maximum number of bytes to transfer. Integer.MAX_VALUE
will transfer at most 2^31 bytes, Long.MAX_VALUE
will allow at most 2^63 bytes (larger than any file in existence).
How to upload remote file to the POST URL in java?
Not 100% what you mean by "without downloading to the local machine."
Here's how to avoid downloading the file into a temporary local file and then uploading that.
The basic approach is to read from one URLConnection (instead of the local file) and write to another URLConnection (like you do already).
Start by doing the request to sourceString
, so
HttpsURLConnection source = (HttpsURLConnection) new URL("https://machine.B/path/to/file.zip").openConnection();
then keep everything until youset the Content-Length
and replace that with
conn.setRequestProperty("Content-Length", source.getContentLength());
and then all you have to do is use
InputStream is = source.getInputStream();
instead of your diskis
.
PS: I don't get the purpose of the .available
logic, why not just use CHUNK_LEN for the buffer size?
PPS: also the while(i<0)
loop can be removed ;-)
Related Topics
Get a Node's Inner Xml as String in Java Dom
Use Java and Regex to Convert Casing in a String
Facebook Offline Access Step-By-Step
What Does Maven Do, in Theory and in Practice? When Is It Worth to Use It
Last Row Always Removed from Defaulttablemodel, Regardless of Index
Problems with Newline in Graphics2D.Drawstring
How Does the Bitwise & (And) Work in Java
How to Convert/Parse from String to Char in Java
Maven Jacoco: Not Generating Code Coverage Report
What's the Difference Between Instance Method Reference Types in Java 8
How to Get a Spring Bean in a Servlet Filter
Accessing the Last Entry in a Map
Java - JPA - @Version Annotation
Does Java Support Inner/Local/Sub Methods