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);
分享到:
相关推荐
s变换用的高斯窗函数( 高斯窗是指数窗的一种,它也无负的旁瓣,而且没有旁瓣波动,因而不回引起计算谱中假的极大值或极小值,而且高斯窗频率窗函数的主瓣比指数窗的主瓣窄,分辨率比指数窗有所提高。
2021科大讯飞车辆贷违预测大赛冠军源码+全部资料.zip [资源说明] 1、该项目是团队成员近期最新开发,代码完整,资料齐全,含设计文档等 2、上传的项目源码经过严格测试,功能完善且能正常运行,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的高校学生、教师、科研工作者、行业从业者下载使用,可借鉴学习,也可直接作为毕业设计、课程设计、作业、项目初期立项演示等,也适合小白学习进阶,遇到问题不懂就问,欢迎交流。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 5、不懂配置和运行,可远程教学 欢迎下载,学习使用!
AI图像处理工具包-一键抠图、背景切换、旧照片修复、人像漫画化、视频卡通化(Python+OpenCV+Dlib+TensorFlow).zip [资源说明] 1、该项目是团队成员近期最新开发,代码完整,资料齐全,含设计文档等 2、上传的项目源码经过严格测试,功能完善且能正常运行,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的高校学生、教师、科研工作者、行业从业者下载使用,可借鉴学习,也可直接作为毕业设计、课程设计、作业、项目初期立项演示等,也适合小白学习进阶,遇到问题不懂就问,欢迎交流。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 5、不懂配置和运行,可远程教学 欢迎下载,学习使用!
基于java+springboot+vue+mysql的远程教育网站设计与实现.docx
毕业设计资料,计算机毕业设计,源码,毕业论文,毕业答辩,答辩PPT,Java毕业设计,php毕业设计,ASP.NET毕业设计,毕业指导,计算机作业,php作业,java作业,ASP.NET作业,编程作业,管理系统,网站,app,毕业设计学习,Java学习,php学习,ASP.NET学习,java课程,php课程,ASP.NET课程,答辩技巧,SQLSERVER数据库,Mysql数据库,jdbc,SSM框架,SpringBoot框架,Html5,小程序
蓝牙串口助手,可以连接HC-05等蓝牙模块,实现单片机设备与手机通讯,安卓手机,蓝牙调试助手,具有按键功能!
TriLib 2 是一个跨平台的运行时 3D 模型导入器
人力资源+大数据+薪酬报告+涨薪调薪,在学习、工作生活中,越来越多的事务都会使用到报告,通常情况下,报告的内容含量大、篇幅较长。那么什么样的薪酬报告才是有效的呢?以下是小编精心整理的调薪申请报告,欢迎大家分享。相信老板看到这样的报告,一定会考虑涨薪的哦。