FTP上传失败'底层连接已关闭:接收时发生意外错误。'

时间:2015-09-03 18:43:31

标签: c# .net ftpwebrequest

我正在尝试将文件上传到FTP ,并且直到今天才成功完成。在这里(在工作中)我从来没有遇到过问题,但在现场制作中我们遇到了零星的问题,但现在今天突然间它不会100%的时间工作而我我无法解决这个问题。

我已经尝试了几乎所有我能找到的东西。

  • 这不是网络服务。
  • 我为FtpWebRequest增加了Timeout和ReadWriteTimeout为-1。
  • 我已经设置了用于写入文件内容的流的WriteTimeout和ReadTimeout。
  • 我们已确保将出站规则设置为允许我们通过其他方式进行通信的端口,并确认我们可以使用来自同一台计算机/网络的FileZilla进行写入/读取。
  • 文件不大。它们最大为1MB,是简单的文本文件。

以下是我使用的方法。如果您需要更多信息,请与我们联系。

public static void FtpUpload(
    String username, 
    String password, 
    String address, 
    Int32 port, 
    Boolean usePassive, 
    String filePath)
{

    string ftpServerIP = String.Format("ftp://{0}", address);
    string ftpUserID = username;
    string ftpPassword = password;
    FileInfo fileInf = new FileInfo(filePath);

    try
    {

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP);

        request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.UseBinary = false;
        request.UsePassive = false;
        request.ContentLength = fileInf.Length;

        // The buffer size is set to 2kb
        int buffLength = 2048;
        byte[] buff = new byte[buffLength];
        int contentLen;

        FileStream fs = fileInf.OpenRead();

        Stream strm = request.GetRequestStream();

        contentLen = fs.Read(buff, 0, buffLength);

        while (contentLen != 0)
        {
            // Write Content from the file stream to the FTP Upload Stream
            strm.Write(buff, 0, contentLen);
            contentLen = fs.Read(buff, 0, buffLength);
        }

        strm.Close();
        fs.Close();

    }
    catch
    {
        throw;
    }
}

screenshot

编辑:

此代码snippit有效。请注意,当我更新此片段以使用块时,它会恢复失败。是否可以使用使用块?

{{1}}

1 个答案:

答案 0 :(得分:1)

对于任何偶然发现此问题的人,

http://www.experts-exchange.com/questions/28693905/FtpWebRequest-The-underlying-connection-as-closed-An-unexpected-error-has-occurred-on-a-receive.html

为我工作,您必须将以下设置为false

class HostComponent {
  @ViewChild(TestComponent ) component!: TestComponent ;
}

describe('TestComponent', () => {

  let component: TestComponent;
  let host: HostComponent;
  let TestServiceMock : TestService;
  
   beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
      ],
      declarations: [
        TestComponent,
      ],
      providers: [
        { provide: TestService, useClass: TestServiceMock }
      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA]
    }).compileComponents().then(() => {
     
      TestServiceMock = TestBed.get(TestService);
      fixture = TestBed.createComponent(HostComponent);
      host = fixture.componentInstance;
      component = host.component;
      fixture.detectChanges();
    });
  }));
  
  it('calls nested Asyc functions', async () => {
    responseMock= {
        'data' : [];
    } // some mock data
    
    spyOn(component,'fun1').and.callThrough();
    spyOn(component,'fun2').and.callThrough();
    spyOn(TestServiceMock, 'test2').and.returnValue(responseMock);
    const data: string[] = ['1f1f221', '1f1f222'];
    await component.test1(data);
    expect(await TestServiceMock.test2).toHaveBeenCalledWith(data);
    fixture.whenStable().then(() => {
        expect(component.fun1).toHaveBeenCalled(); // Error here : 'Unhandled Promis rejection:', 
                                           //'<toHaveBeenCalled> : Expected a spy, but got Function.

        expect(component.fun2).toHaveBeenCalled();
    });
  });
  

});

这解决了FTP上传在本地工作且一旦部署到UAT失败的问题