File download in httpclient using java? - java

My code is,
import java.io.*;
import java.net.*;
public class DownloadHttp
{
public static void main(String a[])
{
DownloadHttp d = new DownloadHttp();
String addr = "http://www.gmail.com";
String file = "D:/venkatesh/Software/download1.html";
d.download(addr,file);
}
public void download(String address, String localFileName) {
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try {
// Get the URL
URL url = new URL(address);
// Open an output stream to the destination file on our local filesystem
out = new BufferedOutputStream(new FileOutputStream(localFileName));
conn = url.openConnection();
in = conn.getInputStream();
// Get the data
byte[] buffer = new byte[1024];
int numRead;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
}
// Done! Just clean up and get out
} catch (Exception exception) {
exception.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
// Shouldn't happen, maybe add some logging here if you are not
// fooling around ;)
}
}
}
}
Here I wants download specific file using httpClient using java.
It produces:
"java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)" as error.
How to resolve it, help me, thanks in advance.

I believe it is a network problem. Have you tried to access the url directly or are you behind a firewall?

Recompiled your code on my machine, it works perfectly well. I'm able to fetch files from the web.
Check if your web-browser can download the file for you (make sure it's not a network problem)
One thing to notice though, in your finally block you might want to close the streams separately. So if anything goes wrong with the input stream, the output stream will still be closed.
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception ignored) {}
try {
if (out != null) {
out.close();
}
} catch (Exception ignored) {}
}

I think you are using a proxy when connecting to internet.
Set these in the code and then retry.
System.setProperty("http.proxyHost", *Proxy-IP*);
System.setProperty("http.proxyPort", *Proxy-Port*);

Related

Java simple update method

So i have this simple method to download and replace a file:
public void checkForUpdates() {
try {
URL website = new URL(downloadFrom);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(downloadTo);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
} catch (IOException e) {
System.out.println("No files found");
}
}
How can i check if there is a concrete file with a certain name located in the destination (downloadFrom) ? Right now if there are no files it downloads the html page.
You can get content type from header
URL url = new URL(urlname);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
String contentType = connection.getContentType();
then check it's HTML/text or files.
I suggest to check the HTTP code for code 200. Something along these lines:
public class DownloadCheck {
public static void main(String[] args) throws IOException {
System.out.println(hasDownload("http://www.google.com"));
System.out.println(hasDownload("http://www.google.com/bananas"));
}
private static boolean hasDownload(String downloadFrom) throws IOException {
URL website = new URL(downloadFrom);
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) website.openConnection();
return connection.getResponseCode() == 200; // You could check other codes though
}
catch (Exception e) {
Logger.getLogger(OffersUrlChecker.class.getName()).log(Level.SEVERE,
String.format("Could not read from %s", downloadFrom), e);
return false;
}
finally {
if (connection != null) {
connection.disconnect(); // Make sure you close the sockets
}
}
}
}
If you run this code, you will get:
true
false
as the output.
You could consider to consider other code than code 200 as OK. See more information on HTTP codes here.

ftp connection to determine if file is there

I am trying to check if the file exist in the FTP. When testing with one user it seems to be fine. But with the scenario of multiple users it seems to throw the below exception :
Exception in thread "main" sun.net.ftp.FtpProtocolException: Welcome message: 421.
Below is the code which we use to check if file is there and we have closed all the connection but still it throws sun.net.ftp.FtpProtocolException:Welcome message: 421.
public boolean getFtpFileExists(String fileUrl)
{
URL theURL = null;
InputStream inputStream = null;
FtpURLConnection ftpUrlConn = null;
boolean ftpFileExists = false;
try
{
theURL = new URL(fileUrl);
ftpUrlConn = (FtpURLConnection)theURL.openConnection();
inputStream = ftpUrlConn.getInputStream();//calling this method will throw a 'FileNotFoundException' if doesn't exist
ftpFileExists = true;
}
catch(FileNotFoundException fnfe)
{
ftpFileExists = false;
}
catch(Exception e)
{
System.out.println(e.toString());
ftpFileExists = false;//hmm, not sure really!
}
finally
{
//close inputStream & connection
if(inputStream != null)
{
try
{
inputStream.close();
}
catch(IOException ioe)
{
System.out.println("Error closing input stream: "+ioe.getMessage());
}
}
try
{
ftpUrlConn.close();
}
catch(Exception e)
{
System.out.println("Error closing ftpUrlConnection");
}
}
return ftpFileExists;
}
could anyone help me please?
Can you recreate the error that you get ?
at any case:
by the error MSG:
421: Service not available, closing control connection. This may be a
reply to any command if the service knows it must shut down.
It sounds like that maybe there's a configuration needs to be done at the FTPServer rather than a code error.

Runtime.getRuntime().exec(), how to execute file from website link?

