Does URLConnection download the file? - java

I need to do some verification on a file before downloading it. I need to verify some of the information about it (extension, MIME type and size). I've got something like this:
URL u = new URL("https://test.com/Flight.pdf?some=true&added=8data=7678");
URLConnection uc = u.openConnection();
String type = uc.getContentType();
int length = uc.getContentLength()
String extension = FilenameUtils.getExtension(u.getPath());
Does opening the URL connection and getting the data I need will download the actual file?

Related

How to set Safari download location - Selenium WebDriver

Trying to set safari (driver) download directory to specific location.
Now it will just download files to default "Downloads" folder.
String currentDirectory = System.getProperty("user.dir");
String downloadFilePath = currentDirectory+"/download/";
Already tried:
dc.setCapability("safari.download.dir", downloadFilePath);
dc.setCapability("browser.download.dir", downloadFilePath);
dc.setCapability("safari.options.dataDir", downloadFilePath); // ("safari.options.dataDir" // this part won't work)
safariOptions.setCapability("safari.options.dataDir", downloadFilePath);
safariPrefs.put("download.deafult_directory", downloadFilePath); // this one I am using for chromedriver (chromePrefs)
Well, you can try to use an HTTP connection to download the file! That way, you can for sure have it saved to the directory specified in your variable.
Most languages have APIs (or libraries) for performing HTTP requests. For example, to accomplish this in Java, you could use URL.openConnection()
String link = linkElement.getAttribute("href");
URL url = new URL(link);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
Then you can use HttpURLConnection.getInputStream() to write the file contents to your preferred location.
try (InputStream in = httpURLConnection.getInputStream()) {
Files.copy(in, new File("/path/to/file.ext").toPath(),
StandardCopyOption.REPLACE_EXISTING);
}
ONLY IN CASE YOU ARE USING COOKIES:
You can add them to your HTTP connection. If in this case, you must use a password saved on your cookies, this can be very useful.
Set<Cookie> cookies = webDriver.manager().getCookies();
String cookieString = "";
for (Cookie cookie : cookies) {
cookieString += cookie.getName() + "=" + cookie.getValue() + ";";
}
httpURLConnection.addRequestProperty("Cookie", cookieString);

Building a JSON search parser

I am working with a database api and they give me the opportunity to search using a url through their database.
This is the url =
http://api.database.com/v2/search?q=(THE PRODCUT)&type=(THE TYPE OF PRODUCT)
&key=(myApiKey)
I want to make a simple search bar were the user can type the product name and choose a type (catagorie) to insert that into the url and then look the product up in the database.
I know how I can parse the data from the url but how can I insert the users text into the url to change it ?
It's always a better approach to use URI to build your search url.
Try something like this
final String FORECAST_BASE_URL ="http://api.openweathermap.org/data/2.5/forecast/daily?";
final String QUERY_PARAM = "q";
Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, params[0])
.build();
URL url = new URL(builtUri.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
And then read the response from the inputStream.
InputStream inputStream = urlConnection.getInputStream();
You can use string concatenation or you can use String.format(String format, Object... args). For example:
String url = String.format("http://api.database.com/v2/search?q=%s&type=%s&key=%", product, productType, apiKey);
or :
var product = "Product";
var productType="ProductType";
var url = "http://api.database.com/v2/search?q="+product+"&type="+productType+"&key="+myApiKey;

Downloading a text file

I am trying to download a text file in Android, i know how to load image file, how different is text file downloading from it?
Moerover how to retrive contents from the downloaded file?
You are asking a few things, this should give you an idea of how to get a remote file using urlconnection and associated classes
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
URLConnection conn = u.openConnection();
fs = conn.getContentLength();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH_op = Environment.getExternalStorageDirectory()
+"//"+ filename;
f = new FileOutputStream(new File(PATH_op));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
completed += len1;
}
f.close();`enter code here`
There will be no difference in downloading the text file or image or XML. every thing is same. but the usage after getting the stream depends on the type of the content.
If its a Image we will decode the stream to convert it to an image.
If its a Text we need to read the content character by character until the whole content got read or got -1 as the character which denotes the end of the file.
When coming to the XML file file we will directly pass the input stream object to Parser.

Parsing PDF files hosted in web servers

I have used iText to parse pdf files. It works well on local files but I want to parse pdf files which are hosted in web servers like this one:
"http://protege.stanford.edu/publications/ontology_development/ontology101.pdf"
but I don't know how??? Could you please answer me how to do this task using iText or other libraries... thx
You need to download the bytes of the PDF file. You can do this with:
URL url = new URL("http://.....");
URLConnection conn = url.getConnection();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { ..error.. }
if ( ! conn.getContentType().equals("application/pdf")) { ..error.. }
InputStream byteStream = conn.getInputStream();
try {
... // give bytes from byteStream to iText
} finally { byteStream.close(); }
Use the URLConnection class:
URL reqURL = new URL("http://www.mysite.edu/mydoc.pdf" );
URLConnection urlCon = reqURL.openConnection();
Then you can use the URLConnection method to retrieve the content. Easiest way:
InputStream is = urlCon.getInputStream();
byte[] b = new byte[1024]; //size of a buffer, can be any
int len;
while((len = is.read(b)) != -1){
//Store the content in preferred way
}
is.close();
Nothing to it. You can pass a URL directly into PdfReader, and let it handle the streaming for you:
URL url = new URL("http://protege.stanford.edu/publications/ontology_development/ontology101.pdf" );
PdfReader reader = new PDFReader( url );
The JavaDoc is your friend.

How to play .wav file from URL in web browser embed - Java

I want to play a .wav sound file in embed default media player in IE. Sound file is on some HTTP location. I am unable to sound it in that player.
Following is the code.
URL url = new URL("http://www.concidel.com/upload/myfile.wav");
URLConnection urlc = url.openConnection();
InputStream is = (InputStream)urlc.getInputStream();
fileBytes = new byte[is.available()];
while (is.read(fileBytes,0,fileBytes.length)!=-1){}
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(fileBytes);
Here is embed code of HTML.
<embed src="CallStatesTreeAction.do?ivrCallId=${requestScope.vo.callId}&agentId=${requestScope.vo.agentId}" type="application/x-mplayer2" autostart="0" playcount="1" style="width: 40%; height: 45" />
If I write in FileOutputStream then it plays well
If I replace my code of getting file from URL to my local hard disk. then it also works fine.
I don't know why I am unable to play file from HTTP. And why it plays well from local hard disk.
Please help.
Make sure you set the correct response type. IE is very picky in that regard.
[EDIT] Your copy loop is broken. Try this code:
URL url = new URL("http://www.concidel.com/upload/myfile.wav");
URLConnection urlc = url.openConnection();
InputStream is = (InputStream)urlc.getInputStream();
fileBytes = new byte[is.available()];
int len;
while ( (len = is.read(fileBytes,0,fileBytes.length)) !=-1){
response.getOutputStream.write(fileBytes, 0, len);
}
The problem with your code is: If the data isn't fetched in a single call to is.read(), it's not appended to fileBytes but instead the first bytes are overwritten.
Also, the output stream which you get from the response is already buffered.

Categories