视频、音频、图片代理下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010363836/article/details/49489767

视频、音频、图片代理下载


视频、音频、图片代理下载





  /**
     * 音频下载
     */
    @RequestMapping("/audio/download")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {  
        //String id = request.getParameter("id"); 
        String filePath = ServletRequestUtils.getStringParameter(request, "filePath", ""); //
        String fileName = ServletRequestUtils.getStringParameter(request, "fileName", ""); //
        String destUrl = filePath;
        //LOG.info("--------------"+filePath); 
        
        String fileFormat=filePath.substring(filePath.lastIndexOf("."));
        String name="";
        if(StringUtils.isBlank(fileName)){
            name=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+fileFormat;
        }else {
            name=fileName.trim()+fileFormat;
        }
        //File f = new File(filePath);
        //response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(f.getName(),"UTF-8"));  
        //LOG.info("--------------"+f.getName());

        // 建立链接  
        URL url = new URL(destUrl);  
        HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();  
        // 连接指定的资源  
        httpUrl.connect();  
        // 获取网络输入流  
        BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());  
  
        Integer lenf=httpUrl.getContentLength();
        //String lenf=this.getFileLength(4189053, 7189053);
        response.setContentType("application/x-msdownload"); 
        response.setHeader("Content-Length", lenf.toString());//文件大小值5几M
        response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(name,"UTF-8"));  
        OutputStream out = response.getOutputStream();  
        byte[] buf = new byte[1024];  
        if (destUrl != null) {  
            BufferedInputStream br = bis;  
            int len = 0;  
            while ((len = br.read(buf)) > 0){  
                out.write(buf, 0, len);  
            }                 
            br.close();  
        }  
        out.flush();  
        out.close();  
  
    } 



