P
Public Network Services
Hi...
I am playing with HttpURLConnection to upload files to a remote server
and cannot seem to figure out a consistent way of identifying the
Content-Type of a file to be uploaded. I assumed that "application/
octet-stream" would be a generic type to supply, but apparently this
is not the case.
More specifically, I can read a file into a byte array (fileBytes) and
open an HttpConnection to a destination URL (url), but setting the
Content-Type property does not work. The code I use is:
HttpURLConnection connection = url.openConnection();
connection.setRequestProperty("Content-Length",
String.valueOf(fileBytes.length));
connection.setRequestProperty("Content-Type", "application/octet-
stream");
If, immediately after the above, I do a
connection.getContentType()
I get a null value (or an Exception saying that the connection is
already connected)!
Eventually, my file gets uploaded through the
connection.getOutputStream(), by simply doing
DataOutputStream output = new
DataOutputStream(connection.getOutputStream());
output.write(fileContents);
but its Content-Type not being properly set causes the server to not
recognize it (e.g., if it is an MP3 file, it does not get the
speakerphone icon that MP3 files get in the serverÅ› web interface,
when uploaded via that interface, through a browser).
Obviously, this is a solved problem given the multitude of properly
working Java-based web clients, so I 'd really aprpeciate
someone pointing me to the correct solution.
In other words, my questions are:
(a) How can I (automatically) identify the Content-Type of a file in
Java?
(b) How can I properly set that Content-Type in an HttpURLConnection?
Many thanks!
I am playing with HttpURLConnection to upload files to a remote server
and cannot seem to figure out a consistent way of identifying the
Content-Type of a file to be uploaded. I assumed that "application/
octet-stream" would be a generic type to supply, but apparently this
is not the case.
More specifically, I can read a file into a byte array (fileBytes) and
open an HttpConnection to a destination URL (url), but setting the
Content-Type property does not work. The code I use is:
HttpURLConnection connection = url.openConnection();
connection.setRequestProperty("Content-Length",
String.valueOf(fileBytes.length));
connection.setRequestProperty("Content-Type", "application/octet-
stream");
If, immediately after the above, I do a
connection.getContentType()
I get a null value (or an Exception saying that the connection is
already connected)!
Eventually, my file gets uploaded through the
connection.getOutputStream(), by simply doing
DataOutputStream output = new
DataOutputStream(connection.getOutputStream());
output.write(fileContents);
but its Content-Type not being properly set causes the server to not
recognize it (e.g., if it is an MP3 file, it does not get the
speakerphone icon that MP3 files get in the serverÅ› web interface,
when uploaded via that interface, through a browser).
Obviously, this is a solved problem given the multitude of properly
working Java-based web clients, so I 'd really aprpeciate
someone pointing me to the correct solution.
In other words, my questions are:
(a) How can I (automatically) identify the Content-Type of a file in
Java?
(b) How can I properly set that Content-Type in an HttpURLConnection?
Many thanks!