我需要将大型文件(最多2GB)从我的.NET Web应用程序上传到java Web服务,而后者又会使用.jar文件。 java webservice API接受MultipartFormDataContent作为其参数。
我面临的问题是,我无法将整个2GB加载到字节数组中,因为它会抛出" SystemOutOfMemoryException"当我尝试上传任何大于300MB的文件时。
我也试过BufferedReader,StreamWriter但是徒劳无功。 我提供了以下代码供您参考:
public bool SendMessage(Dictionary<string, byte[]> files, string fromAddress, string toAddresses, string ccAddresses, string subject, string body)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
Dictionary<string, long> fileSizes = new Dictionary<string, long>();
Dictionary<string, ByteArrayContent> fileContent = new Dictionary<string, ByteArrayContent>();
HttpContent fileSizesContent = null;
try
{
HttpContent messageContent = new StringContent(jss.Serialize(new
{
to = toAddress,
cc = ccAddresses,
subject = subject,
body = "Test"
}));
if (files != null)
{
foreach (var entry in files)
{
fileSizes.Add(entry.Key, entry.Value.Length);
fileContent.Add(entry.Key, new ByteArrayContent(entry.Value));
}
fileSizesContent = new StringContent(jss.Serialize(fileSizes));
}
using (var client = new HttpClient())
{
using (var formData = new MultipartFormDataContent())
{
if (fileContent.Count > 0)
{
foreach (var entry in fileContent)
{
formData.Add(entry.Value, "attachments", entry.Key);
}
formData.Add(fileSizesContent, "fileSizes");
}
formData.Add(messageContent, "message");
var response = client.PostAsync(<java web service url>, formData).Result;
if (!response.IsSuccessStatusCode)
{
return false;
}
return true;
}
}
}
catch (Exception ex)
{
return false;
}
}
问题是:我无法对参数ByteArrayContent进行操作,因为它会为文件&gt; 300MB抛出SystemOutOfMemoeyException。
请帮帮我。
感谢。