//////////////////////////////////////////////////////////////////////////////////////////

  /**
     * 视频下载
     * by XnOU
     */
    @RequestMapping("/video/download2")
    public void downloadVideo2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String vid = ServletRequestUtils.getStringParameter(request, "vid", ""); //
        String df = ServletRequestUtils.getStringParameter(request, "df", "1"); //视频码率
        String fileName = ServletRequestUtils.getStringParameter(request, "fileName", "无限极视频"); //
        
        String fileFormat=(".mp4");
        String name=fileName.trim()+fileFormat;
        
        // set the content type
        response.setContentType("Content-Type: application/force-download");
        response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(name,"UTF-8")); 
        
        // http://v.polyv.net/uc/video/getMp4?vid=5fa716fe381c1618befa75f8066554c0_5&df=2
        //String url = "http://v.polyv.net/uc/video/getMp4?vid=" + vid + "&df=" + df;
        String url = "http://mpv.videocc.net/5fa716fe38/0/5fa716fe381c1618befa75f8066554c0_1.mp4";
        
        URL videoUrl = new URL(url);
        
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(videoUrl.openStream());
            bos = new BufferedOutputStream(response.getOutputStream());
            
            byte[] buf = new byte[2048];
            long totalBytes = 0L;
            int readBytes = -1;
            while((readBytes = bis.read(buf)) != -1) {
                bos.write(buf);
                totalBytes += readBytes;
            }
            
            bos.flush();
            logger.info("download total bytes: {}", totalBytes);
            
        }catch(IOException e) {
            logger.error("An error occurs when downloading a video, vid={}", vid, e);
            
        }finally {
            if(null != bis) {
                try{
                bis.close();
                }catch(Exception e) {
                    //忽略
                }
            }
            if(null != bos) {
                try {bos.close();
                }catch(Exception e) {
                    //忽略
                }
            }
        } 
    } 


	///////////////////////////////////////////////////////////////////////////////////////////////////

	    /**
     * 视频下载
     */
    //@RequestMapping("/video/download")
    //public void downloadVideo(HttpServletRequest request, HttpServletResponse response) throws IOException {  
        //String id = request.getParameter("id"); 
        /*String filePath = ServletRequestUtils.getStringParameter(request, "filePath", ""); //
        String fileName = ServletRequestUtils.getStringParameter(request, "fileName", ""); //
        String vid = ServletRequestUtils.getStringParameter(request, "vid", ""); //
        String df = ServletRequestUtils.getStringParameter(request, "df", "1"); //视频码率
        String destUrl = filePath;
        //LOG.info("-------1-------"+filePath); 
        destUrl=destUrl+"?vid="+vid+"&df="+df;
        String fileFormat=(".mp4");
        String name=fileName.trim()+fileFormat;
        //File f = new File(destUrl);
        //LOG.info("--------------"+f.getName());

        // 建立链接  
        URL url = new URL(destUrl);  
        HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();  
        // 连接指定的资源  
        httpUrl.connect();  
        
        
        // 获取网络输入流  
        BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());  
  
        Integer lenf=httpUrl.getContentLength();
        //LOG.info("--------len-------"+lenf);
        //String lenf=this.getFileLength(22489053, 72489053);
        response.setContentType("application/x-msdownload"); 
        response.setHeader("Content-Length", lenf.toString());//文件大小值20几M
        response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(name,"UTF-8"));  
        OutputStream out = response.getOutputStream();  
        byte[] buf = new byte[1024];  
        if (destUrl != null) {  
            BufferedInputStream br = bis;  
            int len = 0;  
            while ((len = br.read(buf)) > 0){  
                out.write(buf, 0, len);  
            }                 
            br.close();  
        }  
        out.flush();  
        out.close();  */
    //} 

	///////////////////////////////////////////////////////////////////////////////////////////////////

	
    
  
    
    
    /**
     * 图片下载
     */
    @RequestMapping("/picture/download")
    public void downloadPicture(HttpServletRequest request, HttpServletResponse response) throws IOException {  
        //String id = request.getParameter("id"); 
        String filePath = ServletRequestUtils.getStringParameter(request, "filePath", ""); //
        String fileName = ServletRequestUtils.getStringParameter(request, "fileName", ""); //
        String courseId = ServletRequestUtils.getStringParameter(request, "courseId", ""); //
        
        Course course = courseManager.getCourse(courseId);
        if(null != course){
        	String userAgentStr = request.getHeader("user-agent");
        	UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
			String browser = userAgent.getBrowser().getName();
			
        	DownloadDataLog downloadDataLog = new DownloadDataLog();
        	downloadDataLog.setDownloadId(PrimaryKeyUtils.generateSequentialKey());
        	downloadDataLog.setCourseId(courseId);
        	downloadDataLog.setCourseType(course.getCourseType());
        	downloadDataLog.setTitle(course.getTitle());
        	downloadDataLog.setCategoryId(course.getCategoryId());
        	downloadDataLog.setCoverImage(course.getCoverImage());
        	downloadDataLog.setSourceUrl(request.getHeader("Referer"));
        	downloadDataLog.setUserIp(IPUtil.getRealIP(request));
        	downloadDataLog.setDefinition("");
        	downloadDataLog.setBrowser(browser);
        	if (UserAgentDetector.isMobile(userAgentStr)) {
        		downloadDataLog.setPlatformType(PlatformTypeStatus.MOBILE.getValue());
        	}else if (UserAgentDetector.isWeiXin(userAgentStr)) {
        		downloadDataLog.setPlatformType(PlatformTypeStatus.WEIXIN.getValue());
        	}else {
        		downloadDataLog.setPlatformType(PlatformTypeStatus.PC.getValue());
        	}
        	downloadDataLog.setUserAgent(userAgentStr);
        	downloadDataLog.setDateAdded(new Date());
        	boolean opStatus = downloadLogManager.addDownloadDataLog(downloadDataLog);
        	logger.info("New download record, courseId={}, opStatus={}", new Object[] { courseId, opStatus });
        	return;
        }
        
        String destUrl = filePath;
        //LOG.info("--------------"+filePath); 
        
        String fileFormat=filePath.substring(filePath.lastIndexOf("."));
        //String name=fileName.trim()+fileFormat;
        String name=filePath.substring(filePath.lastIndexOf("/")+1, filePath.length()); 
        //File f = new File(filePath);
        //response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(f.getName(),"UTF-8"));  
        //LOG.info("--------------"+f.getName());

        // 建立链接  
        URL url = new URL(destUrl);  
        HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();  
        // 连接指定的资源  
        httpUrl.connect();  
        // 获取网络输入流  
        BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());  
  
        Integer lenf=httpUrl.getContentLength();
        //String lenf=this.getFileLength(4189053, 7189053);
        response.setContentType("application/x-msdownload"); 
        response.setHeader("Content-Length", lenf.toString());//文件大小值5几M
        response.setHeader("Content-Disposition", "attachment; filename="+java.net.URLEncoder.encode(name,"UTF-8"));
        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[1024];  
        if (destUrl != null) {  
            BufferedInputStream br = bis;  
            int len = 0;  
            while ((len = br.read(buf)) > 0){  
                out.write(buf, 0, len);  
            }                 
            br.close();  
        }  
        out.flush();  
        out.close();  
  
    } 





猜你喜欢

转载自blog.csdn.net/u010363836/article/details/49489767