zoukankan      html  css  js  c++  java
  • struts文件上传、文件下载

    文件上传

    如果在表单中上传文件,表单的enctype属性为multipart/form-data

    struts默认上传文件大小为2M,如果需要修改,在配置文件中设置

    <constant name="struts.multipart.maxSize" value="31457280"/>

    jsp页面

    <input type="file" name="file"/>

    action中属性

    private File file;
    private String fileContentType;
    private String fileFileName;

     如何设置上传文件后缀:

     文件下载

    listFile.action->listFile.jsp->downloadFile.action->

        public String listFile(){
            String path = ServletActionContext.getServletContext().getRealPath("upload");
            File file = new File(path);
            String[] list = file.list();
            request.setAttribute("fileList",list);
            return "list";
        }
        <c:forEach items="${fileList}" var="fileName" varStatus="vs">
            <tr>
                <td>${vs.count}</td>
                <td>${fileName}</td>
                <td>
                    <c:url var="url" value="/dept_downloadFile.action">
                        <c:param name="fileName" value="${fileName}"></c:param>
                    </c:url>
                    <a href="${url}">下载</a>
                </td>
            </tr>
        </c:forEach>
        public String downloadFile(){
            return "down";
        }
                <result name="down" type="stream">
                    <param name="contentType">application/octef-stream</param>
                    <param name="inputName">fileInputStream</param>
                    <param name="contentDisposition">attachment;filename=${downloadName}</param>
                    <param name="bufferSize">1024</param>
                </result>

    其中downloadName需要在action中给出相应的get方法,这里获取的是从页面传过来的fileName

        public String getDownloadName() {
            try {
                fileName =  URLEncoder.encode(fileName,"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return fileName;
        }
  • 相关阅读:
    Delphi 多线程知识
    程序员最后归宿是什么?30或35想转行?
    做技术的最终出路!
    路在何方?分析程序员人生之路
    一个垂直滚动的插件
    jQuery 动画中 缓动效果的应用
    [转]jQuery性能优化指南 I
    jQuery 标记当前函数 开始写一个简单的插件
    我发现我写的这俩函数太好用了~~
    jQuery浏览器版本判断
  • 原文地址:https://www.cnblogs.com/juaner767/p/5940277.html
Copyright © 2011-2022 走看看