Java apache FTPServer and common.net FTPClient

I am trying to upload a file to java FTPServer.

File transfer is fine, but all downloaded files have 6ko extra data.

I use the built-in method to start FTPServer in the EJB bean service required for the application

Here is the code I'm using:

package com.cs3Drender.ftpservice; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.ejb.Local; import org.apache.ftpserver.FtpServer; import org.apache.ftpserver.FtpServerFactory; import org.apache.ftpserver.ftplet.Authority; import org.apache.ftpserver.ftplet.FtpException; import org.apache.ftpserver.ftplet.UserManager; import org.apache.ftpserver.listener.ListenerFactory; import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor; import org.apache.ftpserver.usermanager.impl.BaseUser; import org.apache.ftpserver.usermanager.impl.WritePermission; import org.jboss.ejb3.annotation.Service; /** * Session Bean implementation class FtpServerServiceLocal */ @Service @Local(FtpServerServiceLocal.class) public class FtpServerService implements FtpServerServiceLocal { private FtpServer server = null; @Override public void create() throws Exception { FtpServerFactory serverFactory = new FtpServerFactory(); ListenerFactory factory = new ListenerFactory(); // set the port of the listener factory.setPort(2221); // replace the default listener serverFactory.addListener("default", factory.createListener()); // create new user serverFactory.setUserManager(addUser("jboss", "azerty", "c:/tools")); // start the server server = serverFactory.createServer(); } @Override public void start() throws Exception { try { server.start(); } catch (FtpException e) { e.printStackTrace(); } } public void restart() throws Exception { try { server.stop(); server.start(); } catch (FtpException e) { e.printStackTrace(); } } @Override public void stop() { try { server.stop(); } catch (Exception e) { e.printStackTrace(); } } @Override public void destroy() { stop(); server = null; } public UserManager addUser(final String username, final String password, final String ftproot) { PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); userManagerFactory.setFile(new File("c:/jbossusers.properties")); userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor()); UserManager um = userManagerFactory.createUserManager(); BaseUser user = new BaseUser(); user.setName(username); user.setPassword(password); user.setHomeDirectory(ftproot); List<Authority> authorities = new ArrayList<Authority>(); authorities.add(new WritePermission()); user.setAuthorities(authorities); try { um.save(user); } catch (FtpException e) { e.printStackTrace(); } return um; } } 

And a simple client:

 package client; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.SocketException; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import com.cs3Drender.session.RenderSessionRemote; public class Client { public static void main(String[] args) { /* get a initial context. By default the settings in the file * jndi.properties are used. * You can explicitly set up properties instead of using the file. */ Properties properties = new Properties(); properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); properties.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces"); properties.put("java.naming.provider.url","localhost"); try { InitialContext context= new InitialContext(properties); RenderSessionRemote beanRemote=(RenderSessionRemote)context.lookup("RenderSession/remote"); //System.out.println(beanRemote.render("test")); try { FTPClient ftp = new FTPClient(); ftp.connect("localhost", 2221); ftp.login("jboss", "azerty"); //ftp.enterLocalPassiveMode(); //ftp.enterRemotePassiveMode(); ftp.setAutodetectUTF8(true); String filename = "bug sheep 2.png"; ftp.storeFile(filename, new FileInputStream("c:/"+filename)); ftp.logout(); ftp.disconnect(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }catch(NamingException e){ e.printStackTrace(); } } } 

I do not understand why the size of the transfer data is incorrect.

Also, the server trace log does not contain errors.

11: 52: 28,548 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] CREATED 11: 52: 28,549 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] OPEN 11: 52: 28,550 INFO [org.apache .ftpserver.listener.nio.FtpLoggingFilter] SENT: 220 Service is ready for a new user.

11: 52: 28,552 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] RECEIVED: USER jboss 11: 52: 28,553 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT: 331 Username okay, required password for jboss.

11: 52: 28,553 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] RECEIVED: PASS * 11: 52: 28,561 INFO [org.apache.ftpserver.command.impl.PASS] Login Success - jboss 11: 52: 28,561 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT: 230 The user is logged in, continue.

11: 52: 28,562 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] RECEIVED: PORT 127,0,0,1,12,190 11: 52: 28,563 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT: 200 Command PORT is fine.

11: 52: 28,563 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] RECEIVED: STOR bug sheep 2.png 11: 52: 28,705 INFO [org.apache.ftpserver.command.impl.STOR] Uploaded files / bug sheep 2.png 11: 52: 28,706 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT: 150 File status is OK; to open a data connection.

11: 52: 28,706 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT: 226 Transfer completed.

thanks for the help

+4
source share
1 answer

I have found a solution. I have to set the transfer type manually to binary mode.

 ftp.setFileType(FTP.BINARY_FILE_TYPE); 
+2
source

All Articles