I'm using Java to download a file, I have used three different code snippets that I'm going to post here. But the result for all is same. They are all downloading the file partially and as a result the file can't be opened as it is not downloaded completely. How I should fix this problem.
My first code:
public class FileDownloader {
final static int size=1024;
public static void main(String[] args){
fileUrl("http://textfiles.com/holiday","holiday.tar.gz","C:\\Users\\Me\\Downloads");
}
public static void fileUrl(String fAddress, String localFileName, String destinationDir) {
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
try {
URL Url;
byte[] buf;
int ByteRead,ByteWritten=0;
Url= new URL(fAddress);
outStream = new BufferedOutputStream(new
FileOutputStream(destinationDir+"\\"+localFileName));
uCon = Url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((ByteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, ByteRead);
ByteWritten += ByteRead;
}
System.out.println("Downloaded Successfully.");
System.out.println("File name:\""+localFileName+ "\"\nNo ofbytes :" + ByteWritten);
}catch (Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
outStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
Second code I used:
public class downloadFile {
public static void main(String[]args){
downloadFile dfs = new downloadFile();
dfs.downloadFileAndStore("C:\\Users\\Me\\Downloads","Sign&ValidateVersion2.docx");
}
/**
* download and save the file
* @param fileUrl: String containing URL
* @param destinationDirectory : directory to store the downloaded file
* @param fileName : fileName without extension
*/
public void downloadFileAndStore(String destinationDirectory,String fileName){
// URL url = null;
FileOutputStream fos = null;
//convert the string to URL
try {
// url = new URL(fileUrl);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("https://mail.uvic.ca/owa/#path=/mail");
HttpResponse response = client.execute(request);
if(response.getEntity().getContent().read()==-1){
Log.error("Response is empty");
}
else{
BufferedReader rd = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
fos = new FileOutputStream(destinationDirectory + "\\" + fileName);
fos.write(result.toString().getBytes());
fos.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.error("PDF " + fileName + " error: " + e.getMessage());
}
}
}
the third code I used:
public class download {
public static void main(String[] args) {
download dfs = new download();
dfs.downloadFile();
}
public void downloadFile(){
try {
IOUtils.copy(
new URL("https://archive.org/details/alanoakleysmalltestvideo").openStream(),
new FileOutputStream("C:\\Users\\Me\\Downloads\\spacetestSMALL.wmv")
);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I have to download a video file, which is about 6-7 MB, however I have used these three codes for small text files and other types, it didn't work. Any one have any idea?