FTPClient - Java, downloaded file has 0 kb in size - java

I tried this code to download a file from my company's ftp site. The file gets downloaded but has 0 kb in size. Any idea? Thanks a lot !
package org.kodejava.example.commons.net;<br/><br/>
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.io.FileOutputStream;
public class FtpDownloadDemo {
public static void main(String[] args) {
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect("ftp.domain.com");
client.login("admin", "secret");
String filename = "sitemap.xml";
fos = new FileOutputStream(filename);
client.retrieveFile("/" + filename, fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Related

How to download chosen file from ftp-server using java? [duplicate]

With this code iI always get a empty file.
What I have to do with it?
login is always true. (ofc, here is not real password)
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.*;
public class Logs {
public static void main(String[] args) {
FTPClient client = new FTPClient();
try {
client.connect("myac.cs-server.pro", 121);
boolean login = client.login("a3ro", "passWordIsSecret");
System.out.println(login);
String remoteFile1 = "myac_20150304.log";
File downloadFile1 = new File("C:\\Users\\Aero\\Desktop\\test\\myac.log");
OutputStream outputStream1 =
new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = client.retrieveFile(remoteFile1, outputStream1);
System.out.println(success);
outputStream1.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Use FileOutputStream:
String filename = "test.txt";
FileOutputStream fos = new FileOutputStream(filename);
client.retrieveFile("/" + filename, fos);
Use something like this:
InputStream inputStream = client.retrieveFileStream(remoteFileNameHere);
To retrieve the remote file input stream.
Then you can use to copy the stream to desired file:
FileOutputStream out = new FileOutputStream(targetFile);
org.apache.commons.io.IOUtils.copy(in, out);

Unzip files in FTP server using Java

I am trying to unzip files in the FTP location, but when i unzip i am not able to get all the files in FTP server, but when i try the code to unzip files to local machine it is working. I am sure somewhere while writing the data to FTP i am missing something.Below is my code. Please help me on this.
public void unzipFile(String inputFilePath, String outputFilePath) throws SocketException, IOException {
FileInputStream fis = null;
ZipInputStream zipIs = null;
ZipEntry zEntry = null;
InputStream in = null;
FTPClient ftpClientinput = new FTPClient();
FTPClient ftpClientoutput = new FTPClient();
String ftpUrl = "ftp://%s:%s#%s/%s;type=i";
ftpClientinput.connect(server, port);
ftpClientinput.login(user, pass);
ftpClientinput.enterLocalPassiveMode();
ftpClientinput.setFileType(FTP.BINARY_FILE_TYPE);
String uploadPath = "path";
ftpClientoutput.connect(server, port);
ftpClientoutput.login(user, pass);
ftpClientoutput.enterLocalPassiveMode();
ftpClientoutput.setFileType(FTP.BINARY_FILE_TYPE);
try {
// fis = new FileInputStream(inputFilePath);
String inputFile = "/Srikanth/RecordatiFRA_expenses.zip";
String outputFile = "/Srikanth/FR/";
in = ftpClientinput.retrieveFileStream(inputFile);
zipIs = new ZipInputStream(new BufferedInputStream(in));
while ((zEntry = zipIs.getNextEntry()) != null) {
try {
byte[] buffer = new byte[4 * 8192];
FileOutputStream fos = null;
OutputStream out = null;
// String opFilePath = outputFilePath + zEntry.getName();
String FTPFilePath = outputFile + zEntry.getName();
// System.out.println("Extracting file to "+opFilePath);
System.out.println("Extracting file to " + FTPFilePath);
// fos = new FileOutputStream(opFilePath);
out = ftpClientoutput.storeFileStream(FTPFilePath);
// System.out.println(out);
int size;
while ((size = zipIs.read(buffer, 0, buffer.length)) != -1) {
// fos.write(buffer, 0 , size);
out.write(buffer, 0, size);
}
// fos.flush();
// fos.close();
} catch (Exception ex) {
ex.getMessage();
}
}
zipIs.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (ftpClientinput.isConnected()) {
ftpClientinput.logout();
ftpClientinput.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
This method will do what you want, you can tweak it as you like.
I cleaned up and removed a lot that you didn't need; One thing to note is the use of try-with-resources blocks and not declaring your local variables so far from where they're used.
Your main error was that you needed to call completePendingCommand after certain methods as noted in their documentation.
Remember to read the documentation on methods you're using for the first time.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public static void unzipFTP(String server, int port, String user, String pass, String ftpPath)
throws SocketException, IOException {
FTPClient ftp = new FTPClient();
ftp.connect(server, port);
ftp.login(user, pass);
ftp.enterLocalPassiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
try (InputStream ftpIn = ftp.retrieveFileStream(ftpPath);
ZipInputStream zipIn = new ZipInputStream(ftpIn);) {
// complete and verify the retrieve
if (!ftp.completePendingCommand()) {
throw new IOException(ftp.getReplyString());
}
// make the output un-zipped directory, should be unique sibling of the target zip
String outDir = ftpPath + "-" + System.currentTimeMillis() + "/";
ftp.makeDirectory(outDir);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
throw new IOException(ftp.getReplyString());
}
// write the un-zipped entries
ZipEntry zEntry;
while ((zEntry = zipIn.getNextEntry()) != null) {
try (OutputStream out = ftp.storeFileStream(outDir + zEntry.getName());) {
zipIn.transferTo(out);
}
if (!ftp.completePendingCommand()) {
throw new IOException(ftp.getReplyString());
}
}
} finally {
try {
if (ftp.isConnected()) {
ftp.logout();
ftp.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

How to copy file in resource folder in java

Need a quick help. I am new to Java..In my project I have input & output folder in resources. Inside input folder I have a csv file. I need to move that file to output folder through java code. How to copy that input file to output directory. I have tried in google but not getting a working solution. My project structure is based on standard maven project.
I think there are at least four methods you can use to move your csv file to some other directory.
I assume that you already know the absolute directory path.
Method 1. The way of apache commons
You can use apache commons io library.
public static void moveWithApacheCommonsIO()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
try {
FileUtils.moveFile(sourceFile, destinationFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Method 2. The way of java's NIO
You can also use nio (non-blocking io) in java
public static void moveWithFileNIO()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
sourceFile.deleteOnExit();
try {
inputStream = new FileInputStream(sourceFile);
outputStream = new FileOutputStream(destinationFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final FileChannel inChannel = inputStream.getChannel();
final FileChannel outChannel = outputStream.getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
inChannel.close();
outChannel.close();
inputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Method 3. The traditional way of java's IO
You can use traditional method with file in and output stream in java.(Blocking IO)
public static void moveWithFileInOutStream()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
InputStream fin = null;
OutputStream fout = null;
sourceFile.deleteOnExit();
try {
fin = new BufferedInputStream(new FileInputStream(sourceFile));
fout = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte[] readBytes = new byte[1024];
int readed = 0;
while((readed = fin.read(readBytes)) != -1)
{
fout.write(readBytes, 0, readed);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
fin.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Method 4. Using JNI(Java Native Interface)
Finally, you can use some function like mv or MovFile depending on your Operating system with JNI.
I think it is out of scope of this topic. you can google it or see JNA library to accomplish your task if you want.
Here are complete code for you.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import org.apache.commons.io.FileUtils;
public class MovefileTest {
public static void moveWithApacheCommonsIO()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
try {
FileUtils.moveFile(sourceFile, destinationFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void moveWithFileInOutStream()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
InputStream fin = null;
OutputStream fout = null;
sourceFile.deleteOnExit();
try {
fin = new BufferedInputStream(new FileInputStream(sourceFile));
fout = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte[] readBytes = new byte[1024];
int readed = 0;
while((readed = fin.read(readBytes)) != -1)
{
fout.write(readBytes, 0, readed);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
fin.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void moveWithFileNIO()
{
File sourceFile = new File("resource/AssetsImportCompleteSample.csv");
File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv");
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
sourceFile.deleteOnExit();
try {
inputStream = new FileInputStream(sourceFile);
outputStream = new FileOutputStream(destinationFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final FileChannel inChannel = inputStream.getChannel();
final FileChannel outChannel = outputStream.getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
inChannel.close();
outChannel.close();
inputStream.close();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//moveWithApacheCommonsIO();
//moveWithFileNIO();
moveWithFileInOutStream();
}
}
Regards,

How to save xml data from a URL to a file?

What I wanna do is get the content of this URL :
https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=CYQB&hoursBeforeNow=2
and copy it to a file so I can parse it and use the elements.
Here is what I have so far :
package test;
import java.io.*;
import java.net.*;
import org.apache.commons.io.FileUtils;
public class JavaGetUrl {
#SuppressWarnings("deprecation")
public static void main(String[] args) throws FileNotFoundException {
URL u;
InputStream is = null;
DataInputStream dis;
String s = null;
try {
u = new URL(
"https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=CYQB&hoursBeforeNow=2");
is = u.openStream(); // throws an IOException
dis = new DataInputStream(new BufferedInputStream(is));
while ((s = dis.readLine()) != null) {
System.out.println(s);
FileUtils.writeStringToFile(new File("input.txt"), s);
}
} catch (MalformedURLException mue) {
System.out.println("Ouch - a MalformedURLException happened.");
mue.printStackTrace();
System.exit(1);
} catch (IOException ioe) {
System.out.println("Oops- an IOException happened.");
ioe.printStackTrace();
System.exit(1);
} finally {
try {
is.close();
} catch (IOException ioe) {
}
}
}
}
The problem is that the content of s does not show up in input.txt.
If I replace s by any other strings it works. So I guess it's a problem with the data of s. Is it because it's xml?
Thank you all for the help.
The file is probably getting over-written.
You should use "append" mode to get file appended with data(from readLine).
public static void writeStringToFile(File file,
String data,
boolean append)
as you are already using apaches commons-io, you can also simply use
FileUtils.copyURLToFile(URL, File)
see https://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL,%20java.io.File)

Error in ZipOutputStream + FTPClient

I've to upload a zip file to ftp server, And here zip file also constructing dynamically.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
public class CommonsNet {
public static void main(String[] args) throws Exception {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect("127.0.0.1");
client.login("phani", "phani");
String filename = "D://junk.pdf";
fis = new FileInputStream(new File(filename));
byte[] bs = IOUtils.toByteArray(fis);
fis.close();
OutputStream outputStream = client.storeFileStream("remote.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
zipOutputStream.setLevel(ZipOutputStream.STORED);
addOneFileToZipArchive(zipOutputStream,
"junk.pdf", bs);
zipOutputStream.close();
outputStream.close();
client.logout();
System.out.println("Transfer done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void addOneFileToZipArchive(ZipOutputStream zipStream,
String fileName, byte[] content) throws Exception {
ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName);
zipStream.putNextEntry(zipEntry);
zipStream.write(content);
zipStream.flush();
zipStream.closeEntry();
}
}
After executing this code the file is successfully created but i am unable to open a file inside archive.
like :
! D:\phani\remote.zip: The archive is corrupt
! D:\phani\remote.zip: Checksum error in C:\Users\BHAVIR~1.KUM\AppData\Local\Temp\Rar$DIa0.489\MCReport.pdf. The file is corrupt
Try adding client.setFileType(FTP.BINARY_FILE_TYPE); just after you have logged in.
I remember that default transfer mode is ASCII, so non-ascii files may result corrupted.

Categories