zoukankan      html  css  js  c++  java
  • struts2之单文件上传(7)

    前台页面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>

    -->

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

    action

    public class UploadAction extends ActionSupport {
    //三个全局属性注意命名规则,属性名的前半部分保持一致,不然报空值
    //上传的文件(旧文件)
    private File myfile;
    //上传的文件名(旧文件)
    private String myfileFileName;
    //上传文件类型(旧文件)
    private String myfileFileContentType;


    //处理上传请求
    public String upload(){
    //生成新的文件名(使用uuid)
    String newmyfilename = UUIDUtil.getUUID()+myfileFileName.substring(myfileFileName.lastIndexOf("."));
    //指定上传的位置
    String path = ServletActionContext.getServletContext().getRealPath("upload");
    //上传文件的位置
    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();
    }


    return SUCCESS;
    }


    public File getMyfile() {
    return myfile;
    }


    public void setMyfile(File myfile) {
    this.myfile = myfile;
    }

    public String getMyfileFileContentType() {
    return myfileFileContentType;
    }


    public void setMyfileFileContentType(String myfileFileContentType) {
    this.myfileFileContentType = myfileFileContentType;
    }


    public String getMyfileFileName() {
    return myfileFileName;
    }


    public void setMyfileFileName(String myfileFileName) {
    this.myfileFileName = myfileFileName;
    }




    }

    struts.xml

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

    <!--读取国际化文件

      该常量用于读取国际化文件
      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>

  • 相关阅读:
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    网页排版中的浮动和定位(学习笔记)
    在html中,<input tyle = "text">除了text外还有几种种新增的表单元素
    初学者入门web前端:C#基础知识:函数
    初学者入门web前端 C#基础知识:数组与集合
    while/for循环
    jmeter http请求与参数化
    rpm -e --nodeps
  • 原文地址:https://www.cnblogs.com/love1/p/7808481.html
Copyright © 2011-2022 走看看