Download a non html file with HtmlUnit - java

I'm writing a JUnit test that involves the downloading of a file from the web app. How do I do that with HtmlUnit?

I don't what kind of file, but maybe code for this test might be helpful. If not try to find answers in other tests.

I'd bet you already solved the problem, but since this question is on google's top results when searching for "htmlunit download", here's the standard solution. downloadLink is the element with the link to the file you intend to download (a button, an input, an anchor...)
try {
InputStream is = downloadLink.click().getWebResponse().getContentAsStream();
try {
File f = new File("filename.extension");
OutputStream os = new FileOutputStream(f);
byte[] bytes = new byte[1024]; // make it bigger if you want. Some recommend 8x, others 100x
while (read = is.read(bytes)) {
os.write(bytes, 0, read);
}
os.close();
is.close();
} catch (IOException ex) {
// Exception handling
}
} catch (IOException ex) {
// Exception handling
}

Related

Improve performance when reading file from URL and writing it to disk

I made a program which accesses some URLs and downloads the pdfs from there. The files vary between 2MB to 40MB. The program works with no problems but is there a way to improve the perfomance on this? For the larger files it takes a long time to do it.
The code below is the one used for reading / writing the file. This is called in a for loop with different fileNameURLPath.
#Override
public void downloadFile(String fileNameURLPath, String titleCellValue) throws FileException {
try (BufferedInputStream inputStream
= new BufferedInputStream(new URL(fileNameURLPath).openStream())){
FileOutputStream fileOS = new FileOutputStream(FileConstants.MandatoryDownloadProperties.path + titleCellValue + ".pdf");
byte data[] = new byte[32*1024];
int byteContent;
while((byteContent = inputStream.read(data,0 , data.length)) != -1) {
fileOS.write(data, 0 , byteContent);
}
inputStream.close();
fileOS.close();
} catch (MalformedURLException e) {
throw new FileException("Error while processing url. Make sure it is correct");
} catch (IOException e) {
throw new FileException("Error while downloading file. Make sure the download path is correct");
}
}
I read something about Java NIO but I couldn't quite comprehend it or if it can help me in this situation

JAVA 6 and corrupted files after uploading using Apache FTPClient 3.1 and FTP.BINARY_FILE_TYPE

I'm really going bonkers on this.
I'm writing a small Java (must be SDK 6) tool using the commons-net-3.1.jar library to upload a bunch of files to another bunch of servers simultaneously. I will use it with .jpg images mostly. I set the connection to BINARY_FILE_TYPE before anyone ask :)
When I try and run it, everything goes smooth but sometimes, after uploading the images, they have "small lines" on them, like if the file had been corrupted on its way.
This little piece of code is the one who does all the work. Any clues about how should I manage the first catches when the os.write fails in order to keep the file uploading right?
try {
InputStream is = new FileInputStream(rutaFichero);
OutputStream os = ftp.storeFileStream(nombreFichero);
byte buf[] = new byte[8192];
bytesRead = is.read(buf);
while ((bytesRead = is.read(buf)) != -1) {
try {
os.write(buf, 0, bytesRead);
} catch (IOException ioe) {
} catch (NullPointerException npe) {
}
}
is.close();
try {
os.close();
completado = ftp.completePendingCommand();
if (completado) {
} catch (NullPointerException npe) {}
}
Out of curiosity, I thought it could be just bad conditions on the line so I checked the sizes of files after transfering for some days and... none of them matched! WTF!?
You have a bug in the first line here:
bytesRead = is.read(buf);
while ((bytesRead = is.read(buf)) != -1) {
try {
os.write(buf, 0, bytesRead);
} catch (IOException ioe) {
} catch (NullPointerException npe) {
}
}
You never write the first buf contents. You just read them and then read subsequent chunk in while loop. Remove the first is.read() and you'll be fine.
It's another reason to avoid such tedious code and go for utility methods such as IOUtils.copy():
InputStream is = new FileInputStream(rutaFichero);
OutputStream os = ftp.storeFileStream(nombreFichero);
IOUtils.copy(is, os);
is.close();
os.close();
The problem was solved when some fellow sent me an e-mail giving me an advice about being sure to set the mode type to binary AFTER login, never before.

How to describe content of file rar / zip in jtable from url or path(directory)?

How to describe content of file rar/zip in jtable from url or path directory in java?
I found here and here
But how to open from URL on describe to jtable?
I'd use PrpcessBuilder, shown here, to execute unzip -l or unrar -l and parse the output to create my TableModel. See also How to Use Tables.
You need to model the data some how. Maybe a VirtualFile, this should contain the information you want to display.
You then need to create a model of the that wraps this data in some meaningful way. You'll need a TableModel for this purpose (preferably use the Abstract or Default implementation)
Once you've decided on how you want the model to be laid out, you simply supply the modle to the JTable
For more information check out How to use Tables
UPDATE
You can learn more from the Basic I/O lesson, but here's a (really) basic example of reading the contents of URL to local disk
File outputFile = new File("Somefile on disk.rar");
Inputstream is;
OutputStream os;
try {
is = url.openStream();
os = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int bytesIn = -1;
while ((bytesIn = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesIn);
}
os.flush();
} catch (IOException exp) {
exp.printStackTrace();
} finally {
try {
is.close();
} catch (Exception exp) {
}
try {
os.close();
} catch (Exception exp) {
}
}

overwrite file content/bytes

I have a (PDF) file that exists on the file system, at a known location. I wish to overwrite the content of that file (with a fresh byte[]).
Best (and most efficient) possible way to do so (using Java APIs)?
public void oneShotAPI(File file, byte[] bytes) throws IOException
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(file);
fos.write(bytes);
fos.flush();
} finally
{
if (fos != null)
try
{
fos.close();
} catch (IOException e)
{
// Sad, but true
}
}
}
Call it with:
oneShotAPI(new File("myPDF.png"), byteArray);
There's nothing built into the Java APIs that does this, but if you're looking for a library:
Apache Commons IO has FileUtils.writeByteArrayToFile(File, byte[])
Google Guava has Files.write(byte[], File)
I don't see why any of the short methods posted here wouldn't work, though. There's no actual need for a library IMHO.

