I am trying to send some parameters and a file to the server using Commons HTTPClient (V 3.1). On the server end I am using Commons fileupload( V 1.1.1.1) to get parameters and file. I am getting following exception
view plaincopy to clipboardprint?
org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:331)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
at com.asite.supernova.bc.AsiteHttpConnector.readMultipartRequestParam(AsiteHttpConnector.java:648)
org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:331)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:116)
at com.asite.supernova.bc.AsiteHttpConnector.readMultipartRequestParam(AsiteHttpConnector.java:648)
Here is Client code
view plaincopy to clipboardprint?
String strXML =FileTest.readTextFile("C:/development/test.xml");
PostMethod method = new PostMethod("http://localhos:7080/exchange/receive");
method.addRequestHeader("Content-type", "multipart/form-data" );//multipart-mixed
Part[] parts = new Part[5];
parts[0] = new StringPart("username","rdoshi's@asi001.com",method.getRequestCharSet());
parts[1] = new StringPart("password","rdoshi123",method.getRequestCharSet());
parts[2] = new StringPart("RefNo", "05355/1001388462",method.getRequestCharSet());
parts[3] = new StringPart("payload",strXML,method.getRequestCharSet());
File f = new File("C:/development/test.pdf");
parts[4] = new FilePart("pdfdoc",f.getName(),f);
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
HttpClient client = new HttpClient();
int statusCode = client.executeMethod(method);
System.out.println("Status : "+statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
String responseString = method.getResponseBodyAsString();
System.out.println("Response : \n\n"+responseString);
String strXML =FileTest.readTextFile("C:/development/test.xml");
PostMethod method = new PostMethod("http://localhos:7080/exchange/receive");
method.addRequestHeader("Content-type", "multipart/form-data" );//multipart-mixed
Part[] parts = new Part[5];
parts[0] = new StringPart("username","rdoshi's@asi001.com",method.getRequestCharSet());
parts[1] = new StringPart("password","rdoshi123",method.getRequestCharSet());
parts[2] = new StringPart("RefNo", "05355/1001388462",method.getRequestCharSet());
parts[3] = new StringPart("payload",strXML,method.getRequestCharSet());
File f = new File("C:/development/test.pdf");
parts[4] = new FilePart("pdfdoc",f.getName(),f);
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
HttpClient client = new HttpClient();
int statusCode = client.executeMethod(method);
System.out.println("Status : "+statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
String responseString = method.getResponseBodyAsString();
System.out.println("Response : \n\n"+responseString);
Server Code
view plaincopy to clipboardprint?
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if(item.isFormField()) {
if(item.getFieldName().equalsIgnoreCase("username")) {
request.setAttribute("username", item.getString());
}else if(item.getFieldName().equalsIgnoreCase("password")) {
request.setAttribute("password", item.getString());
}else if(item.getFieldName().equalsIgnoreCase(IntegrationPropConstants.ACTION)) {
request.setAttribute(IntegrationPropConstants.ACTION, item.getString());
}else if(item.getFieldName().equalsIgnoreCase("refno")) {
request.setAttribute("refno", item.getString());
}else if(item.getFieldName().equalsIgnoreCase("payload")) {
request.setAttribute("payload", item.getString());
}
}else if(item.getSize()>0){
String filedName = item.getFieldName();
if(filedName.equalsIgnoreCase("pdfdoc")){
String fileExt = item.getName();
fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
String uploadFilePath = "c:/temp/my.pdf";
byte[] b = item.get();
OutputStream fos = new FileOutputStream(uploadFilePath);
fos.write(b);
fos.flush();
fos.close();
request.setAttribute("attach_path", uploadFilePath);
}
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if(item.isFormField()) {
if(item.getFieldName().equalsIgnoreCase("username")) {
request.setAttribute("username", item.getString());
}else if(item.getFieldName().equalsIgnoreCase("password")) {
request.setAttribute("password", item.getString());
}else if(item.getFieldName().equalsIgnoreCase(IntegrationPropConstants.ACTION)) {
request.setAttribute(IntegrationPropConstants.ACTION, item.getString());
}else if(item.getFieldName().equalsIgnoreCase("refno")) {
request.setAttribute("refno", item.getString());
}else if(item.getFieldName().equalsIgnoreCase("payload")) {
request.setAttribute("payload", item.getString());
}
}else if(item.getSize()>0){
String filedName = item.getFieldName();
if(filedName.equalsIgnoreCase("pdfdoc")){
String fileExt = item.getName();
fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
String uploadFilePath = "c:/temp/my.pdf";
byte[] b = item.get();
OutputStream fos = new FileOutputStream(uploadFilePath);
fos.write(b);
fos.flush();
fos.close();
request.setAttribute("attach_path", uploadFilePath);
}
}
Do any one have idea what could be the problem and possible resolution?
--------------------------------------------
Regards
SCJP 1.4, SCBCD 5.0
Amit Vinod Dali
Ranch Hand
Joined: Apr 14, 2010
Posts: 42
posted 2010年4月16日 13:56:45 0
The following link will help you:
http://www.devdaily.com/java/jwarehouse/commons-fi...load/FileUploadBase.java.shtml
sudhir nim
Ranch Hand
Joined: Aug 29, 2007
Posts: 212
I like...
posted 2010年4月16日 14:33:17 0
This Servlet file upload tutorial may help you.
--------------------------------------------
[Servlet tutorial] [Servlet 3.0 Cook Book]
Amit Savani
Greenhorn
Joined: Mar 02, 2009
Posts: 16
posted 2010年4月16日 15:51:25 0
sudhir nim wrote:
This Servlet file upload tutorial may help you.
Thanks for reply. Actually I am able to upload file form html code but I want same scenario using Commons HTTPClient which right now not working
Amit Savani
Greenhorn
Joined: Mar 02, 2009
Posts: 16
posted 2010年4月16日 16:50:54 0
sudhir nim wrote:
This Servlet file upload tutorial may help you.
Thanks for sharing. I found following code in FileUploadBase class which is throwing error.
view plaincopy to clipboardprint?
int boundaryIndex = contentType.indexOf("boundary=");
if (boundaryIndex < 0)
{
throw new FileUploadException(
"the request was rejected because "
+ "no multipart boundary was found");
}
int boundaryIndex = contentType.indexOf("boundary=");
if (boundaryIndex < 0)
{
throw new FileUploadException(
"the request was rejected because "
+ "no multipart boundary was found");
}
Now I wonder what is problem with code where I set content type as multipart/form-data or multipart/mixed?
On the other hand if I submit same data with html form having enc-type="multipart/form-data" it's work fine.
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12612
posted 2010年4月16日 21:10:45 0
I think form-data is the correct type to use here, since you're really submitting a form.
Amit Savani
Greenhorn
Joined: Mar 02, 2009
Posts: 16
posted 2010年4月19日 17:41:31 0
Here I modified client code, used deprecated method and it worked. Below is the code
view plaincopy to clipboardprint?
String strXML =FileTest.readTextFile("C:/development/test.xml");
MultipartPostMethod method = new MultipartPostMethod("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);
File f = new File("C:/development/test.pdf");
method.addParameter("pdfdoc",f.getName(),f);
File f = new File("C:/development/test.pdf");
HttpClient client = new HttpClient();
int statusCode = client.executeMethod(method);
System.out.println("Status : "+statusCode);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
String responseString = method.getResponseBodyAsString();
System.out.println("Response : \n\n"+responseString);