• Struts2中文件的上传与下载









    文件上传
    1.jsp页面
    <s:form action="fileAction" namespace="/file" method="POST" enctype="multipart/form-data">
        <!-- name为后台对应的参数名称 -->
        <s:file name="files" label="file1"></s:file>
        <s:file name="files" label="file2"></s:file>
        <s:file name="files" label="file3"></s:file>
        <s:submit value="提交" id="submitBut"></s:submit>
    </s:form>
    2.Action
    //单个文件上传可以用 File files,String filesFileName,String filesContentType
    //名称要与jsp中的name相同(三个变量都要生成get,set)
    private File[] files;
    // 要以File[]变量名开头
    private String[] filesFileName;
    // 要以File[]变量名开头
    private String[] filesContentType;


    private ServletContext servletContext;


    //Action调用的上传文件方法
    public String execute() {
          ServletContext servletContext = ServletActionContext.getServletContext();
          String dataDir = servletContext.getRealPath("/file/upload");
          System.out.println(dataDir);
          for (int i = 0; i < files.length; i++) {
                File saveFile = new File(dataDir, filesFileName[i]);
                files[i].renameTo(saveFile);
          }
          return "success";
    }
    3.配置上传文件临时文件夹(在struts.xml中配置)
    <constant name="struts.multipart.saveDir" value="c:/temp"/>
    文件下载
    1.下载的url(到Action)
    <a href="${pageContext.request.contextPath}/file/fileAction!down.action">下载</a>
    2.struts.xml配置
        <package name="file" namespace="/file" extends="struts-default">
            <action name="fileAction" class="com.struts2.file.FileAction">   
                <!-- 下载文件配置 -->
                <!--type 为 stream 应用 StreamResult 处理-->
                <result name="down" type="stream">
                    <!--
                            不管实际类型,待下载文件 ContentType 统一指定为 application/octet-stream 
                            默认为 text/plain
                    -->
                    <param name="contentType">application/octet-stream</param>
                    <!-- 
                            默认就是 inputStream,它将会指示 StreamResult 通过 inputName 属性值的 getter 方法,              
                            比如这里就是 getInputStream() 来获取下载文件的内容,意味着你的 Action 要有这个方法 
                    -->             
                    <param name="inputName">inputStream</param>
                    <!-- 
                            默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文 
                            件保有存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名,              
                            这里使用的是动态文件名,${fileName}, 它将通过 Action 的 getFileName() 获得文件名 
                    -->             
                    <param name="contentDisposition">attachment;filename="${fileName}"</param>
                    <!-- 输出时缓冲区的大小 -->
                    <param name="bufferSize">4096</param>
                </result>            
            </action>
        </package>
    3.Action
            //Action调用的下载文件方法
            public String down() {
                    return "down";
            }
            
            //获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容
            public InputStream getInputStream() throws Exception {
                    String dir = servletContext.getRealPath("/file/upload");
                    File file = new File(dir, "icon.png");
                    if (file.exists()) {
                            //下载文件
                            return new FileInputStream(file);
                            
                            //和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1
                            //将内容(Struts2 文件下载测试)直接写入文件,下载的文件名必须是文本(txt)类型
                            //return new ByteArrayInputStream("Struts2 文件下载测试".getBytes());
                    }
                    return null;
            }


            // 对于配置中的 ${fileName}, 获得下载保存时的文件名
            public String getFileName() {
                    String fileName ="图标.png";
                    try {
                            // 中文文件名也是需要转码为 ISO8859-1,否则乱码
                            return new String(fileName.getBytes(), "ISO8859-1");
                    } catch (UnsupportedEncodingException e) {
                            return "icon.png";
                    }
            }转载地址:http://code.google.com/p/j2eewiki/wiki/Struts2FileUpload



  • 相关阅读:
    5213 Exp3 免杀原理与实践
    20155213实验二 后门原理与实践
    20165110石钰网络对抗免考报告_WIFI破解+搭建钓鱼WIFI
    Exp9 Web安全基础 20165110
    Exp8:Web基础 20165110
    Exp7 网络欺诈防范 20165110
    Exp6 信息搜集与漏洞扫描 20165110
    Exp5 MSF基础应用 20165110
    Exp4 恶意代码分析 20165110
    Exp3 免杀原理与实践 20165110
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6470135.html
Copyright © 2020-2023  润新知