1

我有一个在 Java 中运行的客户端应用程序,它将 HTTP REST 命令发送到 c# 中的 Web 服务。

客户端从服务器下载文件,效果很好。

这是我的文件传输代码:

Java 客户端

public void downloadFile(String fileUrl, String fileName, String filePath) throws IOException{
        int bytesRead;
        URL url = new URL(fileUrl);

        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] fileBuffer = new byte[1024];
        while ((bytesRead = bis.read(fileBuffer)) != -1) {
            bos.write(fileBuffer, 0, bytesRead);
        }
        bos.close();
        bis.close();
    }

C# 网络服务

        [WebGet(UriTemplate = "SendFile/{clientID}/{fileName}")]
        public Stream sendFile(String clientID, String fileName)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add(HttpResponseHeader.Expires, DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'"));
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
            Stream ms = File.OpenRead(@jobRepPath + fileName.Replace("___", "."));
            return ms;
        }

现在,基于此,我想设置方法来恢复损坏下载的下载。

我试过这段代码:

Java 客户端

public void resumeBrokenDownload(String fileUrl, String fileName, String filePath, long fileLength) throws IOException{
        int bytesRead;
        URL url = new URL(fileUrl);

        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath, true));

        byte[] fileBuffer = new byte[1024];     
        while ((bytesRead = bis.read(fileBuffer)) != -1) {
            bos.write(fileBuffer, 0, bytesRead);
        }
        bos.close();
        bis.close();
    }

C# 网络服务

        [WebGet(UriTemplate = "ResumeDownload/{clientID}/{fileName}/{startByte}")]
        public Stream resumeDownload(String clientID, String fileName, String startByte)
        {
            Stream ms = File.OpenRead(@jobRepPath + fileName.Replace("___", "."));
            var memoryStream = new MemoryStream();
            ms.CopyTo(memoryStream);
            Byte[] byteFile = memoryStream.ToArray();

            MemoryStream stream = new MemoryStream();
            stream.Write(byteFile, Convert.ToInt32(startByte), byteFile.Length);

            WebOperationContext.Current.OutgoingResponse.Headers.Add(HttpResponseHeader.Expires, DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss 'GMT'"));
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
            return stream;
        }

在 REST URL 中传递的 startByte 值是客户端已经接收到的文件部分的长度。

但这似乎根本不起作用,我从服务器收到一个空流。

有没有人知道如何让这段代码工作,或者更好的方法?

4

1 回答 1

0

我终于解决了适应本教程中的示例的问题。

For those who are interested in my solution, I change the code of the c# web service :

        [WebGet(UriTemplate = "ResumeDownload/{clientID}/{fileName}/{startByte}")]
        public Stream resumeDownload(String clientID, String fileName, String startByte)
        {
            MemoryStream responseStream = new MemoryStream();
            Stream fileStream = File.Open(@jobRepPath + fileName.Replace("___", "."), FileMode.Open);

            fileStream.Seek(Convert.ToInt32(startByte), SeekOrigin.Begin);

            int length = (int)(fileStream.Length - Convert.ToInt32(startByte));
            byte[] buffer = new byte[length];
            fileStream.Read(buffer, 0, length);
            responseStream.Write(buffer, 0, length);

            fileStream.Close();
            responseStream.Position = 0;

            return responseStream; 
        }
于 2013-04-02T10:58:13.917 回答