FTP client in java to upload file sometimes shows error - java

HI Friends I am working on a java application to store the file into the server
but when i upload some file its shows
org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
QUIT
550 can't access file.
can any body tell me how to solve that?
public void uploadtxtFile(String localFileFullName, String fileName, String hostDir)
throws Exception {
FTPClient ftpclient= DBConnection.connect();
File file = new File(localFileFullName);
if (!(file.isDirectory())) {
if (file.exists()) {
FileInputStream input = null;
BufferedInputStream bis=null;
try {
input = new FileInputStream(new File(localFileFullName));
if (input != null) {
hostDir = hostDir.replaceAll("//", "/");
logger.info("uploading host dir : " + hostDir);
boolean bool =false ;
logger.error("Replay of the ftp store file is 1111"+ ftpclient.getReplyCode());
try{
ftpclient.enterLocalPassiveMode();
logger.error("Replay of the ftp store file is 2222"+ ftpclient.getReplyCode());
if( ftpclient.isConnected()){
// here server timeout error is get
logger.error("here server timeout error is get");//new
bis = new BufferedInputStream(input);
logger.error("Replay of the ftp store file is 3333"+ ftpclient.getReplyCode());
bool = ftpclient.storeFile(hostDir, bis);
} else{
logger.error("here server timeout error is get");//new
bis = new BufferedInputStream(input);
logger.error("Replay of the ftp store file is 6666"+ ftpclient.getReplyCode());
ftpclient.enterLocalPassiveMode();
bool = ftpclient.storeFile(hostDir, bis);
}}finally{
bis.close();
input.close();
}
logger.error("Replay of the ftp store file is 4444 "+ ftpclient.getReplyCode());
if (bool) {
logger.info("Success uploading file on host dir :"+hostDir);
} else {
logger.error("file not uploaded.");
}
} else {
logger.error("uploading file input null.");
}
} catch(Exception ex)
{
logger.error("Error in connection ="+ex);
}finally {
ftpclient.logout();
ftpclient.disconnect();
}
} else {
logger.info("uploading file is not exists.");
}
}
}
and also the response of file folder is not show

SYMPTOMS
When attempting to upload a file to a remote FTP site, a 550 error code is encountered, resulting in an error message similar to one of the following examples:
Example 1:
STATUS:> Transferring file "/pub/yourfile.txt"...
COMMAND:> SIZE yourfile.txt
550 yourfile.txt: No such file.
STATUS:> Requested action not taken (e.g., file or directory not found, no access).
COMMAND:> CWD /pub/yourfile.txt
550 /pub/yourfile.txt: No such file or folder.
STATUS:> Requested action not taken (e.g., file or directory not found, no access).
COMMAND:> STOR yourfile.txt
Example 2:
COMMAND:> STOR yourfile.txt
550 Permission Denied.
ERROR:> Requested action not taken (e.g., file or directory not found, no access).
CAUSE
Example 1:
In this example the 550 code returned by the remote FTP server is for
information purposes only. It is not an error and should be ignored
by the user. In this case an upload command has already been given
but before the upload can be started CuteFTP needs it determine
whether or not the file being transferred already exists on the remote
site as either a file or a folder.
First, the SIZE command is sent in an attempt to determine if a file with the same name exists on the remote site. The server
responds with a 550 indicating that the file does not already exist
there.
Next, the CWD command is sent in an attempt to determine if a folder with the same name exists on the remote site. The server
responds with a 550 indicating that a folder by that name does not
exist.
Finally, the STOR command is given and the file upload begins.
Example 2:
A file upload is being attempted but the remote server has denied the
needed permission. The 550 error code is a result of insufficient
account privileges on the remote FTP server. The error is not caused
by CuteFTP.
RESOLUTION
Example 1:
Not applicable. In this example the 550 code returned by the remote
FTP server is for information purposes only. It is not an error and
should be ignored by the user.
Example 2:
If you believe that your FTP account privileges or permissions are
configured incorrectly, contact the technical support department at
the remote FTP site or your Web hosting company for help.
Or you can check FTP "550 Access is denied" Error

Related

FTPSClient, What values to give for Remote and Local

