2 msx985211 msx985211 于 2016.03.15 15:56 提问

Java上传到fth时提示Connection closed without indication.

如题,自己在系统中直接上传的话是可以的,但用代码上传就不行了,代码是这样的
package cn.com.sensetech.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class Upload
{
/**

  • Description: 向FTP服务器上传文件
  • @param url FTP服务器hostname
  • @param port FTP服务器端口
  • @param username FTP登录账号
  • @param password FTP登录密码
  • @param path FTP服务器保存目录
  • @param filename 上传到FTP服务器上的文件名
  • @param input 输入流
  • @return 成功返回true,否则返回false
    */
    public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try
    {
    int reply;
    ftp.connect(url, port);//连接FTP服务器
    //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
    ftp.login(username, password);//登录
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply))
    {
    ftp.disconnect();
    return success;
    }
    ftp.changeWorkingDirectory(path);
    ftp.storeFile(filename, input);

    input.close();
    ftp.logout();
    success = true;
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    finally
    {
    if (ftp.isConnected())
    {
    try
    {
    ftp.disconnect();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    return success;
    }

    public static void main(String[] args)
    {
    try
    {
    String filepath = "D:/upload/liuqian.txt";
    FileInputStream in = new FileInputStream(new File(filepath));
    boolean flag = uploadFile("localhost", 21, "lq", "123", "D:/upload", "walmartDemo", in);
    System.out.println(flag);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }

提示出错信息
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:400)
at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:924)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:183)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
at cn.com.sensetech.ftp.Upload.uploadFile(Upload.java:30)
at cn.com.sensetech.ftp.Upload.main(Upload.java:105)
false

调试的时候发现错误出现在这里
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
}

这究竟是怎么回事啊,怎么修改才能够跑通,本人新手,求各位大师帮忙,感激不尽

4个回答

u013772876
u013772876   2016.03.15 17:12
已采纳
msx985211
msx985211   2016.03.22 11:15

非常感谢,问题已解决,问题已经解决了

fanglj000604
fanglj000604   2017.05.13 09:53

请问是怎么解决的呢?

sigoudeng
sigoudeng   2017.06.22 12:56

调ftps的方法FTPSClient 来做= =我是这么解决的 应该是服务端用了SSL加密!!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
ftp Connection closed without indication.上传失败
org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication. 出现这个问题可能的情况 1你的端口号写错啦!!!!这个是我的情况 默认21= =狗日同事给我虚假信息 2可能启用了ssl= ======也就是ftps   这种情况要用FTPClient创建类并CONNE
FTPConnectionClosedException
API Docments 描述: public class FTPConnectionClosedExceptionextends IOException FTPConnectionClosedException is used to indicate the premature or unexpected closing of an FTP connection resulting
python爬虫过程报错:http.client.RemoteDisconnected: Remote end closed connection without response
爬虫过程报错:http.client.RemoteDisconnected: Remote end closed connection without response 因为服务器根据 UA 来判断拒绝了 python 爬虫。
Python3.6请求网站时报错:http.client.RemoteDisconnected: Remote end closed connection without response
调整前的代码段: data = urllib.request.urlopen(url).read() 运行时报错:     http.client.RemoteDisconnected: Remote end closed connection without response 调整后代码段: headers = {'User-Agent': 'User-
爬虫过程报错:http.client.RemoteDisconnected: Remote end closed connection without response
import urllib from urllib import request page = 1 url = 'http://www.qiushibaike.com/hot/page/'+str(page) #url = 'http://www.baidu.com' try: req = request.Request(url) response = request.urlopen(req)
python3 Connection aborted.', RemoteDisconnected('Remote end closed connection without response'
在写爬虫的时候遇到了问题,网站是asp.net写的 requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',)) 于是就抓包分析,发现只要加了’Accept-Language’就好了。。。 '...
Flask入门_第一天
本文摘自《Python Web 开发实践》董伟明著,用作学习笔记及知识分享。若有侵权行为,请及时联系,删除文章 从Hello World 开始      # coding=utf-8 from flask import Flask # Flask类实现了一个WSGI应用 app = Flask(__name__) # 确定程序的根目录,获取静态文件和模板文件的目录 @app.rout
(八)Jmeter常见问题---(学习笔记)
Jmeter常见问题 收集工作中JMeter遇到的各种问题   1.  JMeter的工作原理是什么?   向服务器提交请求;从服务器取回请求返回的结果。   2.  JMeter的作用?   JMeter可以用于测试静态或者动态资源的性能(文件、Servlets、Perl脚本、java对象、数据库和查询、ftp服务器或者其他的资源)。JMeter用于模拟在服务器、网
python爬取全书网小说
做一个爬取小说的爬虫,使用 requests 和 bs4 具体代码如下 # _*_ utf-8 _*_ import requests import random from bs4 import BeautifulSoup import re import time import datetime class Book_spider(): url = 'http://www.qua
XShell连不上本机虚拟机报:Connection closed by foreign host.
ssh联不上本机虚拟及 http://www.cnblogs.com/gylei/archive/2011/12/10/2283484.html