Java上传文件到ftp文件为0k,求帮忙!

文件夹能创建,但是图片文件为0kb.给ftp用户root权限还是一样!

/** 
     * Description: 向FTP服务器上传文件 
     * @param host FTP服务器hostname 
     * @param port FTP服务器端口 
     * @param username FTP登录账号 
     * @param password FTP登录密码 
     * @param basePath FTP服务器基础目录
     * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
     * @param filename 上传到FTP服务器上的文件名 
     * @param input 输入流 
     * @return 成功返回true,否则返回false 
     */  
    public static boolean uploadFile(String host, int port, String username, String password, String basePath,
            String filePath, String filename, InputStream input) {
        boolean result = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(host, port);// 连接FTP服务器
            // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
            ftp.login(username, password);// 登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            //切换到上传目录
            if (!ftp.changeWorkingDirectory(basePath+filePath)) {
                //如果目录不存在创建目录
                String[] dirs = filePath.split("/");
                String tempPath = basePath;
                for (String dir : dirs) {
                    if (null == dir || "".equals(dir)) continue;
                    tempPath += "/" + dir;
                    if (!ftp.changeWorkingDirectory(tempPath)) {
                        if (!ftp.makeDirectory(tempPath)) {
                            return result;
                        } else {
                            ftp.changeWorkingDirectory(tempPath);
                        }
                    }
                }
            }
            //设置上传文件的类型为二进制类型
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            //上传文件
            if (!ftp.storeFile(filename, input)) {
                return result;
            }
            input.close();
            ftp.logout();
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }
public static void main(String[] args) {
        try {  
            FileInputStream in=new FileInputStream(new File("D:\\图片\\dads.jpg"));  
            boolean flag = uploadFile("10.10.66.230", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2017/01/20", "gaigeming.jpg", in);  
            System.out.println(flag);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  
    }
阅读 6.5k
3 个回答

代码本身没有问题 你试一下使用FTP工具可以不?或者完全关闭防火墙试一下
service iptables stop

/**

  • 日志
    */

private static final Log log = LogFactory.getLog(ftcClients.class);
//FTC连接地址
public static final String URL=ApplicationPropertiesHolder.getProperty("docInterface.ftp.url");
//FTC连接端口
public static final int PORT=Integer.parseInt(ApplicationPropertiesHolder.getProperty("docInterface.ftp.post"));
//FTC账号
public static final String USERNAME=ApplicationPropertiesHolder.getProperty("docInterface.ftp.userName");
//FTC密码
public static final String PASSWORD=ApplicationPropertiesHolder.getProperty("docInterface.ftp.passWord");
//图片存放文件夹
public static final String IMAGESRC=ApplicationPropertiesHolder.getProperty("docInterface.ftp.directoryString");

public static List<String> SubmitPost(Map<String,String> imges){

 List<String> patch=new ArrayList<String>();
    FTPClient ftp = new FTPClient();               
    try {              
                   ftp.connect(URL, PORT);  //连接
                   
                   boolean isLogin = ftp.login(USERNAME, PASSWORD);  //登陆
                     
                   System.out.println("fcp登录:"+isLogin);  
                    
                   ftp.setFileType(FTPClient.BINARY_FILE_TYPE);  //设置传输文件类型
                   
                  // boolean chang= ftp.changeWorkingDirectory(IMAGESRC);//设置传输文件存放文件夹
                   
                   String pacthString=getpacth();
                   
                   if(!mkdirs(ftp,pacthString)){//创建目录
                       return patch;
                   }
                   ftp.setControlEncoding("utf-8");
                   ftp.enterLocalPassiveMode();//设置为主动模式                          
                   File file_in=null;
                   InputStream is=null;
                   BufferedInputStream inStream = null;
                  for (Map.Entry<String, String> imge : imges.entrySet()) { 
                       file_in = new File(imge.getValue());
                      log.info(imge.getKey()+"文件存在于否:"+file_in.exists()); 
                       is = new FileInputStream(file_in); 
                       inStream = new BufferedInputStream(is); 
                       boolean result = ftp.storeFile(new String(imge.getKey().getBytes("utf-8"),"iso-8859-1"), inStream);
                      log.info(imge.getKey()+"文件传输:"+result+"传输地址:"+pacthString+"/"+imge.getKey()); 
                       if(!result){
                           is.close();
                           patch.add(null);
                           return patch;
                       }else{
                           patch.add(pacthString+"/"+imge.getKey());
                       }      
                  }
                is.close();
                ftp.logout();
                return patch; 
              } catch (Exception e) {  
                      log.info(e.getMessage());
                   e.printStackTrace();  
                   return patch;
              }  
                 
            
       }          这是我写的测试通过的    不知道是不是编码导致你上次不上去了
       
       
       
       
       

1.测试一下用户有没有权限,ftp配置这部分,可以用ftp客户端登录操作一下,正常如果是防火墙拦截的话客户端这里也都无法登录操作的。
2.查看你的selinux是否关闭
/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态
关闭selinux
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
我就是这个原因客户端能正常登录操作但是java代码就无法操作。。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题