在执行多部分表单数据请求时,流意外结束

时间:2016-04-17 16:22:40

标签: java c# rest ms-office vsto

所以我们有一个用java编写的服务器应用程序,它有一个休息服务,您可以以多部分请求的形式上传文件。这是方法的签名

@POST
@Transactional(TxType.REQUIRED)
@Path("/{id}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public ContentInfo uploadToInstanceFromMultipart(UploadRequest uploadRequest, @PathParam(JsonKeys.ID) String id) {

正如你所看到的,没有什么特别之处,只是一个邮政服务。但另一方面,我们正在编写一个ms office插件(VSTO),它使用RestSharp将文件发送到该服务。

  void Application_DocumentBeforeSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
        var client = new RestClient("...");
        client.Authenticator = new HttpBasicAuthenticator(...);
        var request = new RestRequest("api/content/{id}", Method.POST);
        var name = Doc.Name;
        var contentId = getContentId(name);
        var fileName = getName(name);

        request.AddUrlSegment("id", contentId); 
        request.AddFile(fileN, Doc.Path + "\\" + Doc.Name, "application/msword");

        IRestResponse response = client.Execute(request);
        var content = response.Content;
        MessageBox.Show(content);

我在这里也看不到任何特别的内容,但在保存文件时(因此将其发送到服务器),服务器会响应以下异常

(default task-33) Generic exception: javax.ws.rs.WebApplicationException: Processing of multipart/form-data request failed. Stream ended unexpectedly

归结为

Caused by: org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:1005)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:903)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at org.apache.commons.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:134)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:100)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:70)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:347)
... 70 more

请问有什么想法吗?

0 个答案:

没有答案