简体   繁体   中英

How to upload a file using Apache Commons file upload from a servlet?

I need to upload a xml file from server side, where the contents of a file is in a string. How can I make this file content to be uploaded (basically save) on the server?

This is what I am trying, which works fine, If i give a file directly to FileBody but how to trick it to have filecontents going to another servlet as multipart request?

private def createConfiguration(def sessiontoken)
{
    def xmlString=""
    HttpClient httpclient = new DefaultHttpClient();
    try {

        HttpPost httppost = new HttpPost(fileParams.create);

        //FileBody bin = new FileBody(new File("C:\\Simon\\myxml.xml"));
        StringBody st = new StringBody(sessiontoken);
        StringBody cfgname = new StringBody(reqParams.c_Cfgname[0]);
        StringBody cfgdesc = new StringBody(reqParams.c_Cfgdesc[0]);
        StringBody cfgtype = new StringBody(reqParams.c_Cfgtype[0]);
        StringBody cfgfile = new StringBody(reqParams.CFGFILE[0]);

        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("sessiontoken", st);
        reqEntity.addPart("cfgname", cfgname);
        reqEntity.addPart("cfgdesc", cfgdesc);
        reqEntity.addPart("cfgenv", cfgtype);
        //reqEntity.addPart("cfgfile", bin);
        reqEntity.addPart("cfgfile", cfgfile);

        httppost.setEntity(reqEntity);

        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        System.out.println("----------------------------------------");
        //System.out.println(response.getStatusLine());
        if (resEntity != null) {
            //System.out.println("Response content length: " + resEntity.getContentLength());
            xmlString=resEntity.getContent().getText()
        }
        EntityUtils.consume(resEntity);
    } finally {
        try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
    }
    xmlString
}

If I use the above code I get the below Exception

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



Exception while processing your Request.
No result defined for action com.abc.dc.actions.CreateConfiguration and
 result input

Update

So now after checking the tomcat logs & the other server side code, I came to know that that internally dc is getting cfgfile and setting it to

public void setCfgfile(File cfgfile)
{
    this.cfgfile = cfgfile
}

which gives me

java.lang.NoSuchMethodException: com.abc.dc.actions.CreateConfiguration.setCfgfile([Ljava.lang.String;)

So how can I overload setCfgfile method with public void setCfgfile(String cfgfile) and convert cfgfile into a File object here?

or Even better,

How can I convert this cfgfile string variable into a FileBody object?

Finally here is how I made it to work :)

private def sendRequest(def sessiontoken,def sendUrl)
{
    logger.debug("Inside sendRequest to: "+sendUrl)
    def xmlString=""
    HttpClient httpclient = new DefaultHttpClient();
    try {

        HttpPost httppost = new HttpPost(sendUrl);

        def filename=reqParams.filename
        logger.debug("Filename: "+filename)
        FileBody bin = new FileBody(writeToFile(filename,reqParams.cfgfile));
        StringBody st = new StringBody(sessiontoken);
        StringBody cfgid=new StringBody("")
        if(reqParams.containsKey('cfgfile')&&reqParams.cfgid!=null)
        {
            cfgid= new StringBody(reqParams.cfgid);
        }

        StringBody cfgname = new StringBody(reqParams.cfgname);
        StringBody cfgdesc = new StringBody(reqParams.cfgdesc);
        StringBody cfgenv = new StringBody(reqParams.cfgenv);

        logger.debug("attaching multipart")
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("sessiontoken", st);
        reqEntity.addPart("cfgid", cfgid);
        reqEntity.addPart("cfgname", cfgname);
        reqEntity.addPart("cfgdesc", cfgdesc);
        reqEntity.addPart("cfgenv", cfgenv);
        reqEntity.addPart("cfgfile", bin);

        httppost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {
            xmlString=resEntity.getContent().getText()
        }
        EntityUtils.consume(resEntity);
    } finally {
        try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
    }
    xmlString
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM