文件上传

如果在表单中上传文件,表单的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;
    }

 

 posted on 2016-10-08 21:41  十三弦  阅读(180)  评论(0编辑  收藏  举报