Problem writing to file

I'm having a problem writing to a file:
FileInputStream fin;
try
{
fin = new FileInputStream ("c:/text.txt");
PrintStream p = new PrintStream(fin);
p.println ("test");
fin.close();
}
catch (IOException ioe)
{
System.err.println (ioe.getMessage);
}
Is there a problem with this code?
You need to use a FileOutputStream.
Get used to the following structure. You'll use it a lot in Java.
PrintStream out = null;
try {
out = new PrintStream(new FileOutputStream("c:/text.txt"));
out.println ("test");
} catch (IOException e) {
System.err.println (e.getMessage);
} finally {
if (out != null) {
try { out.close(): } catch (Exception e) { }
}
out = null; // safe but not strictly necessary unless you reuse fin in the same scope
}
At least until ARM blocks hopefully eventuate in Java 7.
As noted, you should close the PrintStream and not the FileOutputStream so the above is a better form to use.
Problems with that code that immediately strike me:
Non-standard formatting.
Awkward variable names.
The exception handling is not good.
Failure to close the file in the case of exceptions. (Use acquire(); try { use(); } finally { release(); }.
Hidden use of default character encoding.
PrintStream swallows exceptions. BufferedOutputStream is better.
Failure to flush the decorator. It may still have data buffered. Although actually in this case you have left the PrintStream in auto-flush mode, which can be a performance issue.
Use / for a Windows path separator. You might be able to get away with it, but it's not good.
So:
FileOutputStream fileOut = new FileOutputStream(
"c:\\text.txt"
);
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
fileOut,
"UTF-8" // Or, say, Charset.defaultCharset()
));
out.write("test");
out.newLine()
out.flush();
} finally {
fileOut.close();
}
The class: FileInputStream is used to read input from a file. If you want to write to the file, you can use: FileOutputStream. If you want to make your life really easy, you can use a BufferedOutputStream as well.
As pointed out, you should close your streams in the finally block. The reason why you want to do that is say your program isn't really small, and it's a larger application. If you forget to close file streams, for example, the application will hold on to it and if you try to do something to it on the file system (read: at least in Windows) you won't be able to it. We've all seen the 'File cannot be deleted because it's still in use' error.
Here's an example of using the FileOutputStream + BufferedOutputStream: http://www.javadb.com/write-to-file-using-bufferedoutputstream.

Categories

Resources