我想上傳一個Java FTPServer上的文件。Java apache FTPServer和common.net FTPClient
文件轉移是可以的,但所有上傳的文件都有額外的6ko數據。
我使用嵌入式方法來啓動的FTPServer在應用程序所需的
這裏的EJB服務bean是我使用的代碼:
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:28548 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] CREATED 11:52:28549 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] OPENED 11時52: 28,550 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT:220爲新用戶準備的服務。
11:52:28552 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] RECEIVED:USER的JBoss 11:52:28553 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT:331用戶名好,需要密碼爲jboss。
11:52:28553 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] RECEIVED:PASS * 11:52:28561 INFO [org.apache.ftpserver.command.impl.PASS]登錄成功 - jboss 11:52:28,561信息[org.apache.ftpserver.listener.nio.FtpLoggingFilter]發送:230用戶登錄,繼續。
11:52:28562 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] RECEIVED:PORT 127,0,0,1,12,190 11:52:28563 INFO [org.apache.ftpserver.listener .nio.FtpLoggingFilter] SENT:200 Command PORT okay。
11:52:28563 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] RECEIVED:STOR錯誤羊2.png 11:52:28705 INFO [org.apache.ftpserver.command.impl.STOR ] File uploaded/bug sheep 2.png 11:52:28,706 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT:150 File status okay;即將打開數據連接。
11:52:28,706 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] SENT:226傳輸完成。
感謝您的幫助