Struts 上传与下载 笔记

文件上传

Action类
/**
 * 文件上传Action
 * 
 * @author wangn
 *
 */
public class FileUPDown extends ActionSupport {
    private File files;// <input type="file" name="files">
    private String filesFileName;// 文件名
    private String FilesContentType;// 文件类型(MIME)

    public void setFiles(File files) {
        this.files = files;
    }

    public void setFilesFileName(String filesFileName) {
        this.filesFileName = filesFileName;
    }

    public void setFilesContentType(String filesContentType) {
        this.FilesContentType = filesContentType;
    }

    @Override
    public String execute() throws Exception {
        // 获取上传的目录路径
        // String path =
        // ServletActionContext.getServletContext().getRealPath("/upload");
        String path = "E://temp";
        // 创建目标文件对象
        File file = new File(path, filesFileName);
        // 把上传的文件,拷贝到目标文件中
        FileUtils.copyFile(files, file);
        return SUCCESS;
    }

}
struts.xml
<struts>
    <!-- 修改上传文件的最大大小为30M -->
    <constant name="struts.multipart.maxSize" value="31457280"/>
    <package name="file" extends="struts-default">
        <action name="file" class="com.ning.file.FileUPDown" method="execute">
            <!-- 限制允许上传的文件的类型 -->
            <interceptor-ref name="defaultStack">
                <!-- 限制允许的文件的扩展名 -->
                <param name="fileUpload.allowedExtensions">txt,jpg,jar</param>
                <!-- 限制允许的类型 【与上面同时使用,取交集】 <param name="fileUpload.allowedTypes">text/plain</param> -->
            </interceptor-ref>
            <result name="success">success.jsp</result>
            <!-- 文件上传出错返回input -->
            <result name="input">fileerror.jsp</result>
        </action>
    </package>
</struts>
jsp页面
<form action="<%=basePath%>file" method="post" enctype="multipart/form-data">
        <input type="file" name="files">
        <input type="submit" value="提交">
    </form>

文件下载

Action类
/**
 * 下载Action
 * @author wangn
 *
 */
public class Down extends ActionSupport {
    private String fileName;
    /**
     * 从GET方法提交的请求封装给fileName属性
     * @param fileName 传入的文件名
     */
    public void setFileName(String fileName) {
        // 处理传入的参数中问题(get提交)
        try {
            fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        // 把处理好的文件名,赋值
        this.fileName = fileName;
    }
    /**
     * 
     * @return result name
     */
    public String download() {
        return "download";
    }
    /**
     * 在file.xml中 param name="inputName">attrInputStream
     * @return 文件流
     */
    public InputStream getAttrInputStream(){
        return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);
    }
    /**
     * 下载显示的文件名(浏览器显示的文件名)
     * @return 文件名
     */
    public String getDownFileName() {
        // 需要进行中文编码
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return fileName;
    }
}
/**
输入流转输出流实现:例如POI实现下载excel
ByteArrayOutputStream   baos=new   ByteArrayOutputStream();
            workbook.write(baos);
            ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
            return swapStream;
**/
struts.xml
<package name="down" extends="struts-default">
        <action name="down" class="com.ning.file.Down" method="download">
            <result name="download" type="stream">
                <!-- 允许下载的文件的类型:指定为所有的二进制文件类型 -->
                <param name="contentType">application/octet-stream</param>
                <!-- 对应的是Action中属性: 返回流的属性【其实就是getAttrInputStream()】 -->
                <param name="inputName">attrInputStream</param>
                <!-- 下载头,包括:浏览器显示的文件名 -->
                <param name="contentDisposition">attachment;filename=${downFileName}</param>
                <!-- 缓冲区大小设置 -->
                <param name="bufferSize">1024</param>
            </result>
        </action>

    </package>
jsp页面
<body>
    <a href="<%=basePath%>down?fileName=a.txt">下载</a>
  </body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值