停止 Java 小程序中文件的下载
我想知道停止下载文件的最优雅和最佳的方法是什么。我有一个简单的小程序,应该从我网站上的用户帐户下载请求的文件。
我想为我的用户提供随时停止下载的可能性。我的代码的简单版本:
InputStream is = null;
FileOutputStream os = null;
os = new FileOutputStream(...);
URL u = new URL(...);
URLConnection uc = u.openConnection();
is = uc.getInputStream();
final byte[] buf = new byte[1024];
for(int count = is.read(buf);count != -1;count = is.read(buf)) {
os.write(buf, 0, count);
}
我唯一的想法是定义新变量,例如 MyStop,将其设为 false 并放入我的循环指令中:
if(MyStop == true) { break; }
当用户单击“停止”按钮时,MyStop 将更改为 true。
这就是我的想法,但我很确定有更好的解决方案。
提前致谢。 造型器
I wonder what is the most elegant and optimal way to stop downloading a file. I have a simple applet which is supposed to download requested files from user account at my website.
I would like to offer my users possibility to stop download at any moment. Simplet version of my code:
InputStream is = null;
FileOutputStream os = null;
os = new FileOutputStream(...);
URL u = new URL(...);
URLConnection uc = u.openConnection();
is = uc.getInputStream();
final byte[] buf = new byte[1024];
for(int count = is.read(buf);count != -1;count = is.read(buf)) {
os.write(buf, 0, count);
}
My only idea is to define new variable e.g. MyStop, make it false and put in my loop instructions:
if(MyStop == true) { break; }
MyStop would be changing into true when user clicked the "Stop" button.
That's what I was thinking about but I am pretty sure that there is a better solution.
Thanks in advance.
Styler
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论