Jump to content

Hey. I need to be able to download a .zip file using Java. However, the file is always 1KB even though the original file is 28MB. I've tried many different methods that I found online but they all return the same result. I don't know what's happening.

 

The methods I've tried:

 

Buffered Streams:

try (BufferedInputStream in = new BufferedInputStream(url.openStream());
			FileOutputStream fileOutputStream = new FileOutputStream(codeName + ".zip")) {
			byte dataBuffer[] = new byte[1024];
			int bytesRead;
			
			while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
				fileOutputStream.write(dataBuffer, 0, bytesRead);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}

 

Java NIO:

try {
			ReadableByteChannel readChannel = Channels.newChannel(url.openStream());
			
			FileOutputStream fileOS = new FileOutputStream(codeName + ".zip");
			FileChannel writeChannel = fileOS.getChannel();
			writeChannel.transferFrom(readChannel, 0, Long.MAX_VALUE);
			
			fileOS.close();
			readChannel.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}

 

And finally this one I don't know what it is:

try {
			InputStream in = url.openStream();
			Files.copy(in, Paths.get(codeName + ".zip"), StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			e.printStackTrace();
		}

 

All of these return the same results, a 1KB zip that cannot be opened. The original file IS a 28MB zip file and I've verified the download works correctly by downloading it myself normally.

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
https://linustechtips.com/topic/1046842-help-with-downloading-files-in-java/
Share on other sites

Link to post
Share on other sites

 

 

Hi,

 

Try a different source url and destination filename.

Likely you are looking in the wrong place for either.

 

So, try a different file, and afterwards if the 1kb persists, check if the 1kb is equal in content as a manual download or not.

 

Look at the bytes downloaded, likely its html describing a 404, 401 or 500 error describing a issues likely caused by malformed url (did you properly double escape / for example in constant urk strings?)h

 

Hope this helps

 

Link to post
Share on other sites

19 minutes ago, EvilCat70 said:

Hey. I need to be able to download a .zip file using Java. However, the file is always 1KB even though the original file is 28MB. I've tried many different methods that I 


try {
			InputStream in = url.openStream();
			Files.copy(in, Paths.get(codeName + ".zip"), StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			e.printStackTrace();
		}

 

All of these return the same results, a 1KB zip that cannot be opened. The original file IS a 28MB zip file and I've verified the download works correctly by downloading it myself normally.

 

Oops forgote to quote in above post for notification :)

Link to post
Share on other sites

5 minutes ago, Bartholomew said:

 

Oops forgote to quote in above post for notification :)

I fixed it, I needed to use HTTPS instead of HTTP

 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×