我需要发送一些帖子数据和文件流。我正在使用以下代码。
此代码取自 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 at 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 :(得分:0)
看起来很可疑,接收端没有将您正在写入的报头数据与有效负载数据分开。可能值得尝试以下方法: