Okhttp大文件上传失败

时间:2018-04-14 01:40:11

标签: java node.js okhttp

我遇到了Okhttp + Node.js强大的服务器端大文件上传问题。 目前上传适用于< 100Mb文件,但较大的文件失败。 对于较大的文件,onprogress事件服务器端会被触发,直到99%的进度,无论文件大小如何,然后停止,报告请求中止,并且不会触发onfile事件。

已经尝试了超时解决方法,甚至定义了一个自定义SocketFactory来手动设置套接字keepalive和sotimeout。

所以,我卡在这里。任何帮助将不胜感激。

int main()
{
    const int COUNTRIES = 200;

    string cntrCodes[COUNTRIES];
    int64_t cntrPopulation[COUNTRIES];
    string cntrNames[COUNTRIES];

    string inputFileName = "countries.txt";
    if (std::ifstream inputFile{inputFileName})
    {
        int num_countries = 0;
        while (num_countries < COUNTRIES &&
               inputFile >> cntrCodes[num_countries] >> cntrPopulation[num_countries] &&
               getline(std::cin, cntrNames[num_countries]))
            ++num_countries;

        // will see if we can read another character (remember >> skips
        // whitespace by default), rather than check inputFile.eof()
        // which may not be true if there're any extra empty lines after
        // the data...
        char character;
        if (num_countries == COUNTRIES && inputFile >> character)
        {
            std::cerr << "\n\tThe input file  \"" << inputFileName <<
        "\"is too big: \n\tit has more than " << COUNTRIES << " items!\n";
            exit(EXIT_FAILURE);
        }

        // any extra code to actually use the country data goes here...
    }
    else
    {
        std::cerr << "\n\tPlease check the name of the input file and \n\ttry again later!\n";
        exit(EXIT_FAILURE);
    }
}

1 个答案:

答案 0 :(得分:0)

使用snoopy api太简单了:如果不包括标识符定义,则只需一行代码:)

URI uri = ...;
Path fileToUpload = ...;
Snoopy.builder()
      .config(SnoopyConfig.defaults())
      .build()
      .post(uri)
      .followRedirects(true)
      .failIfNotSuccessfulResponse(true)
      .body(fileToUpload)
      .consumeAsString();

https://bitbucket.org/abuwandi/snoopy

在大型文件上进行了测试,它就像一个魅力