I am new to FTPSClient i trying to connect to a FTPS created in my laptop. i don't exactly what some of the methods working and their parameter meaning.
For example,
In my code i have created a FTPSClient as below:
FTPSClient ftps =new FTPSClient();
Then connected to a server use connect() method with ip address.
ftps.connect("172.xx.xx.xxx");
After every step i will check the reply code using.
ftps.getReplyCode();
In the below code i know that
username = system username
password = the password to login
ftps.login(username, password);
In the my system in Internet Information Service(IIS). Created an ftp server with ssl and given the below directory to share.
C:\Users\karan-pt2843\Desktop\FTPS
Want to send the file in below directory to the server.
D:\sam.txt
Now i want to store a file in the server in the given above directory and i tried using
remote="";
local="";
InputStream input;
input = new FileInputStream(local);
ftps.storeFile(remote, input);
input.close();
I don't know what value to give for remote and local. please help me with the values to give on them and the what happens internal.
// Use passive mode as default because most of us are
// behind firewalls these days.
ftps.enterLocalPassiveMode();
...
String remote = "samFromClient.txt"; //Place on FTP
String input = "D:/sam.txt" //Place on your Client
//Your FTP reads from the inputstream and store the file on remote-path
InputStream input = new InputStream(new FileInputStream(input));
ftps.storeFile(remote, input);
input.close();
ftps.logout();
...
Taken from: Apache example

Java application hanging during LIST command to FTP Server (Apache Commons FTPClient)

I'm currently trying to pull a file from an FTP server, but I've hit a snag in the process. The code below is a only partial working piece that:
logs into the FTP server
makes a few setting changes
attempts to list the files found in that directory.
The issue: When the LIST command is pushed to the FTP server, it just hangs without timing out, erroring out, or tossing an exception. I actually accidentally left this process running over the weekend and it hung at this point the entire time. I've included the reply strings output from the FTP server, but I feel like there is more specific error information that I'm missing that would help me pinpoint this error.
Questions:
Is there another method in the apache-commons-net FTPSClient list to output more specific error information from the FTP server? I looked through the JavaDocs for a method and didn't find anything specific - but I'm admittedly notorious for missing methods in JavaDocs (my vision is a bit bad...)
Could I be missing a simple configuration step in the process of connecting to the FTP server? I've checked all of my configuration against the login information sent to me. Here is a screenshot of the Site Manager with server information from Filezilla (note: I've removed the ftp hostname, this is correct in the code). EDIT: The server is set to listen to port 991.
Could I be dealing with a port issue? I'm hoping if this is the case, then request 1 would be able to give me more detail...
We've tried this code in three different environments - one remote an two local. Any help on this would be much appreciated as both myself and a more senior developer on my team are stumped. Thank you for your time!
Main method:
FTPSClient ftp = new FTPSClient();
boolean error = false;
try {
int reply;
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
ftp.connect("SITE_NAME", 991);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
System.out.println("Set file type to binary");
ftp.sendCommand("PBSZ 0");
ftp.sendCommand("PROT P");
ftp.enterLocalPassiveMode();
System.out.println("Set mode to passive (local).");
//ftp.enterRemotePassiveMode();
//System.out.println("Set mode to passive.");
boolean success = ftp.login("USERNAME", "PASSWORD");
if (!success) {
System.out.println("Could not login to the server");
return;
} else {
System.out.println("LOGGED IN SERVER");
}
ftp.setBufferSize(1024 * 1024);
//ftp.sendCommand("OPTS UTF8 ON");
String directory = ftp.printWorkingDirectory();
System.out.println("Working directory: " + directory);
int timeout = ftp.getConnectTimeout();
System.out.println("Timeout Length: " + timeout);
FTPFile[] list = ftp.listFiles();
System.out.println("Generate List of Files");
int length = list.length;
System.out.println("Length: " + length);
ftp.logout();
} catch(IOException e) {
error = true;
e.printStackTrace();
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException ioe) {
// do nothing
}
}
//System.exit(error ? 1 : 0);
}
}
Output from Protocol Command Listener:
220 Microsoft FTP Service
AUTH TLS
234 AUTH command ok. Expecting TLS Negotiation.
TYPE I
200 Type set to I.
Set file type to binary
PBSZ 0
200 PBSZ command successful.
PROT P
200 PROT command successful.
Set mode to passive (local).
USER *******
331 Password required
PASS *******
230 User logged in.
LOGGED IN SERVER
PWD
257 "/" is current directory.
Working directory: /
Timeout Length: 0
SYST
215 Windows_NT
PASV
227 Entering Passive Mode (204,179,168,88,195,91).
LIST
125 Data connection already open; Transfer starting.

Java code for connect alfresco through ftps connection

