0

我正在从网络请求中保存图像,并且发生了一些非常奇怪的事情。在我下载的 8,000 张图像中,大约有一半出现 IOEXCEPTION 错误:ERROR_ACCESS_DENIED (5) INVALID_PARAMETER (87)

在使用 file.open 保存文件之前,我会检查以确保文件不存在。在这行代码中抛出异常:

fileStream = File.Open(destination, FileMode.Create, FileAccess.Write, FileShare.None);

下面是代码:

public static bool DownloadFile(string url, string destination) { bool success = false;

        System.Net.HttpWebRequest request = null;
        System.Net.WebResponse response = null;
        Stream responseStream = null;
        FileStream fileStream = null;

        try
        {
            request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.Method = "GET";
            request.Timeout = 100000; // 100 seconds
            request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy();
            response = request.GetResponse();

            responseStream = response.GetResponseStream();
            fileStream = File.Open(destination, FileMode.Create, FileAccess.Write, FileShare.None);
            //fileStream = File.Create(destination);


            // read up to ten kilobytes at a time
            int maxRead = 10240;
            byte[] buffer = new byte[maxRead];
            int bytesRead = 0;
            int totalBytesRead = 0;

            // loop until no data is returned
            while ((bytesRead = responseStream.Read(buffer, 0, maxRead)) > 0)
            {
                totalBytesRead += bytesRead;
                fileStream.Write(buffer, 0, bytesRead);
            }

            // we got to this point with no exception. Ok.
            success = true;
        }
        catch (System.Net.WebException we)
        {
            // something went terribly wrong.
            success = false;
            //MessageBox.Show(exp.ToString());
            writeErrFile(we.ToString(), url);
            //Debug.WriteLine(exp);
        }
        catch (System.IO.IOException ie)
        {
            // something went terribly wrong.
            success = false;
            //MessageBox.Show(ie.InnerException.ToString());
            writeErrFile(ie.ToString(), destination + " -- " + url);
            //Debug.WriteLine(exp);
        }
        catch (Exception exp)
        {
            // something went terribly wrong.
            success = false;
            //MessageBox.Show(exp.ToString());
            writeErrFile(exp.ToString(), destination + " -- " + url);
            //Debug.WriteLine(exp);
        }
        finally
        {
            // cleanup all potentially open streams.

            if (null != responseStream)
                responseStream.Close();
            if (null != response)
                response.Close();
            if (null != fileStream)
                fileStream.Close();

        }

        // if part of the file was written and the transfer failed, delete the partial file
        if (!success && File.Exists(destination))
            File.Delete(destination);

        return success;
    }

我已经坚持了几天了。任何帮助都将得到难以想象的数量级的赞赏。

4

1 回答 1

0

使用 file.exists() 检查文件是否存在,使用 file.create 或 file.openwrite 写入文件。

从您的代码中,我看不到您如何检查文件是否存在。

于 2009-12-24T19:20:38.880 回答