no boundary was found !

IamtryingtosendsomeparametersandafiletotheserverusingCommonsHTTPClient(V3.1).OntheserverendIamusingCommonsfileupload(V1.1.1.1)togetparametersandfile.Iamgettingfollowingexception

viewplaincopytoclipboardprint?

org.apache.commons.fileupload.FileUploadException:therequestwasrejectedbecausenomultipartboundarywasfound

atorg.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:331)

atorg.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)

atcom.asite.supernova.bc.AsiteHttpConnector.readMultipartRequestParam(AsiteHttpConnector.java:648)

org.apache.commons.fileupload.FileUploadException:therequestwasrejectedbecausenomultipartboundarywasfound

atorg.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:331)

atorg.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)

atcom.asite.supernova.bc.AsiteHttpConnector.readMultipartRequestParam(AsiteHttpConnector.java:648)

HereisClientcode

viewplaincopytoclipboardprint?

StringstrXML=FileTest.readTextFile("C:/development/test.xml");

PostMethodmethod=newPostMethod("http://localhos:7080/exchange/receive");

method.addRequestHeader("Content-type","multipart/form-data");//multipart-mixed

Part[]parts=newPart[5];

parts[0]=newStringPart("username","rdoshi'[email protected]",method.getRequestCharSet());

parts[1]=newStringPart("password","rdoshi123",method.getRequestCharSet());

parts[2]=newStringPart("RefNo","05355/1001388462",method.getRequestCharSet());

parts[3]=newStringPart("payload",strXML,method.getRequestCharSet());

Filef=newFile("C:/development/test.pdf");

parts[4]=newFilePart("pdfdoc",f.getName(),f);

method.setRequestEntity(newMultipartRequestEntity(parts,method.getParams()));

HttpClientclient=newHttpClient();

intstatusCode=client.executeMethod(method);

System.out.println("Status:"+statusCode);

if(statusCode!=HttpStatus.SC_OK){

System.err.println("Methodfailed:"+method.getStatusLine());

}

StringresponseString=method.getResponseBodyAsString();

System.out.println("Response:\n\n"+responseString);

StringstrXML=FileTest.readTextFile("C:/development/test.xml");

PostMethodmethod=newPostMethod("http://localhos:7080/exchange/receive");

method.addRequestHeader("Content-type","multipart/form-data");//multipart-mixed

Part[]parts=newPart[5];

parts[0]=newStringPart("username","rdoshi'[email protected]",method.getRequestCharSet());

parts[1]=newStringPart("password","rdoshi123",method.getRequestCharSet());

parts[2]=newStringPart("RefNo","05355/1001388462",method.getRequestCharSet());

parts[3]=newStringPart("payload",strXML,method.getRequestCharSet());

Filef=newFile("C:/development/test.pdf");

parts[4]=newFilePart("pdfdoc",f.getName(),f);

method.setRequestEntity(newMultipartRequestEntity(parts,method.getParams()));

HttpClientclient=newHttpClient();

intstatusCode=client.executeMethod(method);

System.out.println("Status:"+statusCode);

if(statusCode!=HttpStatus.SC_OK){

System.err.println("Methodfailed:"+method.getStatusLine());

}

StringresponseString=method.getResponseBodyAsString();

System.out.println("Response:\n\n"+responseString);

ServerCode

viewplaincopytoclipboardprint?

FileItemFactoryfactory=newDiskFileItemFactory();

ServletFileUploadupload=newServletFileUpload(factory);

Listitems=upload.parseRequest(request);

Iteratoriter=items.iterator();

