4

サーブレットに文字列として xml ファイルのコンテンツがあります。xml ファイルとしてアップロードするには、マルチパート ポスト リクエストで別の URL を呼び出す必要があります。

それを行う方法はありますか?

これまでのところ、これは私がやっていることです

private def createConfiguration(def sessiontoken)
{
    /*reqParams is request.getParameterMap(), fileParams is again a map*/
    def charset = "UTF-8";
    def query = String.format("emailaddress=%s&projectid=%s&cfgname=%s&cfgdesc=%s&cfgfile=%s",
        URLEncoder.encode(sessiontoken, charset),
        URLEncoder.encode(reqParams.c_Cfgname[0], charset),
        URLEncoder.encode(reqParams.c_Cfgdesc[0], charset),
        URLEncoder.encode(reqParams.c_Cfgtype[0], charset),
        URLEncoder.encode(reqParams.CFGFILE[0], charset),)

    URLConnection connection = new URL(fileParams.login).openConnection()
    connection.setDoOutput(true)
    connection.setRequestProperty("Accept-Charset", charset)
    connection.setRequestProperty("Content-Type", "multipart/form-data;charset=" + charset)
    try {
        connection.getOutputStream().write(query.getBytes(charset))
    }
    finally {
        connection.getOutputStream().close()
    }
    InputStream response = connection.getInputStream()
    def xmlString=response.getText()
    xmlString
}

以下はフェッチされた例外です

SEVERE: サーブレット RedirectRequest の Servlet.service() が例外 java.io.IOException をスローしました: サーバーが HTTP 応答コードを返しました: 800 for URL: http://abhishek157:10070/project/create.action at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at sun.net .www.protocol.http.HttpURLConnection$getInputStream.call(不明なソース) . .

アップデート

BalusC からこの非常に便利なリンクを取得したので、それを使用しました。

private def getStreamFromString(str)
{
    // convert String into InputStream
    InputStream is = new ByteArrayInputStream(str.getBytes())
    is
}

private def createConfiguration(def sessiontoken)
{


    println "ok good $sessiontoken"
    def charset = "UTF-8"
    def boundary = Long.toHexString(System.currentTimeMillis())
    def CRLF = "\r\n"
    String param = "value"

    URLConnection connection = new URL(fileParams.create).openConnection()
    println fileParams.create
    connection.setDoOutput(true)
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    PrintWriter writer = null
    try {

        OutputStream output = connection.getOutputStream()
        writer = new PrintWriter(new OutputStreamWriter(output, charset), true)

        // Sending normal param.
        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"sessiontoken\"$CRLF$CRLF$sessiontoken").append(CRLF)
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF)
        writer.append(CRLF)
        writer.append(param).append(CRLF).flush()

        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"cfgname\"$CRLF$CRLF${reqParams.c_Cfgname[0]}").append(CRLF)
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF)
        writer.append(CRLF)
        writer.append(param).append(CRLF).flush()

        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"cfgdesc\"$CRLF$CRLF${reqParams.c_Cfgdesc[0]}").append(CRLF)
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF)
        writer.append(CRLF)
        writer.append(param).append(CRLF).flush()

        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"cfgenv\"$CRLF$CRLF${reqParams.c_Cfgtype[0]}").append(CRLF)
        //writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF)
        writer.append(CRLF)
        writer.append(param).append(CRLF).flush()

        // Sending xml file.
        writer.append("--" + boundary).append(CRLF)
        writer.append("Content-Disposition: form-data; name=\"cfgfile\"; filename=\"" + reqParams.FILENAME[0] + "\"").append(CRLF)
        writer.append("Content-Type: text/xml; charset=" + charset).append(CRLF)
        writer.append(CRLF).flush()
        BufferedReader reader = null
        try {
            reader = new BufferedReader(new InputStreamReader(getStreamFromString(reqParams.CFGFILE[0]), charset))
            for (String line; (line = reader.readLine()) != null;) {
                writer.append(line).append(CRLF)
            }
        }
        catch(Exception e)  {
            e.printStackTrace()
        }
        finally {
            if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
        }
        writer.flush();
        writer.append("--" + boundary + "--").append(CRLF)
    } 
    finally {
        if (writer != null) writer.close();
    }
    InputStream response = connection.getInputStream()
    def xmlString=response.getText()
    xmlString
}

そして私が得るコンソールで

http://abhishek157:10070/project/create.action
完了

しかし全然当たらないhttp://abhishek157:10070/project/create.action
何か助けはありますか?

その他のアップデート

実際のリクエスト (HTML フォームから作業し、Web ブラウザからファイルを選択)

-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="sessiontoken"

4611685684744086913
-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="cfgname"

sadf
-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="cfgdesc"

sadf
-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="cfgenv"

Production
-----------------------------7dcf4d30e8a
Content-Disposition: form-data; name="cfgfile"; filename="C:\Simon\xmls\agentind.xml"
Content-Type: text/xml

<?xml version="1.0" encoding="UTF-8"?> and so on...

フィドラーでの実際のリクエストからのマッチング後に、params 部分を更新しました。createConfiguration関数を見る

例外がフェッチされました (create.actionサーブレットからの呼び出し中)

:パラメータを送信する前にサーブレットをチェックインしましたcreate.action。すべて有効です

java.lang.NumberFormatException: null 

サーバーで読み取られるパラメーターはありません。すべてがnull. 問題はどこだ。助けてください。

4

1 に答える 1

3

connection.getInputStream();更新されたコードでは、実際に HTTP 要求を送信する (および HTTP 応答を取得する) ための呼び出しを忘れていました。

于 2012-05-09T12:30:58.493 に答える