So far I've got this line to work perfectly and it executes calc.exe on my computer:
Runtime.getRuntime().exec("calc.exe");
But how can I download and execute a file from a website link? For example http://website.com/calc.exe
I found this code on the web but it doesn't work:
Runtime.getRuntime().exec("bitsadmin /transfer myjob /download /priority high http://website.com/calc.exe c:\\calc.exe &start calc.exe");
You use URL and/or URLConnection, download the file, save it somewhere (current working directory, or a temp directory, for example), then execute it using the Runtime.getRuntime().exec().
Using this answer as a starting point you could do this: (this uses HttpClient)
public static void main(String... args) throws IOException {
System.out.println("Connecting...");
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://website.com/calc.exe");
HttpResponse response = client.execute(get);
InputStream input = null;
OutputStream output = null;
byte[] buffer = new byte[1024];
try {
System.out.println("Downloading file...");
input = response.getEntity().getContent();
output = new FileOutputStream("c:\\calc.exe");
for (int length; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
System.out.println("File successfully downloaded!");
Runtime.getRuntime().exec("c:\\calc.exe");
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}
}

Java - Storing File on FTP Server fails

I am trying to store a byteArrayInputStream as File on a FTP Server. I could already connect to the Server and change the working path, but triggering the method to store the Stream as File on the Server returns always false.
I am using the apache FTPClient.
Can someone please give me a hint where my mistake can be!?
Here the Code:
String filename = "xyz.xml"
// connection returns true
connectToFtpServer(ftpHost, ftpUser, ftpPassword, exportDirectory);
// byteArray is not void
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
try {
// change returns true
result = ftpClient.changeWorkingDirectory(exportDirectory);
// storing the file returns false
result = ftpClient.storeFile(filename, byteArrayInputStream);
byteArrayInputStream.close();
ftpClient.logout();
} catch (...) {
...
} finally {
// disconnect returns true
disconnectFromFtpServer();
}
I don't believe it's your code. Here is another example that looks very similar from kodejava:
package org.kodejava.example.commons.net;
import org.apache.commons.net.ftp.FTPClient;
import java.io.FileInputStream;
import java.io.IOException;
public class FileUploadDemo {
public static void main(String[] args) {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("ftp.domain.com");
client.login("admin", "secret");
//
// Create an InputStream of the file to be uploaded
//
String filename = "Touch.dat";
fis = new FileInputStream(filename);
//
// Store file to server
//
client.storeFile(filename, fis);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I agree it's file permissions. There is not a way to change permissions in java itself yet, but there are other solutions. See this thread: How do i programmatically change file permissions?
HTH,
James
It was actually a permission issue due to an invalid usergroup. After adding my user to the usergroup, i was able to store again files.

Apache Commons FTP problems

I want to implement a FTP Client with Apache Commons Net only for uploading data.
The Connection and Login to FTP-Server works fine.
But the upload does not work right.
The files are a little to big as the originals.
And the files are damaged.
I tried an image, a video and a textfile. Only the textfile is alright.
Now I see while debugging
boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
gives me false. So it can not be set. Why?
(Maybe this is not the problem?)
Here a the rest of my code
client=new FTPClient();
try {
int reply;
client.connect(url, port);
reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
client.login(user, pw);
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
client.setControlKeepAliveTimeout(300);
client.enterLocalPassiveMode();
if (client.isConnected())
{
try {
File file=new File(<FILE>);
FileInputStream inputStream = new FileInputStream(file);
OutputStream outputStream = client.storeFileStream(file.getName());
byte[] buffer = new byte[4096];
int l;
while((l = inputStream.read(buffer))!=-1)
{
outputStream.write(buffer, 0, l);
}
inputStream.close();
outputStream.flush();
outputStream.close();}
Change the following:
boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
Should be:
boolean xxx=client.setFileType(FTP.BINARY_FILE_TYPE);
You have confused FileTransferModes with FileTypes.
The available FileTypes are:
FTP.ASCII_FILE_TYPE (default)
FTP.BINARY_FILE_TYPE
FTP.EBCDIC_FILE_TYPE
FTP.LOCAL_FILE_TYPE
The available FileTransferModes are:
FTP.STREAM_TRANSFER_MODE (default)
FTP.BLOCK_TRANSFER_MODE
FTP.COMPRESSED_TRANSFER_MODE
I suppose if apache introduced enums for these constant types, then this kind of problem could be avoided, but then the library would not be available to pre-java-5 runtimes.
I wonder how much of an issue java 1.4 compatibility really is.
If only the text file was transferred successfully, I suspect you need to set the binary transfer file type.
See the setFileType method to see how to do this.
The commons-net wiki mentions this is the cause of most file corruption issues.
This work for me, Uploading Image and download after It´s Ok: Using
FTP.LOCAL_FILE_TYPE
this code using logger, replace for you logger or use System.out.println("");
private void cargarData(File filelocal) {
FTPClient client = new FTPClient();
try {
client.connect("URLHOSTFTP", "PORT: DEFAULT 21");
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
client.disconnect();
logger.error("FTP server refused connection.");
System.exit(1);
}
client.login("USER FTP", "PASS FTP");
boolean type = client.setFileType(FTP.LOCAL_FILE_TYPE);
logger.info("Tipo Aceptado:" + type);
client.setControlKeepAliveTimeout(300);
client.enterLocalPassiveMode();
if (client.isConnected()) {
FileInputStream fis = null;
fis = new FileInputStream(filelocal);
client.storeFile(filelocal.getName(), fis);
client.logout();
if (fis != null) {
fis.close();
}
}
logger.info(client.getReplyString());
} catch (IOException e) {
logger.error("error" + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
logger.error("error" + e.getMessage());
e.printStackTrace();
}
}

Categories

Resources