0
我需要從服務器端上傳一個xml文件,其中文件的內容是一個字符串。我怎樣才能使這個文件內容上傳(基本上保存)在服務器上?如何使用Apache Commons文件上傳從servlet上傳文件?
這就是我正在嘗試,它工作正常,如果我直接給文件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
對象?
這意味着S2(我認爲)要發佈到被拒絕您的文章的行動,最有可能基於驗證失敗。 –
@DaveNewton:你是正確的..在內部,它調用一個將'cfgfile'(這是一個字符串)賦給'File'對象的方法。那麼有沒有辦法將這個字符串轉換成一個'File'對象? – abi1964