【问题标题】:Restlet multpart form file upload fails with UnknownSizeExceptionRestlet 多部分表单文件上传失败,出现未知大小异常
【发布时间】:2015-11-03 04:19:38
【问题描述】:

我正在尝试使用restlet客户端模拟带有附件和一些参数的表单提交。从浏览器提交的服务器工作正常,但问题是尝试使用 restlet 上传时失败。

上传代码是。为了便于阅读,我删除了验证和异常处理。

ClientResource cr = new ClientResource(targetUrl);
cr.setCookies(cookie);
FormDataSet form = new FormDataSet();
form.getEntries().add(new FormData("title", request.getTitle()));

File f = new File(request.getFilePath());
FileRepresentation file = new FileRepresentation(f,MediaType.MULTIPART_FORM_DATA);
FormData fd = new FormData("upload_file", file); 
form.getEntries().add(fd);
form.setMultipart(true);

Representation responseObject = cr.post(form); 

但服务器抱怨请求中缺少内容大小。

org.apache.commons.fileupload.FileUploadBase$UnknownSizeException: 请求被拒绝,因为它的大小未知

我在上面的代码中做错了什么。 仅供参考,服务器也是使用 restlet 编写的,并遵循代码示例 here

我发现了一个 restlet 博客,它有一些替代方法 here。但他们指的是一个类“MultipartRepresentation”,我无法在restlet分布中找到它。在这里我可能丢失了一个扩展 jar 文件,但我仍然找不到。

如有任何帮助,将不胜感激

【问题讨论】:

    标签: rest file-upload client multipartform-data restlet


    【解决方案1】:

    我在 Restlet 2.3.1 中使用您的代码进行了测试,但无法重现您的问题。

    这是它产生的请求的内容:

    POST /upload HTTP/1.1
    Accept-Encoding: gzip
    User-Agent: Jetty/9.2.6.v20141205
    Date: Thu, 05 Nov 2015 15:48:31 GMT
    Content-Type: multipart/form-data; boundary=---Aa1Bb2Cc3---
    Accept: */*
    Host: localhost:8182
    User-Agent: Restlet-Framework/2.3.1
    Content-Length: 248
    
    -----Aa1Bb2Cc3---
    Content-Disposition: form-data; name="title"
    
    request.getTitle()
    -----Aa1Bb2Cc3---
    Content-Disposition: form-data; name="upload_file"; filename="test.txt"
    Content-Type: multipart/form-data
    
    some text
    -----Aa1Bb2Cc3-----
    

    这是我的测试项目中的依赖项:

    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet</artifactId>
        <version>${restlet-version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet.ext.jetty</artifactId>
        <version>${restlet-version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet.ext.fileupload</artifactId>
        <version>${restlet-version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.restlet.jse</groupId>
        <artifactId>org.restlet.ext.html</artifactId>
        <version>${restlet-version}</version>
    </dependency>
    

    您能否详细说明您使用的确切依赖项以及发送请求的内容?

    我猜它可以链接到您使用的客户端扩展(默认的?)。就我而言,我使用了 Jetty 扩展中的那个...

    谢谢! 蒂埃里

    【讨论】:

      猜你喜欢
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 2022-08-24
      • 2020-04-10
      相关资源
      最近更新 更多