我正在尝试在java FTPServer上传文件。
文件传输正常,但所有上传的文件都有额外的6ko数据。
我使用嵌入式方法在应用程序所需的EJB服务bean中启动FTPServer
以下是我使用的代码:
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;
}
}
一个简单的客户:
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();
}
}
}
我真的不明白为什么transfert数据大小是错误的。
服务器的跟踪日志也没有错误。
11:52:28,548 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter]已创建 11:52:28,549 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter]已打开 11:52:28,550 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT:220为新用户准备好服务。
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用户名好,需要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]登录成功 - jboss 11:52:28,561 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT:230用户已登录,继续。
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好。
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]上传的文件/ bug sheep 2.png 11:52:28,706 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT:150文件状态正常;即将开放数据连接。
11:52:28,706 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT:226传输完成。
感谢您的帮助
答案 0 :(得分:2)
我找到了解决方案。我必须手动将传输类型设置为二进制模式。
ftp.setFileType(FTP.BINARY_FILE_TYPE);