struts2学习之文件上传以及拦截文件类型(十二)

struts2文件上传
struts2基于拦截器实现,使用fileupload组件(apache)。

  1. 编写fileupload.jsp和success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<form action="upload" method="post" enctype="multipart/form-data">
    文件:<input type="file" name="newbeedaly"/><br/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
文件上传成功!
</body>
</html>

2.配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!-- 大文件上传 -->
    <constant name="struts.multipart.maxSize" value="20000000"></constant>

    <package name="manager" extends="struts-default">

        <action name="upload" class="com.newbeedaly.action.FileUploadAction">
            <result name="success">/success.jsp</result>
            <result name="input">/fileupload.jsp</result>
            <!-- 拦截文件类型 -->
            <!-- <interceptor-ref name="fileUpload">
                <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
                <param name="maximumSize">81101</param>
            </interceptor-ref>

            <interceptor-ref name="defaultStack"></interceptor-ref> -->
        </action>

    </package>

</struts>

3.编写FileUploadAction类

package com.newbeedaly.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private File newbeedaly; // 文件

    private String newbeedalyFileName;  // 文件名

    private String newbeedalyContentType;  // 文件类型

    public File getnewbeedaly() {
        return newbeedaly;
    }

    public void setnewbeedaly(File newbeedaly) {
        this.newbeedaly = newbeedaly;
    }

    public String getnewbeedalyFileName() {
        return newbeedalyFileName;
    }

    public void setnewbeedalyFileName(String newbeedalyFileName) {
        this.newbeedalyFileName = newbeedalyFileName;
    }

    public String getnewbeedalyContentType() {
        return newbeedalyContentType;
    }

    public void setnewbeedalyContentType(String newbeedalyContentType) {
        this.newbeedalyContentType = newbeedalyContentType;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("文件名:"+newbeedalyFileName);
        // 保存时注意要做文件类型的判断,不是需要的类型不能保存,后缀名自己手动写。
        // 原因:hack可通过抓包工具修改后缀名。
        System.out.println("文件类型:"+newbeedalyContentType);
        String filePath="E:/"+newbeedalyFileName;
        File saveFile=new File(filePath);
        FileUtils.copyFile(newbeedaly, saveFile);
        return SUCCESS;
    }




}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值