Java NIO downloaded file is different than web browser - java

I am trying to download a .torrent file from a URL using Java NIO. Downloading this file from a web browser results in a proper torrent file which can be used. Downloading in Java results in a different/corrupted file.
This is the code I am using to perform the download:
URL url = new URL("http://torcache.net/torrent/E4B5326AA31D1AE0C30E05129193E87AA086F5F8.torrent");
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream("test.torrent");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();

Related

Error in opening zip file: Opening of big zip files >2GB

When I try to create a java.util.zip.ZipFile I get a java.util.zip.ZipException: error in opening zip file. This exception only occurs when I try to open a large ZipFile (> 2GB). Is there a trick to open big zip files?
Later I need to extract single files from this zip and I doubt that the ZipInputStream is fast enough to extract the required files, since I need to run over all files.
Here is my StackTrace:
Caused by: java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:225)
at java.util.zip.ZipFile.<init>(ZipFile.java:148)
at java.util.zip.ZipFile.<init>(ZipFile.java:162)
Update:
I found out that it works on my desktop computer and it works if I open the ZipFile as a JUnit-Test within Android Studio, too (Since JUnit-Tests are run on the local desktop computer and not on the android device). However, I could not get it working on the android device. I guess the reason is the android file system.
Key point to remember, especially if you are processing large zip archives is that, Java 6 only support zip file up to 2GB.
Java 7 supports zip64 mode, which can be used to process large zip file with size more than 2GB
Also using streams for big files is a good idea:
private static void readUsingZipInputStream() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE_NAME));
final ZipInputStream is = new ZipInputStream(bis);
try {
ZipEntry entry;
while ((entry = is.getNextEntry()) != null) {
System.out.printf("File: %s Size %d Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));
extractEntry(entry, is);
}
} finally {
is.close();
}

Java Apache Ftp retrieveFile not downloading big file

I'm trying to retrieve file(.dat) with size of 1 GB, but, after retrieving some bytes from server it shows no connection of Ftp Server but, this program throws no error and hangs, my code runs perfactly fine for small file but, for big file it fails
code is as follows
//Ftp Connection
ftpClient.connect(server, port);
ftpClient.login(username, password);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//File Downloading
File downloadFile = new File(savePath);
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
isFileDownloaded= ftpClient.retrieveFile(remoteFilePath, outputStream);`

incompletely download of pgp file from ftp sever using apache.commons.net

I'm trying to transfer a pgp file with apache.commons.net.ftp.FTPClient, result seems successfully, but when I want to convert it to a txt file I encounter to this error:
gpg: [don't know]: invalid packet (ctb=20)
and when I check the exact size of downloaded file, I notice that it's size is about 1KB less than original file.
here is the code for downloading file:
FileOutputStream fos = new FileOutputStream(Localfilename);
InputStream inputStream = ftpClient.retrieveFileStream(remoteFileDir);
IOUtils.copy(inputStream, fos);
fos.flush();
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(inputStream);
boolean commandOK = ftpClient.completePendingCommand();
can any one understand what is mistake with my way or code?
[edited] noted that the original file decode (convert to txt)successfully, so the problem occures while downloading file.
[edited2] I run the program in my windows desktop and download file in windows, no problem for decode, and I understand that when I run my program with linux server this problem appears!
I found my problem!
The problem was with addressing the remote path, a silly mistake!
so If any one has this problem recheck and recheck again the addresses.

Access File in Mac using java.io.file

I am using java.io.File to create a new file in java.On Mac-Os I am using the same code which was working fine on windows but its not working on mac.
I am using
File file = new File(path);
to create a new file.
In windows:
I used String path = "C:\test\1.html";
It was working fine.
On a Mac:
I want to create file at "/Users/pls/1.html"
But it is throwing error as
java.io.FileNotFoundException : /Users/pls/1.html (No such file or directory)
Please help
Don't write separators manually,Use system Independent File.separator instead.
String path = File.separator+"Users"+File.separator+"pls"+File.separator+"1.html";
File file = new File(path);

What should format use to export .key and .req file?

I exported .key and .req file by my java application.
Code:
BufferedOutputStream bos1 = new BufferedOutputStream( new FileOutputStream(txtRequest.getText()));
bos1.write(certificate.getBytes());
bos1.close();
BufferedWriter bw = new BufferedWriter(new FileWriter(txtPrivateKey.getText()));
PEMWriter writer = new PEMWriter(bw);
writer.writeObject(getPrivateKey());
writer.close();
bw.close();
I have a problem that when I export .pfx file by using these .req and .key files. It's only get to install window XP and not get to install window server 2008 and window 7.
How should I do? Please show the way.
PKCS12 - can be read by openssl tool and can be exported too.
http://en.wikipedia.org/wiki/PKCS12

Categories

Resources