Collect from 企业网站模板

文件下载

第一次登录送5个金币,绑定QQ送5个金币。金币可以用来观看视频和下载资源哦!

 
	 @RequestMapping(value = "/downfile", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	    @ResponseBody
	    public String  downfile(HttpSession session,HttpServletResponse response,MuMedia o,HttpServletRequest request) throws Exception {
	        o=mediaService.queryById(o.getId()); 
	        String f="";
	        if(o.getType().equals("audio"))f=o.getAudio_src();
	        if(o.getType().equals("video"))f=o.getVideo_src();
	        String format=f.substring(f.indexOf("."), f.length());
	        String abPath= Sys.Upimg.absolute_path+f;
			abPath=abPath.replace("/", "\\");
			 
			 
	      
	        String filepath = abPath;
	       
	        // 如果文件名不为空,则进行下载
	       
			File file = new File(filepath);
			// 如果文件存在,则进行下载
			if (file.exists()) {
			    // 配置文件下载
			    response.setHeader("content-type", "application/octet-stream");
			    response.setContentType("application/octet-stream");
			    // 下载文件能正常显示中文
			    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(o.getTitle()+format, "UTF-8"));
			    // 实现文件下载
			    byte[] buffer = new byte[1024];
			    FileInputStream fis = null;
			    BufferedInputStream bis = null;
			    try {
			        fis = new FileInputStream(file);
			        bis = new BufferedInputStream(fis);
			        OutputStream os = response.getOutputStream();
			        int i = bis.read(buffer);
			        while (i != -1) {
			            os.write(buffer, 0, i);
			            i = bis.read(buffer);
			        }
			        System.out.println("Download  successfully!");
			       
		        	return "1";
			    } catch (Exception e) {
	                    
	                	return "0";
			    } finally {
	                if (bis != null) {
	                    try {
	                        bis.close();
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	                if (fis != null) {
	                    try {
	                        fis.close();
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	            }
	        }else{
	        	 
	        	return "-1";
	        }
	         
	        
	    } 
在此处输入评论

允许提问者通过qq联系我

    现在绑定QQ

点击加载更多...