Web服务可以返回流吗? [英] Can a web service return a stream?

查看:67
本文介绍了Web服务可以返回流吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一个小应用程序,该程序可以使人们上传&下载文件给我。我已经在该应用程序中添加了一个Web服务,以这种方式提供上传/下载功能,但是我不太确定我的实现将如何处理大文件。

I've been writing a little application that will let people upload & download files to me. I've added a web service to this applciation to provide the upload/download functionality that way but I'm not too sure on how well my implementation is going to cope with large files.

目前,上载&下载方法如下(使用Apache CXF编写):

At the moment the definitions of the upload & download methods look like this (written using Apache CXF):

boolean uploadFile(@WebParam(name = "username") String username,
    @WebParam(name = "password") String password,
    @WebParam(name = "filename") String filename,
    @WebParam(name = "fileContents") byte[] fileContents)
    throws UploadException, LoginException;

byte[] downloadFile(@WebParam(name = "username") String username,
    @WebParam(name = "password") String password,
    @WebParam(name = "filename") String filename) throws DownloadException,
    LoginException;

因此文件被上传并下载为字节数组。但是,如果我有一个愚蠢的文件大小(例如1GB),那肯定会尝试将所有信息放入内存中并使服务崩溃。

So the file gets uploaded and downloaded as a byte array. But if I have a file of some stupid size (e.g. 1GB) surely this will try and put all that information into memory and crash my service.

所以我的问题是-是否可以返回某种流?我想这将不会完全独立于操作系统。尽管我了解Web服务背后的理论,但实际方面还是我需要了解的一些信息。

So my question is - is it possible to return some kind of stream instead? I would imagine this isn't going to be terribly OS independent though. Although I know the theory behind web services, the practical side is something that I still need to pick up a bit of information on.

为任何输入欢呼,
Lee

Cheers for any input, Lee

推荐答案

Stephen Denne 具有满足您要求的Metro实现。在简短解释一下为什么会出现这种情况后,下面提供了我的答案。

Stephen Denne has a Metro implementation that satisfies your requirement. My answer is provided below after a short explination as to why that is the case.

大多数使用HTTP作为消息协议构建的Web Service实现都符合REST,它们只允许使用简单的收发模式,仅此而已。这将极大地提高互操作性,因为所有各种平台都可以理解这种简单的体系结构(例如,与.NET Web服务通信的Java Web服务)。

Most Web Service implementations that are built using HTTP as the message protocol are REST compliant, in that they only allow simple send-receive patterns and nothing more. This greatly improves interoperability, as all the various platforms can understand this simple architecture (for instance a Java web service talking to a .NET web service).

保持这一点,您可以提供分块。

If you want to maintain this you could provide chunking.

boolean uploadFile(String username, String password, String fileName, int currentChunk, int totalChunks, byte[] chunk);

在未按正确顺序获得块的情况下(或您可以只要求这些块以正确的顺序排列),但是它可能很容易实现。

This would require some footwork in cases where you don't get the chunks in the right order (Or you can just require the chunks come in the right order), but it would probably be pretty easy to implement.

这篇关于Web服务可以返回流吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