サーバー側からxmlファイルをアップロードする必要があります。ファイルの内容は文字列です。このファイルのコンテンツをサーバーにアップロード(基本的に保存)するにはどうすればよいですか?
これは私が試していることであり、正常に機能します。ファイルを直接に渡す場合、ファイルFileBody
コンテンツをマルチパートリクエストとして別のサーブレットに送信するようにトリックする方法はありますか?
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
オブジェクトに変換するにはどうすればよいですか?