zoukankan      html  css  js  c++  java
  • struts2之多文件上传与拦截器(8)

    前台jsp

    <s:form action="uploadAction" enctype="multipart/form-data" method="post">
    <label>上传文件:</label><br/>
    <s:file name="myfiles"></s:file> <br/>
    <s:file name="myfiles"></s:file> <br/>
    <s:file name="myfiles"></s:file> <br/>
    <s:file name="myfiles"></s:file> <br/>
    <s:submit value="提交"></s:submit>
    </s:form>

    action

    public class UploadAction extends ActionSupport {
    //三个全局属性注意命名规则,属性名的前半部分保持一致,不然报空值
    //上传的文件(旧文件)
    private File[] myfiles;
    //上传的文件名(旧文件)
    private String[] myfilesFileName;
    //上传文件类型(旧文件)
    private String[] myfilesContentType;
    //封装上传方法
    public void copy(File myfile,String myfileFileName,String path){
    //生成新的文件名(使用uuid)
    String newmyfilename = UUIDUtil.getUUID()+myfileFileName.substring(myfileFileName.lastIndexOf("."));

    //上传文件的位置
    String filepath = path+File.pathSeparator+newmyfilename;
    System.out.println("filepath = "+filepath);
    //构建新文件
    File newfile = new File(filepath);

    //读入写出 从旧文件读内容到新文件
    FileInputStream fis = null;
    FileOutputStream fos = null;

    try {
    //将旧文件封装到输入流
    fis = new FileInputStream(myfile);
    //将新文件封装到输出流
    fos = new FileOutputStream(newfile);
    //设置一个字节数组缓冲内容
    byte [] bt = new byte[1024];
    int len = 0;
    /**
    * 循环读取缓冲区的内容
    * 输入流不断的将字节读入到缓冲区(旧文件到缓冲区)
    * 输出流不断的将字节写出到新文件(缓冲区到新文件)
    */
    while((len = fis.read(bt))!=-1){
    fos.write(bt, 0, len);
    }
    fos.close();
    fis.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    //处理上传请求
    public String upload(){
    //指定上传的位置(因为只有一个,所以不用再循环)
    String path = ServletActionContext.getServletContext().getRealPath("upload");
    for (int i = 0; i < myfiles.length; i++) {
    copy(myfiles[i], myfilesFileName[i], path);

    }


    return SUCCESS;
    }


    public File[] getMyfiles() {
    return myfiles;
    }


    public void setMyfiles(File[] myfiles) {
    this.myfiles = myfiles;
    }


    public String[] getMyfilesFileName() {
    return myfilesFileName;
    }


    public void setMyfilesFileName(String[] myfilesFileName) {
    this.myfilesFileName = myfilesFileName;
    }


    public String[] getMyfilesContentType() {
    return myfilesContentType;
    }


    public void setMyfilesContentType(String[] myfilesContentType) {
    this.myfilesContentType = myfilesContentType;
    }




    }

    struts.xml

    <!-- struts2中文件上传拦截
    struts2 的核心包下的default.properties文件里有默认的大小为struts.multipart.maxSize=2097152,也就是2M. 这是struts2默认拦截,
    解决方法:在struts.xml配置文件中,添加
    <constant name="struts.multipart.maxSize" value="10485760"/>
    这里的value单位为B,即10485760B = 100MB。
    -->
    <constant name="struts.multipart.maxSize" value="104857600"/>

    <!-- 该常量用于读取国际化文件
    name表示国际化资源
    value表示国际化文件所在位置,
    注意:国际化文件不要写后缀 -->


    <constant name="struts.custom.i18n.resources" value="com.oak.action.myUpload"></constant>
    <package name="upload" namespace="/" extends="struts-default">
    <action name="uploadAction" class="com.oak.action.UploadAction" method="upload">
    <!-- fileUpload拦截器是系统拦截器,只需要引用,单词是固定的 -->
    <interceptor-ref name="fileUpload">
    <!-- 允许用户上传文件的大小,单位是字节 10M -->
    <param name="maximumSize">10485760</param>
    <!-- 允许用户上传文件的扩展名,如果不设置,则不受限,多个可以以逗号分隔 -->
    <param name="allowedExtensions">.jpg,.txt,.jsp</param>
    </interceptor-ref>
    <!--还需要引用系统默认的拦截器-->
    <interceptor-ref name="validationWorkflowStack"></interceptor-ref>
    <interceptor-ref name="basicStack"></interceptor-ref>
    <result>
    /welcome.jsp
    </result>
    <!-- 用于输出错误信息到页面 -->
    <result name="input">
    /upload.jsp
    </result>
    </action>
    </package>

  • 相关阅读:
    最小路径和
    S2 深入.NET和C#编程 机试测试错题积累
    S2 深入.NET和C#编程 笔试测试错题积累
    影院项目的内容信息
    抽象类和抽象的方法注意事项
    六种设计原则
    体检套餐的笔记
    C#图解 类和继承
    深入类的方法
    S2 深入.NET和C#编程 三:使用集合组织相关数据
  • 原文地址:https://www.cnblogs.com/love1/p/7809369.html
Copyright © 2011-2022 走看看