您的位置:首页 > 编程语言 > Java开发

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

2012-03-22 18:40 771 查看
前言:该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这个文件,除非你看一次再提取一次
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息