0

JDK の sun.net.httpserver を使用して、デバイス用の単純な http サービスを作成しています。

投稿フォームのデータをファイルに保存したいのですが、ファイルに追加のコンテンツが保存されていることが常にわかりました。

root@android:/sdcard # cat a.jpg
-----------------------------288271103222787
Content-Disposition: form-data; name="myfile"; filename="a.jpg"
Content-Type: text/plain

CONTENT of the uploaded file

-----------------------------288271103222787--

「アップロードされたファイルのコンテンツ」のみを保存するには、これをどのように処理できますか?

私の現在のコードは次のとおりです。

private String handleWebSvcApi(HttpExchange exchange, String apiName) throws Exception {
        Log.d(TAG, "handleWebSvcApi : " + apiName);
        InputStream is = exchange.getRequestBody();
        String ret = mWebServiceApiHandler.handleWebServiceApi(apiName, is);
        return ret;
    }

public String handleWebServiceApi(String apiName, InputStream apiData) throws Exception {
        Object ret = null;
        if(apiName.equals("upload")) {
            String filePath = saveApiDataAsFile(apiData);
        }
}

private static String saveApiDataAsFile(InputStream is) throws IOException {
        BufferedInputStream in = new BufferedInputStream(is);
        File outPutFile = new File(Environment.getExternalStorageDirectory().getPath() + "/newfirmware.zip");
        OutputStream os = new FileOutputStream(outPutFile);
        int len = 0;
        byte buff[] = new byte[8192];
        while ((len = in.read(buff)) > 0) {
            os.write(buff, 0, len);
        }
        os.close();
        return outPutFile.getAbsolutePath();
   }
4

0 に答える 0