When I try to connect our alfresco through SFTP it is not able to connect alfresco. It hangs the explorer and no error goes in the logger file also.
public void FTPTest()throws SocketException, IOException, NoSuchAlgorithmException
{
FTPSClient ftp = new FTPSClient("SSL");
System.out.println("1");
ftp.connect("172.17.178.144",2121); // or "localhost" in your case
System.out.println("2"+ftp.getReplyString());
System.out.println("login: "+ftp.login("admin", "admin"));
System.out.println("3"+ ftp.getReplyString());
ftp.changeWorkingDirectory("/alfresco");
// list the files of the current directory
FTPFile[] files = ftp.listFiles();
System.out.println("Listed "+files.length+" files.");
for(FTPFile file : files) {
System.out.println(file.getName());
}
// lets pretend there is a JPEG image in the present folder that we want to copy to the desktop (on a windows machine)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // don't forget to change to binary mode! or you will have a scrambled image!
FileOutputStream br = new FileOutputStream("C:\\Documents and Settings\\casonkl\\Desktop\\my_downloaded_image_new_name.jpg");
ftp.retrieveFile("name_of_image_on_server.jpg", br);
ftp.disconnect();
}
I got output in our console only
1
at the execution of ftp.connect("172.17.178.144",2121); this code system will be hang no error got in our console
I am able to connect to my Alfresco through SFTP with the Filezila FTP client software. Can any one help me resolve this issue?
If I'm not mistaken then Alfresco chose for FTPS.
So try it with the following code here: http://alvinalexander.com/java/jwarehouse/commons-net-2.2/src/main/java/examples/ftp/FTPSExample.java.shtml

Permission denied error for "ls" operation on SFTP server through Java API

I've an SFTP server with appropriate credentials setup.
I've an admin user on that machine, which has certain rights to perform certain actions.
So, I've edited the sudoers file to add action like this
sudo visudo -f /etc/sudoers
# Added the below line at end of file
admin ALL=NOPASSWD: /bin/ls
I want to list down a directory and get the count of files in that directory through Java API.
SFTPv3Client client = null;
try {
client = new SFTPv3Client(connection); // connection is successful.
} catch (IOException e) {
e.printStackTrace();
}
Vector list = null;
try {
list = client.ls(directoryName);
} catch (IOException e) {
e.printStackTrace(); // throws IOException
}
I've used ch.ethz.ssh2.SFTPv3Client jar. I'm able to connect to server and also authenticate through username and password.
But, when listing down the directory, I face below error
ch.ethz.ssh2.SFTPException: Permission denied (SSH_FX_PERMISSION_DENIED: The user does not have sufficient permissions to perform the operation.)
Does anybody have an idea about the cause of this issue ?

Jscape SFTP/FTP upload to root dir fails

I have been having an FTP issue using the com.jscape.FileTransfer API that I haven't been able to make sense for quite sometime. Here's my code:
public void do FTPTest() {
FileTransfer f = protocol.equalsIgnoreCase("FTP") ? new FtpTransfer(ftpHost, userName, password, new Integer((int) port)) : (protocol.equalsIgnoreCase("SFTP") ? new SftpTransfer(
ftpHost,
userName,
password,
new Integer((int) port)) : null);
try {
f.connect();
f.setAuto();
System.out.println(f.getDir());
f.upload("test".getBytes(), "ktest1.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
This code simply connects to an FTP/SFTP server and uploads a file called "ktest.txt"
My issue happens when I try to upload files to the base directory of the FTP/SFTP site.
Case 1: Connecting to FTP site 'A'
f.getDir() --> '/'
f.upload() --> SUCCEEDS
Case 2: Connecting to SFTP site 'B'
f.getDir() --> '/'
f.upload() --> FAILS (with permission denied error)
However,
Case 3: Connecting to SFTP site 'B'
f.getDir() --> '/'
f.setDirUp()
f.getDir() --> ''
f.upload() --> SUCCEEDS
Notice how it works when I do a setDirUp()
If i try to do the same on site A,
Case 4: Connecting to FTP site 'A'
f.getDir() --> '/'
f.setDirUp() (permission denied error)
All cases succeed if a do a setDir('testFolder') before uploading the file. i.e. a dir that is not in the base dir.
I don't understand how doing a setDirUp from '/' --> '' lets you upload a file to the base directory. What happens when you do an UP from '/'. What is '' ?
And how uploading to '/' works for some FTP sites.
BTW both FTP sites lets me upload files to the base dir without any issues using a client like WinSCP.
Could be that base dir / is not allowed for upload operation. When you do setDirUp() FTP server moves to back to default directory which is allowed for FTP transfer. Try to upload something to / with some GUI FTP client and see if it works.

Categories

Resources