java FTPServerでファイルをアップロードしようとしています。Java apache FTPServerとcommon.net FTPClient
ファイル転送はOKですが、アップロードされたすべてのファイルには余分な6koデータがあります。
私はアプリケーションここ
のために必要なEJBサービス豆で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:28548 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] CREATED 11:52:28549 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter] OPENED 11:52: 28,550情報[org.apache.ftpserver.listener.nio.FtpLoggingFilter]送信:220新しいユーザーのためのサービス準備完了。
11:52:28552 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter]受付: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] SENT:230ユーザーがログインしました。
11:52:28562 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter]受付:PORT 127,0,0,1,12,190 11:52:28563 INFO [org.apache.ftpserver.listener .nio.FtpLoggingFilter] SENT:200コマンドポートは大丈夫です。
11:52:28563 INFO [org.apache.ftpserver.listener.nio.FtpLoggingFilter]受付:STORバグヒツジ2.png 11:52:28705 INFO [org.apache.ftpserver.command.impl.STOR ]ファイルのアップロード/バグの羊2.png 11:52:28,706情報[org.apache.ftpserver.listener.nio.FtpLoggingFilter]送信:150ファイルの状態は大丈夫です。データ接続を開こうとしています。
11:52:28,706情報[org.apache.ftpserver.listener.nio.FtpLoggingFilter]送信:226転送が完了しました。
ありがとうございました