【问题标题】:Uploading a user file on discord bot在不和谐机器人上上传用户文件
【发布时间】:2019-12-30 12:12:38
【问题描述】:

我正在尝试让用户在 discord 上提交文件,然后将这些文件上传到网站进行处理。但是,我的代码似乎有问题。

当我上传文件时,我得到响应代码 400(错误请求),但该站点应该接受 w3g 格式的文件。

private static void onFileUpload(Message.Attachment attachment, TextChannel channel) {
    // api.wc3stats.com/upload
    String fileName = attachment.getFileName();
    if (fileName.substring(fileName.length() - 3).equals("w3g")) {

        File file = new File(attachment.getFileName());
        attachment.downloadToFile(file);
        try {
            HttpHelper.postFile(file, "https://api.wc3stats.com/upload");
            channel.sendMessage("Uploading: " + file.toString()).queue();
        } catch (IOException e) {
            e.printStackTrace();
            channel.sendMessage(e.getMessage()).queue();
        }
        System.out.println("Deleted");
        file.delete();

    } else {
        channel.sendMessage("Invalid file type.").queue();
    }
}

public static void postFile(File file, String url) throws IOException {
    HttpURLConnection connection = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    BufferedOutputStream out = null;
    try {
        URL urlForPostRequest = new URL(url);
        connection = (HttpURLConnection) urlForPostRequest.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);


        //if (responseCode == HttpURLConnection.HTTP_OK) {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            out = new BufferedOutputStream(connection.getOutputStream());
            byte[] buffer = new byte[8192];
            int i;
            while ((i = bis.read(buffer)) > 0) {
                out.write(buffer, 0, i);
            }
       //}
        int responseCode = connection.getResponseCode();
        System.out.println("Response: " + connection.getResponseMessage());
        System.out.println(responseCode);
    } finally {
        if (fis != null) fis.close();
        if (bis != null) bis.close();
        if (out != null) out.close();
        System.out.println("closed");
    }
}

它给我的当前异常是找不到文件。但是在添加discord并下载后,我在程序目录中可以清楚地看到它。这也只会在我第一次上传文件时发生。我第二次上传相同的文件时,它只会给我错误的请求响应。

第一次上传尝试 LastReplay.w3g 输出:

    java.io.FileNotFoundException: lol.w3g 
    C:\SomePath\lol.w3g
    cannot be read
    closed
    Deleted

我还可以注意到该文件现在存在于程序目录中。虽然它应该在一切完成后被删除。

LastReplay.w3g 输出的第二次上传尝试

C:\SomePath\lol.w3g
Can be read
Response: Bad Request
closed
Deleted

【问题讨论】:

  • 尝试使用 okhttp 并设置用户代理和内容类型

标签: java http file-upload discord


【解决方案1】:

我设法在使用请求承诺的帮助下使用 Javascript 来做到这一点,至于 java 我没有...

        if (msg.attachments.size > 0) {
            let attached = msg.attachments.array()[0];
            const request = require('request-promise');
            let formData = {
                file: request (attached.url)
            };
            request.post('https://api.wc3stats.com/upload', {formData, json: true})
            .then(function (json) {
                // on success handle response json
            })
            .catch(function (err) {
                console.log(err);
            });
        }

【讨论】:

    猜你喜欢
    • 2017-12-29
    • 2021-11-18
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2021-04-26
    • 2018-09-18
    • 1970-01-01
    • 2021-12-15
    相关资源
    最近更新 更多