while(iter.hasNext()){

FileItemitem=(FileItem)iter.next();

if(item.isFormField()){

if(item.getFieldName().equalsIgnoreCase("username")){

request.setAttribute("username",item.getString());

}elseif(item.getFieldName().equalsIgnoreCase("password")){

request.setAttribute("password",item.getString());

}elseif(item.getFieldName().equalsIgnoreCase(IntegrationPropConstants.ACTION)){

request.setAttribute(IntegrationPropConstants.ACTION,item.getString());

}elseif(item.getFieldName().equalsIgnoreCase("refno")){

request.setAttribute("refno",item.getString());

}elseif(item.getFieldName().equalsIgnoreCase("payload")){

request.setAttribute("payload",item.getString());

}

}elseif(item.getSize()>0){

StringfiledName=item.getFieldName();

if(filedName.equalsIgnoreCase("pdfdoc")){

StringfileExt=item.getName();

fileExt=fileExt.substring(fileExt.lastIndexOf('.'));

StringuploadFilePath="c:/temp/my.pdf";

byte[]b=item.get();

OutputStreamfos=newFileOutputStream(uploadFilePath);

fos.write(b);

fos.flush();

fos.close();

request.setAttribute("attach_path",uploadFilePath);

}

}

FileItemFactoryfactory=newDiskFileItemFactory();

ServletFileUploadupload=newServletFileUpload(factory);

Listitems=upload.parseRequest(request);

Iteratoriter=items.iterator();

while(iter.hasNext()){

FileItemitem=(FileItem)iter.next();

if(item.isFormField()){

if(item.getFieldName().equalsIgnoreCase("username")){

request.setAttribute("username",item.getString());

}elseif(item.getFieldName().equalsIgnoreCase("password")){

request.setAttribute("password",item.getString());

}elseif(item.getFieldName().equalsIgnoreCase(IntegrationPropConstants.ACTION)){

request.setAttribute(IntegrationPropConstants.ACTION,item.getString());

}elseif(item.getFieldName().equalsIgnoreCase("refno")){

request.setAttribute("refno",item.getString());

}elseif(item.getFieldName().equalsIgnoreCase("payload")){

request.setAttribute("payload",item.getString());

}

}elseif(item.getSize()>0){

StringfiledName=item.getFieldName();

if(filedName.equalsIgnoreCase("pdfdoc")){

StringfileExt=item.getName();

fileExt=fileExt.substring(fileExt.lastIndexOf('.'));

StringuploadFilePath="c:/temp/my.pdf";

byte[]b=item.get();

OutputStreamfos=newFileOutputStream(uploadFilePath);

fos.write(b);

fos.flush();

fos.close();

request.setAttribute("attach_path",uploadFilePath);

}

}

Doanyonehaveideawhatcouldbetheproblemandpossibleresolution?

--------------------------------------------------------------------------------

Regards

SCJP1.4,SCBCD5.0

AmitVinodDali

RanchHand

Joined:Apr14,2010

Posts:42

posted2010年4月16日13:56:450

Thefollowinglinkwillhelpyou:

http://www.devdaily.com/java/jwarehouse/commons-fi...load/FileUploadBase.java.shtml

sudhirnim

RanchHand

Joined:Aug29,2007

Posts:212

Ilike...

posted2010年4月16日14:33:170

ThisServletfileuploadtutorialmayhelpyou.

--------------------------------------------------------------------------------

[Servlettutorial][Servlet3.0CookBook]

AmitSavani

Greenhorn

Joined:Mar02,2009

Posts:16

posted2010年4月16日15:51:250

sudhirnimwrote:

ThisServletfileuploadtutorialmayhelpyou.

Thanksforreply.ActuallyIamabletouploadfileformhtmlcodebutIwantsamescenariousingCommonsHTTPClientwhichrightnownotworking

AmitSavani

Greenhorn

Joined:Mar02,2009

Posts:16

posted2010年4月16日16:50:540

sudhirnimwrote:

ThisServletfileuploadtutorialmayhelpyou.

Thanksforsharing.IfoundfollowingcodeinFileUploadBaseclasswhichisthrowingerror.

viewplaincopytoclipboardprint?

intboundaryIndex=contentType.indexOf("boundary=");

if(boundaryIndex<0)

{

thrownewFileUploadException(

"therequestwasrejectedbecause"

+"nomultipartboundarywasfound");

}

intboundaryIndex=contentType.indexOf("boundary=");

if(boundaryIndex<0)

{

thrownewFileUploadException(

"therequestwasrejectedbecause"

+"nomultipartboundarywasfound");

}

NowIwonderwhatisproblemwithcodewhereIsetcontenttypeasmultipart/form-dataormultipart/mixed?

OntheotherhandifIsubmitsamedatawithhtmlformhavingenc-type="multipart/form-data"it'sworkfine.

DavidNewton

Author

Rancher

Joined:Sep29,2008

Posts:12612

posted2010年4月16日21:10:450

Ithinkform-dataisthecorrecttypetousehere,sinceyou'rereallysubmittingaform.

AmitSavani

Greenhorn

Joined:Mar02,2009

Posts:16

posted2010年4月19日17:41:310

HereImodifiedclientcode,useddeprecatedmethodanditworked.Belowisthecode

viewplaincopytoclipboardprint?

StringstrXML=FileTest.readTextFile("C:/development/test.xml");

MultipartPostMethodmethod=newMultipartPostMethod("http://localhos:7080/exchange/receive");

method.addRequestHeader("Content-type","multipart/form-data");//multipart-mixed

method.addParameter("username","imerys");

method.addParameter("password","a311xhq23");

method.addParameter("RefNo","05355/1001388462");

method.addParameter("payload",strXML);

Filef=newFile("C:/development/test.pdf");

method.addParameter("pdfdoc",f.getName(),f);

Filef=newFile("C:/development/test.pdf");

HttpClientclient=newHttpClient();

intstatusCode=client.executeMethod(method);

System.out.println("Status:"+statusCode);

if(statusCode!=HttpStatus.SC_OK){

System.err.println("Methodfailed:"+method.getStatusLine());

}

StringresponseString=method.getResponseBodyAsString();

System.out.println("Response:\n\n"+responseString);

相关推荐