Struts2 随笔1

配置拦截器
<interceptors>
                <interceptor-stack name="mystack">
                    <!-- 默认的拦截器 -->
                    <interceptor-ref name="struts-default"></interceptor-ref>
                    <!--查看每个运行action时间 -->
                    <interceptor-ref name="timer"></interceptor-ref>
                </interceptor-stack>
            </interceptors>
            <!-- 修改默认拦截器(要在action之前) -->
            <default-interceptor-ref name="mystack"></default-interceptor-ref>

 

在struts.xml文件中,有时候打一个 < 却没有提示,原因是因为没有连接互联网,访问不到"http://struts.apache.org/dtds/struts-2.3.dtd"这个路径
解决方法:1、连接到互联网,工具会自动下载使用
          2、在MyEclipse中配置一下
配置步骤如下:
Window---Preferences--MyEclipse Enterprise …. ---XML-----XML Catalog --
然后点击Add按钮,添加文件。Location 添加的是struts-2.3.dtd文件的路径,Key Type选择URL,KEY中的内容为http://struts.apache.org/dtds/struts-2.3.dtd,选择OK.
注意:在配置完成之后,struts.xml文件中可能会有一个红叉,按一下回车键,然后在保存一下就可以了

 

<!--公共的跳转 -->
    <global-results>
         <result name="logins" type="redirectAction">
            <param name="actionName">login</param>
              <!-- login?code=1001 -->
            <param name="code">1001</param>
          </result>
    </global-results>

 

文件下载
<action name="download" class="com.peng.action.UserAction" method="download">
                <result type="stream">
            //文件类型 <param name="contentType">${fileType}</param>
            //文件大小 <param name="contentLength">${fileLength}</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="contentCharSet">UTF-8</param>
            //指向getInputstream <param name="inputName">inputstream</param> </result> </action> //action   public String download() { return SUCCESS; } public InputStream getInputstream() throws FileNotFoundException { fileName = "zhangqingfeng.doc"; File file = new File("E:/Download/",fileName); fileType = map.get(file.getName().substring(file.getName().trim().lastIndexOf("."))); fileLength = file.length(); return new FileInputStream(file); }

 

//文件上传
<action name="upload" class="com.peng.action.UserAction" method="upload">
                <result type="redirectAction">
                    <param name="actionName">home</param>
                </result>
            </action>

//action
public String upload() throws Exception{
        System.out.println("Desc: " + desc);
        //获取文件名称为临时文件的名称
        System.out.println("Pic Name: " + pic.getName());
        System.out.println("Pic length :" + pic.length());
        //获取文件真正的文件名
        System.out.println("File Name:" + picFileName);
        System.out.println("文件类型:" + picContentType);
        
        InputStream input = new FileInputStream(pic);
        OutputStream out = new FileOutputStream(new File("E:/Download/",UUID.randomUUID().toString()+picFileName.substring(picFileName.lastIndexOf("."))));
        byte[] buffer =  new byte[1024]; 
        int len = -1;
        
        while ((len=input.read(buffer))!=-1) {
            out.write(buffer,0,len);
        }
        
        out.flush();
        out.close();
        input.close();
        return SUCCESS;
    }

 

转载于:https://www.cnblogs.com/fudapeng/p/3805396.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值