我可以通过映射到“/”的POST请求将文件和一些额外数据发送到我的REST服务器 .

这是一个使用HTML代码的示例,使用此表单,服务器存储文件和一些元数据(appID,description):

<form method="POST" enctype="multipart/form-data" action="/">
        <table>
            <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
            <tr><td></td><td><input type="submit" value="Upload" /></td></tr>
        </table>
                <input type="hidden" name="appID" value="uploadForm" />
                <input type="hidden" name="description" value="example of description" />
    </form>

我需要使用此POST请求发送应用程序的屏幕截图 .

我写了一个似乎正确保存文件“screenshot.png”的方法(我发现模拟器使用的.cn1文件夹中的文件),但POST请求失败,出现HTTP错误代码400(显示在对话框中) . 以下方法有什么问题?请假设前面的HTML代码没有错误,我希望对Codename One也这样做 .

日志报告以下信息,服务器的URL正确:

[EDT] 0:0:17,736 - POST多部分请求的存储URL:http:// localhost:8090 / [EDT] 0:0:17,738 - 要发送的屏幕截图文件:file://home/screenshot.png

public static void sendScreenshotToCloud(Form form, String info) {

    if (form == null) {
        form = getCurrentForm();
    }

    Image screenshot = Image.createImage(form.getWidth(), form.getHeight());
    form.revalidate();
    form.setVisible(true);
    form.paintComponent(screenshot.getGraphics(), true);

    ImageIO imgIO = ImageIO.getImageIO();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        imgIO.save(screenshot, out, ImageIO.FORMAT_PNG, 1);
    } catch (IOException err) {
        Log.e(err);
    }
    byte[] ba = out.toByteArray();
    try (OutputStream os = Storage.getInstance().createOutputStream("screenshot.png")) {
        os.write(ba);
    } catch (IOException err) {
        Log.e(err);
        Log.sendLog();
    }

    MultipartRequest request = new MultipartRequest();
    request.addResponseListener((e) -> {
        // process the response
        if (e.getResponseCode() == 200) {
            Log.p("Screenshot uploaded to the cloud successfully");
            ToastBar.Status status = ToastBar.getInstance().createStatus();
            status.setMessage("Screenshot sent...");
            status.show();
            UITimer.timer(2000, false, () -> status.clear());
        } else {
            Log.p("ERROR in sending the screenshot to the cloud");
            ToastBar.Status status = ToastBar.getInstance().createStatus();
            status.setMessage("ERROR: cannot send screenshot...");
            status.show();
            UITimer.timer(2000, false, () -> status.clear());
        }
    });
    String url = RestRequest.getInstance().getConnectionRequestStorageURL("");
    String filePath = getAppHomePath()+"screenshot.png";
    Log.p("Storage URL for POST multipart request: \n" + url);
    Log.p("Screenshot file to send: \n" + filePath);
    request.setUrl(url);
    try {
        request.addData("file", filePath, "image/png");
    } catch (IOException ex) {
        Log.e(ex);
        Log.sendLog();
    }
    request.addArgument("appID", RegisterApp.getAppID());
    // request.addArgument("description", info + Miscellaneous.getAllLogs());
    NetworkManager.getInstance().addToQueue(request);

    ToastBar.Status status = ToastBar.getInstance().createStatus();
    status.setMessage("Uploading...");
    status.show();
    UITimer.timer(2000, false, () -> status.clear());
}