2012-04-27 94 views
0

我需要发送一些文章数据以及文件流。我使用下面的代码。发送表单数据以及用于MPP文件的内容类型“application/vnd.ms-project”的文件数据

此代码取自 http://technet.rapaport.com/Info/LotUpload/SampleCode/Full_Example.aspx

private Stream GetPostStream(string filePath, Dictionary<string, string> paramMap, string boundary) { 

     Stream postDataStream = new System.IO.MemoryStream(); 

     //adding form data 
     string formDataHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine + 
      "Content-Disposition: form-data; name=\"{0}\";" + Environment.NewLine + Environment.NewLine + "{1}"; 
     foreach (KeyValuePair<string, string> pair in paramMap) 
     { 
      byte[] formItemBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(formDataHeaderTemplate, pair.Key, pair.Value)); 
      postDataStream.Write(formItemBytes, 0, formItemBytes.Length); 
     } 

     //adding file data 
     FileInfo fileInfo = new FileInfo(filePath); 

     string fileHeaderTemplate = Environment.NewLine + "--" + boundary + Environment.NewLine + 
     "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + 
     Environment.NewLine + "Content-Type: application/vnd.ms-project" + Environment.NewLine + Environment.NewLine; 

     byte[] fileHeaderBytes = System.Text.Encoding.UTF8.GetBytes(string.Format(fileHeaderTemplate, "UploadMPPFile", fileInfo.FullName)); 

     postDataStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length); 

     FileStream fileStream = fileInfo.OpenRead(); 

     byte[] buffer = new byte[1024]; 

     int bytesRead = 0; 

     while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) 
     { 
      postDataStream.Write(buffer, 0, bytesRead); 
     } 

     fileStream.Close(); 

     byte[] endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--"); 
     postDataStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); 

     return postDataStream; 
    } 

在JAVA上的服务器端,我使用MPXJ第三方库来读取文件数据。但是,我遇到以下异常。它报告标题签名中的一些不匹配错误。

嵌套的例外是:net.sf.mpxj.MPXJException:错误读取文件] 与根源java.io.IOException的:标题无效签名;阅读 0x2D2D2D2D2D2D0A0D,预计0xE11AB1A1E011CFD0在 org.apache.poi.poifs.storage.HeaderBlockReader。(HeaderBlockReader.java:125) 在 org.apache.poi.poifs.filesystem.POIFSFileSystem。(POIFSFileSystem.java:153) 在net.sf.mpxj.mpp.MPPReader.read(MPPReader.java:84)

任何人都可以请帮助我这种情况,并提出一些解决方案!

非常感谢。

回答

0

可疑地看起来像接收端没有将您写入流中的头数据从有效负载数据中分离出来。可能是值得尝试以下操作:

  1. 验证您正在阅读的MPP通过更换呼叫与本地文件流的写入postDataStream.Write文件正确。验证您创建的文件的内容与您正在阅读的文件是否相同。
  2. 将服务器端的接收代码替换为仅将接收到的数据回显到文件的内容,以便验证接收的内容。
  3. 更新现有的接收代码,将提取的标题数据和有效负载数据写入单独的文件,以验证两者是否正确接收,以及有效负载文件是否匹配从客户端发送的内容。
相关问题