Download 从远程服务器下载文件

Download 从远程服务器下载文件,download,text-files,Download,Text Files,我正在使用C#和一个控制台应用程序,我正在使用这个脚本从远程服务器下载文件。我想补充几点。首先,当它写入文件时,它不考虑换行符。这似乎运行了一定数量的字节,然后转到换行符。我想它保持相同的格式作为文件读取。其次,服务器上有多个.jpg文件需要下载。如何使用此脚本下载多个.jpg文件 public static int DownLoadFiles(String remoteUrl, String localFile) { int bytesProcessed = 0;

我正在使用C#和一个控制台应用程序,我正在使用这个脚本从远程服务器下载文件。我想补充几点。首先,当它写入文件时,它不考虑换行符。这似乎运行了一定数量的字节,然后转到换行符。我想它保持相同的格式作为文件读取。其次,服务器上有多个.jpg文件需要下载。如何使用此脚本下载多个.jpg文件

public static int DownLoadFiles(String remoteUrl, String localFile)
    {
        int bytesProcessed = 0;

        // Assign values to these objects here so that they can
        // be referenced in the finally block
        StreamReader remoteStream = null;
        StreamWriter localStream = null;
        WebResponse response = null;

        // Use a try/catch/finally block as both the WebRequest and Stream
        // classes throw exceptions upon error
        try
        {
            // Create a request for the specified remote file name
            WebRequest request = WebRequest.Create(remoteUrl);
            request.PreAuthenticate = true;
            NetworkCredential credentials = new NetworkCredential("id", "pass");
            request.Credentials = credentials;

            if (request != null)
            {
                // Send the request to the server and retrieve the
                // WebResponse object
                response = request.GetResponse();
                if (response != null)
                {
                    // Once the WebResponse object has been retrieved,
                    // get the stream object associated with the response's data
                    remoteStream = new StreamReader(response.GetResponseStream());

                    // Create the local file
                    localStream = new StreamWriter(File.Create(localFile));

                    // Allocate a 1k buffer
                    char[] buffer = new char[1024];
                    int bytesRead;

                    // Simple do/while loop to read from stream until
                    // no bytes are returned
                    do
                    {
                        // Read data (up to 1k) from the stream
                        bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

                        // Write the data to the local file
                        localStream.WriteLine(buffer, 0, bytesRead);

                        // Increment total bytes processed
                        bytesProcessed += bytesRead;
                    } while (bytesRead > 0);
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            // Close the response and streams objects here
            // to make sure they're closed even if an exception
            // is thrown at some point
            if (response != null) response.Close();
            if (remoteStream != null) remoteStream.Close();
            if (localStream != null) localStream.Close();
        }

        // Return total bytes processed to caller.
        return bytesProcessed;

结果文件中出现伪换行的原因是StreamWriter.WriteLine()将它们放在那里。尝试改用StreamWriter.Write()


关于下载多个文件,您能否只运行该函数几次,将所需不同文件的URL传递给它?

结果文件中出现伪换行的原因是StreamWriter.WriteLine()将它们放在了那里。尝试改用StreamWriter.Write()


关于下载多个文件,您能否只运行该功能几次,将所需不同文件的URL传递给它?

为什么不改用
WebClient.DownloadData
WebClient.downloaddfile

WebClient client = new WebClient();
client.Credentials = new NetworkCredentials("id", "pass");
client.DownloadFile(remoteUrl, localFile);
顺便说一下,将一个流复制到另一个流的正确方法不是您所做的。您根本不应该读入
char[]
,因为在下载二进制文件时可能会遇到编码和行尾问题。
WriteLine
方法调用也有问题。将流的内容复制到另一个流的正确方法是:

void CopyStream(Stream destination, Stream source) {
   int count;
   byte[] buffer = new byte[BUFFER_SIZE];
   while( (count = source.Read(buffer, 0, buffer.Length)) > 0)
       destination.Write(buffer, 0, count);
}

WebClient
类更易于使用,我建议改用它。

为什么不改用
WebClient.DownloadData
WebClient.DownloadFile

WebClient client = new WebClient();
client.Credentials = new NetworkCredentials("id", "pass");
client.DownloadFile(remoteUrl, localFile);
顺便说一下,将一个流复制到另一个流的正确方法不是您所做的。您根本不应该读入
char[]
,因为在下载二进制文件时可能会遇到编码和行尾问题。
WriteLine
方法调用也有问题。将流的内容复制到另一个流的正确方法是:

void CopyStream(Stream destination, Stream source) {
   int count;
   byte[] buffer = new byte[BUFFER_SIZE];
   while( (count = source.Read(buffer, 0, buffer.Length)) > 0)
       destination.Write(buffer, 0, count);
}

WebClient
类更易于使用,我建议改用它。

嘿,谢谢,我真的希望它停在换行符上。它现在不这样做。传入文件的唯一问题是,我不知道文件名,只知道它们是JPG。如果希望它在看到换行符时停止读取,则可能需要使用StreamReader.ReadLine()。然而,当您正在阅读JPG文件时,我不确定您为什么要读入这些文件,并用换行符将其写出。也许你应该看看BinaryWriter和BinaryReader?嘿,谢谢,我真的希望它停在一条新线上。它现在不这样做。传入文件的唯一问题是,我不知道文件名,只知道它们是JPG。如果希望它在看到换行符时停止读取,则可能需要使用StreamReader.ReadLine()。然而,当您正在阅读JPG文件时,我不确定您为什么要读入这些文件,并用换行符将其写出。也许你应该看看BinaryWriter和BinaryReader?是的,使用.Credentials属性。谢谢,我会试试的。如何在不知道文件名的情况下下载多个JPEG?下载文件方法是否考虑了换行符?它的格式类似于包含列和行的数据源文件?谢谢你不知道他们的文件名?!您如何在不知道URL的情况下手动下载文件!?我怀疑这是可能的。要下载多个文件,您可以通过循环或其他方式进行下载。要异步下载它们,可以使用DownloadDataAsync或类似方法。WebClient.DownloadFile将整齐地下载文件。关于新的线路或任何其他问题都不会有问题。是的,使用.Credentials属性。谢谢,我会试试。如何在不知道文件名的情况下下载多个JPEG?下载文件方法是否考虑了换行符?它的格式类似于包含列和行的数据源文件?谢谢你不知道他们的文件名?!您如何在不知道URL的情况下手动下载文件!?我怀疑这是可能的。要下载多个文件,您可以通过循环或其他方式进行下载。要异步下载它们,可以使用DownloadDataAsync或类似方法。WebClient.DownloadFile将整齐地下载文件。关于新行或任何其他相关内容都不会有问题。顺便说一句,作为旁注,您应该使用
using(streams=…)
语句,而不是手动编写try{}finally{}块,这可能容易出错,也很难编写。顺便说一句,您应该使用
using(streams=…)
语句(streams=…)语句,而不是手动编写try{}finally{}块,这可能容易出错,而且编写起来很麻烦。