如何使用 Apache Commons 文件从 servlet 上传文件? [英] How to upload a file using Apache Commons file upload from a servlet?

查看:23
本文介绍了如何使用 Apache Commons 文件从 servlet 上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这就是我正在尝试的方法,效果很好,如果我将文件直接提供给 FileBody,但是如何欺骗它让文件内容作为多部分请求发送到另一个 servlet?

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

更新

所以现在在检查 tomcat 日志后 &另一个服务器端代码,我开始知道内部 dc 正在获取 cfgfile 并将其设置为

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
}

这给了我

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

那么我如何用 public void setCfgfile(String cfgfile) 重载 setCfgfile 方法并将 cfgfile 转换为 File代码>对象在这里?

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

或者更好,

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

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
}

这篇关于如何使用 Apache Commons 文件从 servlet 上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