我已经浏览并按照以下方式将多个文件发布到 serverlet 页面,但未能做到这一点 https://stackoverflow.com/questions/18958488/uploading-multiple-files-from-aspx-to-servelet-page-through- http-post-method-is 我通过 http post 方法将多个文件从 .Net 应用程序发布到 Java Servelet 页面,我收到错误:N~java.io.IOException: Corrupt form data: noleading boundary: != - -----------------------------------------8d08693613639c3
Can any body please & please help me out:
my code is
public int postenrollments(string source, string posturl)
{
string[] enrolldirect;
int FindDirectoriesLength=System.IO.Directory.GetDirectories(source).Length ;
string SuccessUploadedFiles = ConfigurationManager.AppSettings.Get("UploadedEnrollmentsSource");
if (FindDirectoriesLength == 0)
{
status = 0;
}
else
{
enrolldirect = Directory.GetDirectories(source).ToArray();
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(posturl);
NameValueCollection nvc = new NameValueCollection();
nvc.Add("stTerminal", "000018112");
nvc.Add("stAgentID", "13220001");
nvc.Add("stvendorId", "112");
posturl += "?stTerminal=" + "000018112" + "&stAgentID=" + 13220001 + "&stvendorId=" + 112;
foreach (string dir in enrolldirect)
{
if (VerifyFiles(dir))
{
string[] enrollfiles = Directory.GetFiles(dir).ToArray();
UploadFile[] files = new UploadFile[enrollfiles.Length];
string[] filestoUpload = new string[enrollfiles.Length]; //This For PostMultipleFiles & UploadFilesToRemoteUrl Method
for (int i = 0; i < enrollfiles.Length; i++)
{
filestoUpload[i] = enrollfiles[i];//This For PostMultipleFiles & UploadFilesToRemoteUrl Method
files[i] = new UploadFile(enrollfiles[i]);
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(posturl);
HttpWebResponse resp = UploadFilesToRemoteUrl(posturl, filestoUpload, "", nvc);
StreamReader respreader = new StreamReader(resp.GetResponseStream());
**//Here in response I am getting the boundary exception**
string response = respreader.ReadToEnd();
}
}
return status;
}
用于发布文件的方法
public HttpWebResponse UploadFilesToRemoteUrl(string url, string[] files, string logpath, NameValueCollection nvc)
{
HttpWebResponse webResponse21;
long length = 0;
string boundary = "--------------"+DateTime.Now.Ticks.ToString("x");
HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest2.ContentType = "multipart/form-data; boundary=" +boundary;
httpWebRequest2.Method = "POST";
httpWebRequest2.KeepAlive = true;
httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream memStream = new System.IO.MemoryStream();
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +boundary + "\r\n");
string formdataTemplate = "\r\n--" + boundary +"\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
foreach (string key in nvc.Keys)
{
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
for (int i = 0; i < files.Length; i++)
{
string header = string.Format(headerTemplate, "file" + i, files[i]);
//string header = string.Format(headerTemplate, "uplTheFile", files[i]);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(files[i], FileMode.Open,FileAccess.Read);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
memStream.Write(boundarybytes, 0, boundarybytes.Length);
fileStream.Close();
}
httpWebRequest2.ContentLength = memStream.Length;
Stream requestStream = httpWebRequest2.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
webResponse21 = (HttpWebResponse)httpWebRequest2.GetResponse();
return webResponse21;
}