首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >中断OutputStream - Android

中断OutputStream - Android
EN

Stack Overflow用户
提问于 2015-03-30 23:59:43
回答 1查看 211关注 0票数 0

我使用类来上传HTTP中的文件,我的问题是,当我从路由器中删除lan电缆时,上传块作为写调用块出现,下面显示了它的确切位置,我尝试中断线程,但这也没有帮助。

现在的问题是我如何打断outputStream?

代码语言:javascript
复制
public void addFilePart(String fieldName, File uploadFile)
        throws IOException {
    ...
    //removed code from here for clarity.
    //Can be found in the link above

    FileInputStream inputStream = new FileInputStream(uploadFile);
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        //The code stuck here.
        //using if (Thread.interrupted()) doesn't help
        //as the write blocks when the network cable 
        //removed from the router
        outputStream.write(buffer, 0, bytesRead);
    }
    outputStream.flush();
    inputStream.close();

    writer.append(LINE_FEED);
    writer.flush();    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-01 20:41:12

到目前为止,我找到的最好的解决方案是从另一个线程执行HttpURLConnection.disconnect(),这似乎很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29358373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档