I'm trying to upload image to my server but when I'm trying to open this image - it's not possible to open, apparently because it's corrupted. Can someone help me and tell me why? thanks!
I'm using the Apache Commons Net jar.
String hostName = "host";
String username = "username";
String password = "pass";
String location = "filePath.png";
FTPClient ftp = null;
InputStream in = null;
try {
ftp = new FTPClient();
ftp.connect(hostName);
ftp.login(username, password);
ftp.changeWorkingDirectory("/pictures");
int reply = ftp.getReplyCode();
System.out.println("Received Reply from FTP Connection:" + reply);
if(FTPReply.isPositiveCompletion(reply))
{
System.out.println("Connected Success");
}
ftp.setFileType(FTP.BINARY_FILE_TYPE);
File f1 = new File(location);
in = new FileInputStream(f1);
ftp.storeFile("fileName.png",in);
System.out.println("SUCCESS");
in.close();
ftp.logout();
ftp.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
My antivirus was blocking it..
Related
I have uploaded a file on server 'www.server.com'. Now I wish to know the path of the file on the same server through java code. Is there any specific method or process present? I am new to this.
public class Test {
public static void main(String[] args) {
test test = new test();
String server ="www.server.com";
int port = 21;
String username = "abc";
String password = "abc`enter code here`";
FTPClient ftpclient = new FTPClient();
try {
ftpclient.connect(server, port);
ftpclient.login(username, password);
ftpclient.enterLocalPassiveMode();
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
File firstLocalFile = new File("D:\\ADF\\Tax Files\\TERData.zip");
String firstRemoteFile = "TERData.zip";
InputStream inputstream = new FileInputStream(firstLocalFile);
System.out.println("Start Uploading the First File");
System.out.println(" File Path ":+firstLocalFile.getAbsolutePath());
boolean done = ftpclient.storeFile(firstRemoteFile, inputstream);
if(done){
System.out.println(" File successfully uploaded ");
}
} catch (IOException e) {
// TODO: Add catch code
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FTPFile[] files1 = ftpClient.listFiles("/test");//"/test/test.txt";
String remoteFile1 =files1[0].getName();
File downloadFile1 = new File("D:/Downloads/test.mp4");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
I tried to download csv file from ftp .
File is downloaded successfully but it gives me garbage value in that file.
Following is my code
String server = "client.in";
int port = 21;
String user = "user";
String pass = "pwd";
InputStream input=null;
BufferedWriter out = null;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
FileOutputStream fos = null;
String filename = "Test.csv";
OutputStream data = (OutputStream) new FileOutputStream("Test/"+filename);
ftpClient.retrieveFile(filename, data);
data.close();
}
catch(Exception e)
{
e.printStackTrace();
}
What is the solution ....
please help
I'm trying to upload an image from android to ftp server, but when i try to open the image that i uploaded i see the following message instead of the image "the image cannot be displayed because it contains errors"
and this is the code that i use
public void uploadImage(String path){
String server = "www.domainname.com";
int port = 21;
String user = "ftp-username";
String pass = "ftp-password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: uploads first file using an InputStream
File firstLocalFile = new File(path);
long fileSize = firstLocalFile.length();
Log.i("File Size",fileSize+"");
String firstRemoteFile = "testfile1.jpg";
InputStream inputStream = new FileInputStream(firstLocalFile);
Log.i("uploading", "Start uploading first file");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done) {
Log.i("uploaded", "finished uploading first file");
}
// APPROACH #2: uploads second file using an OutputStream
File secondLocalFile = new File(path);
String secondRemoteFile = "testfile2.jpg";
inputStream = new FileInputStream(secondLocalFile);
Log.i("uploading", "Start uploading second file");
OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
}
inputStream.close();
outputStream.close();
boolean completed = ftpClient.completePendingCommand();
if (completed) {
Log.i("uploaded", "finished uploading second file");
}
} catch (IOException ex) {
Log.i("Error", "Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
where is the error!!?
Thanks in advance..
This looks suspiciously like this bug: FTPClient corrupts the images while uploading to ftp server on android?
Try using FTP4J instead.
I am able to connect Ftp server. My requirement is to check if the path that will be shown after login into the server is writable in the ftp server.
Below code is not checking File remotefile = new File(pwd)
public StringBuffer verifyMath(String host, String uname, String password, String cType){
String MathString = "FTPHost:[" + host + "];uname[" + uname + "];cType[" + cType + "]";
StringBuffer mBuffer = new StringBuffer();
FileInputStream fis = null;
FTPClient client = new FTPClient();
try {
client.connect(host);
boolean login = client.login(uname, password);
client.getReplyCode(); //230
if (login == true) {
log.debug("Connection established...");
String pwd = client.printWorkingDirectory();
File remotefile = new File(pwd);
boolean rmtfile = remotefile.canWrite();
boolean rmtdir = remotefile.isDirectory();
if(!(remotefile.isDirectory() && remotefile.canWrite())) {
mBuffer.append(MathLogger.raiseError(MathString, "Math is not Writable"));
}
boolean logout = client.logout();
if (logout) {
log.debug("Connection close...");
}
} else {
mBuffer.append(MathLogger.raiseError(MathString, "Connection failed."));
}
} catch (IOException e) {
mBuffer.append(MathLogger.raiseError(MathString, e));
} finally {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return mBuffer;
}
mlistFile (or possibly mlistDir) is probably the API you are looking for to call on the remote directory. That returns an FTPFile object that has the permission info. Of course these will only work if the FTP server supports RFC 3659 extensions.
So something like:
FTPFile remoteDir = client.mlistFile(client.printWorkingDirectory());
if (remoteDir.hasPermission(FTPFile.USER_ACCESS,FTPFile.WRITE_PERMISSION)) {
...
}
I created a function to download files from an FTP server that I have access to. How would I upload files back to the FTP server?
Below is the download_files method i used:
public static void download_files(String un, String pw, String ip, String dir, String fn, String fp){
URLConnection con;
BufferedInputStream in = null;
FileOutputStream out = null;
try{
URL url = new URL("ftp://"+un+":"+pw+"#"+ip+"/"+dir+"/"+fn+";type=i");
con = url.openConnection();
in = new BufferedInputStream(con.getInputStream());
out = new FileOutputStream(fp+fn);
int i = 0;
byte[] bytesIn = new byte[1024];
while ((i = in.read(bytesIn)) >= 0) {
out.write(bytesIn, 0, i);
}
}catch(Exception e){
System.out.print(e);
e.printStackTrace();
System.out.println("Error while FTP'ing "+fn);
}finally{
try{
out.close();
in.close();
}catch(IOException e){
e.printStackTrace();
System.out.println("Error while closing FTP connection");
}
}
}
Use the FTPClient Class from the Apache Commons Net library.
This is a snippet with an example:
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();
}
}
Snippet taken from http://www.kodejava.org/examples/356.html
I have used the EDT FTP package, a free GPL library for FTP in Java: http://www.enterprisedt.com/products/edtftpj/overview.html
Here is a code sample, from the Demo.java class they provide:
ftp = new FTPClient();
ftp.setRemoteHost("hostname");
// connect
ftp.connect();
// login
ftp.login("user", "password");
// set up passive ASCII transfers
ftp.setConnectMode(FTPConnectMode.PASV);
ftp.setType(FTPTransferType.ASCII);
// get directory and print it to console
String[] files = ftp.dir(".", true);
for (int i = 0; i < files.length; i++)
log.debug(files[i]);
// copy file to server
ftp.put("test.txt", "test.txt");
// copy file from server
ftp.get("test.txt" + ".copy", "test.txt");
// delete file from server
ftp.delete("test.txt");
// Shut down client
ftp.quit();
Check out FTP4J as well...
Take a look at apache-commons-net they have a some FTP tools which may help you out!