代码如下:
string ftpdir = FTPAddress;
string temofolder = Application.StartupPath + "\\Temp\\";
Parallel.ForEach(downloaduris,(downloaduri, state) =>
{
FileInfo fileInfo = new FileInfo(temofolder + downloaduri);
string sDir = fileInfo.DirectoryName;
if (!Directory.Exists(sDir))//文件夹不存在则创建
{
Directory.CreateDirectory(sDir);
}
try
{
FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(ftpdir + "//" + downloaduri);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
FileStream outputStream = new FileStream(temofolder + downloaduri, FileMode.Create);
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
Stream ftpStream = response.GetResponseStream();
int readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Flush();
ftpStream.Close();
outputStream.Flush();
outputStream.Close();
response.Close();
reqFTP.Abort();
}
catch (WebException webex)
{ //FTP上不存在该文件
state.Stop();
}
});
downloaduris 里面是200多张图片或TXT文本的相对路径。
在下载之前Temp文件夹是清过的。
在 FileStream outputStream = new FileStream(temofolder + downloaduri, FileMode.Create);
这句话经常报这样的错:
System.IO.IOException
"D:\PYProject\UpFile\Temp\Dog20180202\dog11.png"正由另一个进程使用,因此该进程无法访问此文件。
有没有大神解答一下