Hello, my friends!
Downloaded file size and original file size on my HTTP is the same - everything is well. But When I open a downloaded mp3 file it has knocking pauses every 2 or 3 seconds, what's happening? when the file is in my computer(HDD), everything works well, but when it is on the internet (HTTP), there is this problem, It sounds like damaged mp3, but the file Size in Byte is the same
thanks a lot! I love Java 'n I love forum of Java Developers
PHP Code:
InputStream in;
FileOutputStream out;
byte buffer[];
try {
URL url1 = new URL("http://teodore.ge/a.mp3"); //mp3 file
in = url1.openStream(); // openStream in InputStream
out = new FileOutputStream("new.mp3"); // new mp3 File
/* I need HttpURLConnection for to request the file length from the Server */
HttpURLConnection conn = null;
conn = (HttpURLConnection) url1.openConnection();
conn.setRequestMethod("HEAD");
conn.getInputStream();
/* ****************** */
int readedByte = 0;
int avaiable = conn.getContentLength();
int bufferSize=1024;
buffer = new byte[bufferSize];
while (readedByte <= conn.getContentLength() && readedByte >= 0)
{
if (avaiable < 1024) // if there is less that 1024 byte left use new bufferSize
{
buffer = new byte[avaiable]; // Size of new bufferSize
in.read(buffer);
out.write(buffer);
readedByte = readedByte + avaiable; // Readed Bytes
avaiable = avaiable - avaiable; // left Byte
break;
}
// if there is more that 1024 byte, use buferrSize 1024
in.read(buffer);
out.write(buffer);
readedByte = readedByte + bufferSize; // ReadedByte
avaiable = avaiable - bufferSize; // left Byte
}
} catch (Exception e) {
e.printStackTrace();
}