1

我使用 spring-ws 4.3.3,我想知道是否可以获得包含数据处理程序参数的肥皂请求的真实内容大小。

如果请求大小似乎小于 4096 字节,则以下代码运行良好,否则如果 content-size 大于 4096,则 requestSize 等于 -1。但是在javadoc中它是这样写的:

返回请求正文的长度(以字节为单位)并由 * 输入流提供,如果长度未知,则返回 -1 ir 大于 * Integer.MAX_VALUE

在我的示例中,如果肥皂请求超过 51200000,但如果我的请求大于 4096,则会出现错误,我会尝试生成错误消息。

TransportContext tc = TransportContextHolder.getTransportContext();
HttpServletConnection connection = (HttpServletConnection) tc.getConnection();
Integer requestSize = connection.getHttpServletRequest().getContentLength();
if (requestSize==-1 || requestSize > 51200000) {
    response.setStatus(getStatusResponse(PricingConstants.WS_FILE_SIZE_EXCEEDED_CODE, 
        PricingConstants.WS_FILE_SIZE_EXCEEDED_MSG));
return response;

XSD

    <xs:complexType name="wsAddDocumentRequest">
    <xs:sequence>
        <xs:element name="callingAspect">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="userId" type="xs:string" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="Id" type="xs:string" />
        <xs:element name="contentPath" type="xs:string" minOccurs="0" />
        <xs:element name="file" type="xs:base64Binary" minOccurs="0"
            xmime:expectedContentTypes="application/octet-stream" />
        <xs:element name="document" type="prc:document" />
        <xs:element name="authorizedUsers" minOccurs="0">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="user" type="xs:string" maxOccurs="unbounded" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

谢谢

4

2 回答 2

0

谢谢你安德烈亚斯打开我的心扉

我找到了解决方案

/**
 * Returns true if content size exceeds the max file size
 * 
 * @param dataHandler content
 * @param maxSize the max size allowed
 * @return true if contentSize greater than maxSize, false otherwise
 * @throws IOException
 */
public static boolean isTooBigContent(DataHandler dataHandler, long maxSize) throws IOException {
    long contentSize = 0;

    try (InputStream input = dataHandler.getInputStream()) {
        int n;
        while (IOUtils.EOF != (n = input.read(new byte[4096]))) {
            contentSize += n;
            if (Long.compare(contentSize, maxSize) > 0)
                return true;
        }
    }

    return false;
}
于 2017-09-19T15:49:42.907 回答
0

这种大小的请求通常使用 HTTP 分块,并且事先不知道内容长度,getContentLength()即将返回 -1。要禁止超过特定大小的请求,您可以配置您的应用程序服务器(如果它具有限制请求大小的选项)或安装一个 servlet 过滤器,该过滤器会在从请求中读取一定数量的字节后触发错误。

于 2017-08-28T11:02:26.747 回答