본문 바로가기

Visual Std 2010

Visual Studio 에서 FTP 연결을 통한 업로드,다운로드 소스참고

이벤트에 다음과 같이 소스를 작성해서 실행하면 됩니다~

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            '다운로드할 ftp주소

            Dim u As New Uri(System.IO.Path.Combine("ftp://127.0.0.0/aa/bb/cc/", fileName))


            Dim u As New Uri(System.IO.Path.Combine("ftp://127.0.0.0/aa/bb/cc/", "불량코드.txt"))

 

            'c:에 다운로드
            'Dim downFile As String = System.IO.Path.Combine("D:\Project\FileToDB\File\", fileName)
            Dim downFile As String = System.IO.Path.Combine("C:\Users\hhhwang\Desktop", "불량코드.txt")

 

            '리퀘스트작성

            Dim ftpReq As System.Net.FtpWebRequest = CType(System.Net.WebRequest.Create(u), System.Net.FtpWebRequest)

 

            '아이디,비번입력

            ftpReq.Credentials = New System.Net.NetworkCredential("아이디", "비번")

 

            '리퀘스트메소드에 다운로드
            ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
            ftpReq.KeepAlive = False
            ftpReq.UseBinary = False
            ftpReq.UsePassive = False

 

            '리스폰스취득

            Dim ftpRes As System.Net.FtpWebResponse = CType(ftpReq.GetResponse(), System.Net.FtpWebResponse)

 

            '화일 다운로드하기위에 스트림취득
            Dim resStrm As System.IO.Stream = ftpRes.GetResponseStream()

 

            '다운로드할 화일을 엽니다.
            Dim fs As System.IO.FileStream = New System.IO.FileStream(downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write)

 

            '전송

            Dim buffer(1024) As Byte


            While (True)
                Dim readSize As Integer = resStrm.Read(buffer, 0, buffer.Length)
                If (readSize = 0) Then
                    Exit While
                End If

                fs.Write(buffer, 0, readSize)
            End While

 

            fs.Close()
            resStrm.Close()
        Catch ex As Exception
            'Throw New Exception(ex.Message)
            System.Windows.Forms.MessageBox.Show(ex.ToString())
        End Try

 


    End Sub