需求,每晚上传1500张jpg图片,下面的代码多次打开和关闭一个连接,想知道有没有更好的方法。
...这是一个代码片段,所以这里有在别处定义的变量
Dim picClsRequest = DirectCast(System.Net.WebRequest.Create(ftpImagePath), System.Net.FtpWebRequest)
Dim picClsStream As System.IO.Stream
Dim picCount As Integer = 0
For i = 1 To picPath.Count - 1
picCount = picCount + 1
log("Sending picture (" & picCount & " of " & picPath.Count & "):" & picDir & "/" & picPath(i))
picClsRequest = DirectCast(System.Net.WebRequest.Create(ftpImagePath & "/" & picPath(i)), System.Net.FtpWebRequest)
picClsRequest.Credentials = New System.Net.NetworkCredential(ftpUsername, ftpPassword)
picClsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
picClsRequest.UseBinary = True
picClsStream = picClsRequest.GetRequestStream()
bFile = System.IO.File.ReadAllBytes(picDir & "/" & picPath(i))
picClsStream.Write(bFile, 0, bFile.Length)
picClsStream.Close()
Next
一些评论:
是的,我知道 picCount 是多余的……那是深夜。
ftpImagePath, picDir, ftpUsername, ftpPassword 都是变量
是的,这是未加密的
此代码工作正常,我正在寻找优化