如何使用来自servlet的Apache Commons文件上传文件?

时间:2012-05-17 16:38:28

标签: java file-upload groovy apache-commons

我需要从服务器端上传一个xml文件,其中文件的内容是字符串。如何在服务器上上传(基本保存)此文件内容?

这就是我正在尝试的,哪个工作正常,如果我直接向FileBody提供文件但是如何欺骗它让filecontents作为多部分请求转到另一个servlet?

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
}

如果我使用上面的代码,我会得到以下异常

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



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

更新

所以现在检查了tomcat日志&在其他服务器端代码中,我发现内部dc正在获取cfgfile并将其设置为

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

给了我

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

那么如何使用setCfgfile重载public void setCfgfile(String cfgfile)方法并将cfgfile转换为File对象?

或更好,

如何将此cfgfile字符串变量转换为FileBody对象?

1 个答案:

答案 0 :(得分:0)

最后,我的工作方式如下:)

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
}
相关问题