使用Ftpwebrequest将固定宽度的.PRN文件上传到Mainframe。每条记录被截断为80个字符

时间:2011-08-11 18:08:44

标签: .net encoding upload ftpwebrequest

我正在使用ftpwebrequest将一些文件上传到我们拥有的旧主机系统(MPE / IX)。

该文件是.prn文件,基本上有许多固定宽度且没有分隔符的记录。每条记录长176个字符,最后有一个CRLF。

如果选择usebinary = false作为ASCII文件上传,则会在主机上创建包含每条记录的文件,但每行/记录将被截断为80个字符。

如何指定字符数为176。

谢谢!

这是我正在使用的代码,就像我说的那样将文件传输到大型机,然后将电子邮件发送给相应的人员。

    Public Sub Main()
    Dim i As Integer = 1
    Dim credential As NetworkCredential

    Try
        EmailList = ReadEmailAddress()
        credential = New NetworkCredential("USERNAME", String.Empty)
        FileList.AddRange({"CAN04_30030_", "CAN04_34120_", "CSI01_30030_", "CSI01_34120_"})

        For Each FileName In FileList
            Dim File As FileInfo
            Dim Response As ArrayList

            Try
                File = New FileInfo(IO.Path.Combine(SourceFileFolder, FileName & Now.ToString("yyyyMMdd") & ".prn"))

                If File.Exists Then
                    Response = ftp.Upload(File, "ftp://SERVERNAME/FILENAME" & i, credential)
                    UploadResults.Add(Response(1))
                Else
                    UploadResults.Add("<font color=""red"">" & File.Name & " was not found in the folder " & SourceFileFolder & "</font>")
                End If

                If Not Response Is Nothing Then
                    If Response(0) = 226 Then ArchiveFile(File)
                End If

            Catch ex As Exception
                UploadResults.Add("<font color=""red"">" & ex.Message & " occurred while trying to upload the file " & File.Name & "</font>")
            Finally
                File = Nothing
                Response = Nothing
            End Try

            i += 1
        Next

        SendMail()
    Catch ex As Exception

    Finally
        credential = Nothing
    End Try


End Sub  


Public Function Upload(ByVal File As FileInfo, ByVal target As String, ByVal credential As NetworkCredential) As ArrayList
    Dim AList As New ArrayList

    Try
        request = DirectCast(WebRequest.Create(target), FtpWebRequest)

        With request
            .Method = WebRequestMethods.Ftp.UploadFile
            .UsePassive = True
            .Credentials = credential
            .KeepAlive = False
            .UseBinary = False
        End With

        Dim sourceStream As StreamReader = New StreamReader(File.FullName, True)
        Dim filecontents() As Byte = Encoding.ASCII.GetBytes(sourceStream.ReadToEnd)
        sourceStream.Close()
        request.ContentLength = filecontents.Length

        Dim stream As Stream = request.GetRequestStream
        stream.Write(filecontents, 0, filecontents.Length)
        stream.Flush()
        stream.Close()

        response = DirectCast(request.GetResponse, FtpWebResponse)

        Dim t() As String = Split(target, "/")

        Try
            AList.Add(response.StatusCode)
        Catch ex As Exception
            AList.Add(String.Empty)
        End Try


        If response.StatusCode = 226 Then
            AList.Add(File.Name & " (" & t(t.GetUpperBound(0)) & ") " & " was transferred to " & UCase(credential.UserName) & " [<i>" & Split(response.StatusDescription, ".")(0) & "</i>]")
        Else
            AList.Add(File.Name & " (" & t(t.GetUpperBound(0)) & ") " & " was NOT transferred to " & UCase(credential.UserName) & " [<i>" & Split(response.StatusDescription, ".")(0) & "</i>]")
        End If

    Catch ex As Exception
        AList.Add(File.Name & " was NOT transferred.  " & ex.Message.ToString)
    Finally
        response.Close()
        request = Nothing
        response = Nothing
        File = Nothing
    End Try
    Return AList
End Function

1 个答案:

答案 0 :(得分:1)

看起来您只需将;rec=-176附加到文件名的末尾即可。尝试这样的事情:

Response = ftp.Upload(File, "ftp://SERVERNAME/FILENAME" & i & ";rec=-176", credential)