discuz 网站 出现此页面不能正确地重定向的处理办法之一
2015-03-27 10:57
239 查看
断点续传主要是使用http协议中range的属性来取得资源的部分内容,由于一般服务是不对外直接提供url访问的,一般都是通过id,在servlet中输出byte[]来实现,所以要想实现断点续传一般要自己实现一个服务端。
一个简单实现:
服务端:主要是分析了range属性,利用RandomAccessFile读取内容输出字节流
客户端:分两次取得部分内容,输出到RandomAccessFile中
接下来的工作:实现客户端的并发,多线程,即多个下载任务同时进行,连接的复用,实现暂停,显示进度条,下载完成的事件处理等,不涉及具体业务,搭建整个架构。
一个简单实现:
服务端:主要是分析了range属性,利用RandomAccessFile读取内容输出字节流
public class Download extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { int id = Integer.parseInt(req.getParameter("id")); String filename ; if (id == 1) filename = "E:/JDK_API_1_5_zh_CN.CHM"; else if (id == 2) filename = "E;/JDK_API_1_6_zh_CN.CHM"; else if (id == 3) filename = "E:/apache-tomcat-5.5.26/webapps/ROOT/tomcat.gif"; else filename = "c.rar"; RandomAccessFile raFile = new RandomAccessFile(filename, "r"); String range = req.getHeader("RANGE"); int start=0, end=0; if(null!=range && range.startsWith("bytes=")){ String[] values =range.split("=")[1].split("-"); start = Integer.parseInt(values[0]); end = Integer.parseInt(values[1]); } int requestSize=0; if(end!=0 && end > start){ requestSize = end - start + 1; resp.addHeader("content-length", ""+(requestSize)); } else { requestSize = Integer.MAX_VALUE; } byte[] buffer = new byte[4096]; resp.setContentType("application/x-download"); resp.addHeader("Content-Disposition", "attachment;filename=a.chm"); ServletOutputStream os = resp.getOutputStream(); int needSize = requestSize; raFile.seek(start); while(needSize > 0){ int len = raFile.read(buffer); if(needSize < buffer.length){ os.write(buffer,0,needSize); } else { os.write(buffer,0,len); if(len < buffer.length){ break; } } needSize -= buffer.length; } raFile.close(); os.close(); } }
客户端:分两次取得部分内容,输出到RandomAccessFile中
public static void main(String[] args) throws MalformedURLException, FileNotFoundException { test1(0,1000); test1(1001,0); } public static void test1(int start, int end) throws MalformedURLException, FileNotFoundException{ String endpoint = "http://localhost:8080/Hello/download?id=1"; RandomAccessFile raFile = new RandomAccessFile("E:/temp/test.chm", "rw"); URL url = new URL(endpoint); try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //conn.addRequestProperty("location", "/tomcat.gif"); conn.setRequestProperty("Content-Type","text/html; charset=UTF-8"); conn.setRequestProperty("RANGE","bytes="+start+"-"+end); conn.connect(); System.out.println(conn.getResponseCode()); System.out.println(conn.getContentLength()); System.out.println(conn.getContentType()); InputStream ins = (InputStream)conn.getContent(); raFile.seek(start); byte[] buffer = new byte[4096]; int len = -1; while((len = ins.read(buffer))!=-1){ raFile.write(buffer,0,len); } raFile.close(); conn.disconnect(); } catch (IOException e) { e.printStackTrace(); } }
接下来的工作:实现客户端的并发,多线程,即多个下载任务同时进行,连接的复用,实现暂停,显示进度条,下载完成的事件处理等,不涉及具体业务,搭建整个架构。
相关文章推荐
- APK文件传到网站上出现不能被下载问题解决办法
- 跳转页面出现在框架中的处理办法
- 【ASP.NET 问题】IIS发布网站后出现 "处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误"的解决办法
- 配置Silverlight网站访问出现空白页面的解决办法(未配置MIME)---转
- jsp页面向action通过get方式传参,出现乱码的处理办法
- VS2010发布.NET2.0网站,出现“未预编译文件* 因此不能请求该文件”的解决办法
- 【ASP.NET 问题】IIS发布网站后出现 "处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误"的解决办法
- IIS 不能解析动态页面的处理办法。
- dedecms网站出现多图发布无法使用页面空白的原因及解决办法
- 在全新安装的IIS7下搭建网站,访问页面时出现错误信息,配置错误: 不能在此路径中使用此配置节。
- 在本机部署的dedecms网站上传到空间后出现首页不能访问的解决办法
- 【ASP.NET 问题】IIS发布网站后出现 "处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误"的解决办法
- thinkphp出现此页面不能重定向
- 火狐 此页面不能正确地重定向
- ASP.NET网站出现错误:由于无法创建应用程序域,因此未能执行请==的处理办法
- 网站安全退出后,再点击浏览器后退按钮不能返回前一个页面的笨办法
- VS2010发布.NET2.0网站,出现“未预编译文件* 因此不能请求该文件”的解决办法
- IIS发布网站后出现 "处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误"的解决办法
- U8800 手机恢复出厂设置出现轻触Android开始页面 处理办法
- Asp.Net : 捕捉和记录网站中出现的所有未处理错误,抛出详细的页面来源和访问ip,调用的接口方法及异常实例(记事本日志,系统日志及数据库日志)