手机APP实现断点续传功能,

/** 
     * 下载服务器已存在的文件 
     *  
     * @param request 
     * @param response 
     * @param proposeFile 
     * @throws IOException 
     * @throws FileNotFoundException 
     */  
	@Value("${uploadDir}")
	String path;
	@RequestMapping(value="/app/downLoadExists")
    private void downloadExistsFile(HttpServletRequest request,  
            HttpServletResponse response,String attachmentId,long pos) throws IOException,  
            FileNotFoundException {  
    	try {
			Attachment attachment=service.getInfoById(attachmentId);
			//SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			String apkPath=path+File.separator+"attachment"+File.separator+attachment.getEntityName()+File.separator+attachment.getCreateDate()+File.separator+attachment.getAttachmentName();
			RandomAccessFile raf=new RandomAccessFile(new File(apkPath),"r");
			raf.seek(pos);//当前文件的开始位置
			// 下载  
			ServletOutputStream out = response.getOutputStream();  
			BufferedOutputStream bufferOut = new BufferedOutputStream(out);  
     
			byte[] buffer = new byte[1 * 1024];  
			int length = 0;  
			while ((length = raf.read(buffer, 0, buffer.length)) != -1) {  
			    bufferOut.write(buffer, 0, length);  
			}  
			bufferOut.flush();  
			bufferOut.close();  
			out.close();  
			raf.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }  

你可能感兴趣的:(手机APP实现断点续传功能,)