我正在尝试在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();
}
}
}
我不太明白为什么传输的数据大小是错误的。
服务器的跟踪日志也没有错误。
11:52:28,548信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]已创建
11:52:28,549信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]已打开
11:52:28,550信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]发送:220服务已为新用户准备好。
11:52:28,552信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]收到:USER jboss
11:52:28,553信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]发送:331用户名还可以,jboss需要密码。
11:52:28,553信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]收到:通过 *
11:52:28,561信息[org.apache.ftpserver.command.impl.PASS]登录成功-jboss
11:52:28,561信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]发送:230用户登录,继续。
11:52:28,562信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]收到:端口127,0,0,1,12,190
11:52:28,563信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]发送:200命令端口。
11:52:28,563信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]收到:STOR bug绵羊2.png
11:52:28,705信息[org.apache.ftpserver.command.impl.STOR]文件上传/ bug绵羊2.png
11:52:28,706信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]发送:150文件状态正常;即将打开数据连接。
11:52:28,706信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]发送:226传输完成。
谢谢你的帮助
最佳答案
我找到了解决方案。我必须将传输类型手动设置为二进制模式。
ftp.setFileType(FTP.BINARY_FILE_TYPE);