使用Struts2进行上传下载

文件上传的必要条件:
1: form表单的 method必须是post
2: form表单的 enctype必须是multipart/form-data
3: 提供 input type=”file”类型上传输入域

一:单文件上传

jsp

<s:fielderror/>
<s:actionerror/>
<form action="${pageContext.request.contextPath}/upload.action" method="post" enctype="multipart/form-data">
    姓名:<input type="text" name="username">
    头像:<input type="file" name="photo">
    <input type="submit" value="上传">
</form>

Action

public class SingleUploadAction extends ActionSupport {

    private String username;
    //必须是File类型,名字对应表单上的name
    private File photo;
    //上传文件名字固定写法:xxxFileName
    private String photoFileName;
    //文件类型:xxxContentType 上传文件的MIEM类型
    private String photoContentType;


    public String upload() throws IOException {

        System.out.println(username);
        System.out.println(photoContentType);

        //找到存放上传文件所在位置的真正路径
        ServletContext servletContext = ServletActionContext.getServletContext();
        String directory = servletContext.getRealPath("/file");

        //构建目标文件
        File target = new File(directory, photoFileName);

        //写入文件
        FileUtils.copyFile(photo, target);

        return SUCCESS;

    }

    public File getPhoto() {
        return photo;
    }

    public void setPhoto(File photo) {
        this.photo = photo;
    }

    public String getPhotoContentType() {
        return photoContentType;
    }

    public void setPhotoContentType(String photoContentType) {
        this.photoContentType = photoContentType;
    }

    public String getPhotoFileName() {
        return photoFileName;
    }

    public void setPhotoFileName(String photoFileName) {
        this.photoFileName = photoFileName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


}

struts.xml

<!--修改文件上传大小  10M -->
    <constant name="struts.multipart.maxSize" value="10485760"/>

<package name="upload" extends="struts-default">
        <action name="upload" class="com.lanou.struts.fileupload.SingleUploadAction" method="upload">

            <!--限制文件上传的类型只能是jpg和png-->
            <interceptor-ref name="defaultStack">
            <param name="fileUpload.allowedExtensions">jpg,png</param>
            </interceptor-ref>

            <result name="success" >/success.jsp</result>
            <result name="input">/singleFileUpload.jsp</result>
        </action>
</package>

二:下载

action

 //定义一个输入流,名字随意,in是不能用的
 private InputStream inputStream;
 private String fileName;


    public String download() throws IOException {

        //实现下载:给 inputStream 赋值
        String realPath = ServletActionContext.getServletContext().getRealPath("/喷雾.gif");
        //获取文件的名字
        fileName = filenameEncoding(FilenameUtils.getName(realPath),
                ServletActionContext.getRequest(),ServletActionContext.getResponse());

        inputStream = new FileInputStream(realPath);


        return SUCCESS;

    }

    //文件名中文适配
    public String filenameEncoding(String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String agent = request.getHeader("User-Agent"); //获取浏览器的类型    System.out.println(agent);
        if (agent.contains("Firefox")) {
            BASE64Encoder base64Encoder = new BASE64Encoder();
            filename = "=?utf-8?B?"                + base64Encoder.encode(filename.getBytes("utf-8"))
                    + "?=";
        } else if(agent.contains("MSIE")) {
            filename = URLEncoder.encode(filename, "utf-8");
        } else if (agent.contains("Safari")){
            filename = new String(filename.getBytes("UTF-8"),"ISO8859-1");
        }else {
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }

struts.xml

<package name="download" extends="struts-default">
        <action name="download" class="com.lanou.struts.down.DownLoadAction" method="download">
            <result name="success" type="stream">
                <!--指定动作类中的输入流,属性名-->
                <param name="inputName">inputStream</param>
                <!--通知浏览器,以下载的形式打开-->
                <param name="contentDisposition">attachment;filename=${fileName}</param>
            </result>
        </action>

    </package>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值