主题:请大家帮我看看
接触.net不久,最近写了了文件操作类,但发现在上传大文件时,内存消耗很大,而且上传完成后,内存释放不了,尝试了很多方法未过,现把代码发上来,请有经验的同志指点一下,先谢谢了
'***************文件操作类
Public Class FileOperate
Public CurrentFile As FileInfo = Nothing
Private BufferSize As Integer = 64 * ConstGather.Kbyte
Private BufferRead As BufferedStream = Nothing
Private BufferWrite As BufferedStream = Nothing
Private FileReader As FileStream = Nothing
Private FileWriter As FileStream = Nothing
Private Overwrite As Boolean = False
Public Sub New(ByVal FullPath As String)
Me.CurrentFile = New FileInfo(FullPath)
End Sub
'上传文件 1 成功 2 上传文件长度为0 3 文件已经存在 4 未指定文件保存位置或文件名
Public Function UpLoad(ByVal SrcStream As Stream) As Integer
Dim Status As Integer = 0 '上传结果标志
Dim ResidualLength As Long = 0 '上传剩余字节数
Dim ContentArray(Me.BufferSize) As Byte
If (SrcStream.Length <= 0) Then
Status = 2
Return Status
Exit Function
Else
ResidualLength = SrcStream.Length
End If
If (Me.CurrentFile Is Nothing) Then
Status = 4
Return Status
Exit Function
End If
Try
Me.FileWriter = Me.CurrentFile.Open(FileMode.Create, FileAccess.Write)
Catch ex As Exception
Log.SaveLog(ex.Message)
Try
Me.FileWriter.Close()
Catch ex1 As Exception
Log.SaveLog(ex1.Message)
End Try
Status = 3
Return Status
Exit Function
End Try
While ResidualLength >= Me.BufferSize
SrcStream.Read(ContentArray, 0, Me.BufferSize)
Me.FileWriter.Write(ContentArray, 0, Me.BufferSize)
SrcStream.Flush()
Me.FileWriter.Flush()
ResidualLength -= Me.BufferSize
End While
If (ResidualLength > 0) Then
SrcStream.Read(ContentArray, 0, ResidualLength)
Me.FileWriter.Write(ContentArray, 0, ResidualLength)
SrcStream.Flush()
Me.FileWriter.Flush()
End If
SrcStream.Close()
Me.FileWriter.Close()
Status = 1
Return Status
End Function
End Class
'上传页面文件upfile.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f1 As FileOperate = New FileOperate("d:\site\filemanage.net\up.txt")
f1.UpLoad(Me.File1.PostedFile.InputStream)
Me.File1.PostedFile.InputStream.Close()
Me.File1.Dispose()
End Sub
'***************文件操作类
Public Class FileOperate
Public CurrentFile As FileInfo = Nothing
Private BufferSize As Integer = 64 * ConstGather.Kbyte
Private BufferRead As BufferedStream = Nothing
Private BufferWrite As BufferedStream = Nothing
Private FileReader As FileStream = Nothing
Private FileWriter As FileStream = Nothing
Private Overwrite As Boolean = False
Public Sub New(ByVal FullPath As String)
Me.CurrentFile = New FileInfo(FullPath)
End Sub
'上传文件 1 成功 2 上传文件长度为0 3 文件已经存在 4 未指定文件保存位置或文件名
Public Function UpLoad(ByVal SrcStream As Stream) As Integer
Dim Status As Integer = 0 '上传结果标志
Dim ResidualLength As Long = 0 '上传剩余字节数
Dim ContentArray(Me.BufferSize) As Byte
If (SrcStream.Length <= 0) Then
Status = 2
Return Status
Exit Function
Else
ResidualLength = SrcStream.Length
End If
If (Me.CurrentFile Is Nothing) Then
Status = 4
Return Status
Exit Function
End If
Try
Me.FileWriter = Me.CurrentFile.Open(FileMode.Create, FileAccess.Write)
Catch ex As Exception
Log.SaveLog(ex.Message)
Try
Me.FileWriter.Close()
Catch ex1 As Exception
Log.SaveLog(ex1.Message)
End Try
Status = 3
Return Status
Exit Function
End Try
While ResidualLength >= Me.BufferSize
SrcStream.Read(ContentArray, 0, Me.BufferSize)
Me.FileWriter.Write(ContentArray, 0, Me.BufferSize)
SrcStream.Flush()
Me.FileWriter.Flush()
ResidualLength -= Me.BufferSize
End While
If (ResidualLength > 0) Then
SrcStream.Read(ContentArray, 0, ResidualLength)
Me.FileWriter.Write(ContentArray, 0, ResidualLength)
SrcStream.Flush()
Me.FileWriter.Flush()
End If
SrcStream.Close()
Me.FileWriter.Close()
Status = 1
Return Status
End Function
End Class
'上传页面文件upfile.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f1 As FileOperate = New FileOperate("d:\site\filemanage.net\up.txt")
f1.UpLoad(Me.File1.PostedFile.InputStream)
Me.File1.PostedFile.InputStream.Close()
Me.File1.Dispose()
End Sub