WEB项目中如何实现禁止下载文件(一)

前言:该WEB项目为Struts2(struts也可以)
实际背景:在视频点播网站或者其他资源观看及下载网站,要实现限制非法下载。
视频播放:
<object type="application/x-shockwave-flash" data="http://localhost:8080/video/flv/vcastr3.swf" width="555" height="430" id="vcastr3">
    <param name="movie" value="http://localhost:8080/video/flv/vcastr3.swf" />
    <param name="allowFullScreen" value="true" />
    <param name="FlashVars" value="xml=<vcastr>
        <plugIns>
            <javaScriptPlugIn>
                <url>http://localhost:8080/video/flv/javaScriptPlugIn.swf</url>
            </javaScriptPlugIn>
        </plugIns>
        <channel>
            <item>
                <source>1.flv</source>
            </item>
        </channel>
    </vcastr>" />
</object>

只要你知道对方的网址及看到这段页面源码,可以使用任意下载工具(如迅雷),建立任务就能下载

步骤一:在该项目的的WEB.xml下添加

<filter-mapping>
          <filter-name>struts</filter-name>
          <url-pattern>*.flv</url-pattern> 
</filter-mapping>

这样在播放页面是无法播放了 因为struts将flv作为请求拦截住了,但是找不到这个Action(但是迅雷还能下载,囧)

步骤二:

jsp:
......
             <item>
                <source>download.flv?id=1</source>
            </item>
......

struts.xml中:

<action name="download" class="com.download.action.DownloadAction">
            <result name="success" type="stream">
                <param name="contentType">flv-application/octet-stream</param>
                <param name="contentDisposition">file="1.flv"</param>
                <param name="inputName">DownloadFile</param>
            </result>
            <interceptor-ref name="defaultStack" />
 </action>

DownloadAction

......

public class DownloadAction extends _ExtendsAction {
    private Integer id;
   
    public Integer getid() {
        return videoId;
    }
    public void setid(Integer Id) {
        this.videoId = videoId;
    }

    public InputStream getDownloadFile()
   {
        HttpServletRequest request     = ServletActionContext.getRequest();
           
        User user = (User)request.getSession().getAttribute("User");
          
        if(user == null)
        return ServletActionContext.getServletContext().getResourceAsStream("/video
         /flv/"+"请不要非法下载.flv");
       
        else
        return ServletActionContext.getServletContext().getResourceAsStream("/video
         /flv/"+id+".flv");
    }
   
    public String execute() throws Exception
   {
            return "success";   
   }
......

到这里位置我们就实现了禁止文件下载的功能(迅雷下载只能下载到“请不要非法下载.flv”因为没有session)
实现原理:将文件(flv,rar)等资源文件的后缀名成为struts2的Action后缀,然后使用struts2的文件下载功能,这样我们就可以经过一系列的控制阻止非法的下载。

缺点:只能针对指定的资源文件进行拦截,如有不同资源软件,均要进行方法设置

WEB项目中如何实现禁止下载文件(二)

http://blog.csdn.net/linshaoxu123/article/details/7384350


如何获取本地缓存的视频

复制下面代码至txt文件并重命名为bat文件,可以读取IE缓存所有东西

echo off
cls
echo 请选择要提取的文件类型
echo    1(swf) 2(flv) 3(jpg) 4(gif) 5(mp3) 6(mid) 7(其它类型)
set /p type=
if %type%==1 (set    filetype=swf)
if %type%==2 (set    filetype=flv)
if %type%==3 (set    filetype=jpg)
if %type%==4 (set    filetype=gif)
if %type%==5 (set    filetype=mp3)
if %type%==6 (set    filetype=mid)
if %type%==7 (echo 请输入文件类型,例如bmp
set /p    filetype=)

for /f "tokens=1,2*" %%i in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"^|find /i "Cache"') do set "IEtmp=%%k"
::echo %userprofile%%IEtmp:~13% 从注册表里提取的缓存目录

:select_del_or_not
echo 提取后是否删除原文件?(y/n)
set /p delornot=
if %delornot%==y (goto startcopy)
if %delornot%==n (goto startcopy)
goto select_del_or_not
:startcopy
echo 正在提取文件,请稍候……
md %filetype%
for /f "delims=" %%i in ('dir "%userprofile%%IEtmp:~13%\*.%filetype%" /s/b') do copy /y "%%i" "%CD%\%filetype%\"
if %delornot%==y (for /f "delims=" %%i in ('dir "%userprofile%%IEtmp:~13%\*.%filetype%" /s/b') do del /f/q "%%i")
pause
goto startcopy

但是使用先前的拦截方法,提取文件都被重名名掉了,只有download.flv这个文件,除非你看一次再提取一次

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值