`
dyg001
  • 浏览: 27658 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

no boundary was found !

阅读更多
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);   

分享到:
评论

相关推荐

    Bochs - The cross platform IA-32 (x86) emulator

    Now the USB device classes no longer exist twice if both HC plugins are loaded. - added 'pseudo device' in common USB code for the device creation. This makes the HCs independent from the device ...

    Sakemail

    A obscure bug was found by HuangYeJun from china, in the RetrieveHeaders function if the retrieved text was larger than 1024 bytes and the crlf.crlf fall in the middle of two chunks, the function is ...

    雷达技术知识

    date, however, no one has tested if LiDAR returns from water surfaces can be used to measure local water surface slopes within the active channel. Much of the reason that researchers have not ...

    690个四级高频词汇

    例如:There was no suspicion of foul play.(没有发现任何犯罪迹象。) 26. **suspicious** (a.) - 怀疑的,可疑的。例如:The detective found something suspicious about the scene.(侦探觉得现场有些可疑。...

    最新英语四级高频词汇

    - 例句:There was no real evidence, just a suspicion that something was wrong.(没有确凿的证据,只是怀疑有些不对劲。) 26. **suspicious** (adj.) 怀疑的 - 例句:She became suspicious when she ...

    uhdd.sys源码

    gets enabled by the EMM driver XMGR loads there with no B copies all its boot data and takes over XMS work For a small XMS only system XMGR can also run entirely in low memory RDISK is a DOS ...

    四级高级词汇

    - *例句*: There was no suspicion of foul play. 26. **suspicious** (a.) 怀疑的,可疑的 - *解析*: 描述对某事持怀疑态度的状态。 - *例句*: His suspicious behavior raised concerns among his colleagues...

    Using DIB Sections in VB

    This technique was originally presented in VBPJ article (Note the original article is no longer easily available). It uses a hidden VB call exposed by MSVBVM50.DLL (which is also available in VB6 - ...

    2012新四级700高频词汇

    - **例句**:The mistake was so insignificant that no one noticed it. 31. **accelerate** [əkˈseləreɪt] - **含义**:vt. 加速,促进 - **例句**:Pressing the gas pedal accelerates the car. 32. **...

    大学英语四级重点词汇700词

    例如:“There was no suspicion of foul play.”(没有迹象表明有任何不正当行为。) #### suspicious a. 怀疑的,可疑的 - **解析**:suspicious 形容词,用来描述某人或某事令人怀疑或可疑的特性。例如:“He ...

    DIBSection Class VB

    This technique was originally presented in VBPJ article (Note the original article is no longer easily available). It uses a hidden VB call exposed by MSVBVM50.DLL (which is also available in VB6 - ...

    kgb档案压缩console版+源码

    The decompressor requires as much memory as was used to compress. There is no option. It is not possible to add, remove, or update files in an existing archive. If you want to do this, extract the ...

Global site tag (gtag.js) - Google Analytics