zoukankan      html  css  js  c++  java
  • Struts2框架下的文件上传文件类型、名称约定

    Struts2框架下的文件上传机制:
    1.通过multipart/form-data form提交文件到服务器
    2.文件名是通过什么地方设置的?
    在strust2的FileUploadInterceptor中
    有下面这段代码,将参数写入写入到request的parameters中,再通过OGNL注入到Action中去。
    所以按照struts2约定,上传文件名以及上传文件类型和上传文件本身,都是以页面input的name来决定的。
    String[] fileName = multiWrapper.getFileNames(inputName);//得到请求的所有文件名

    if (isNonEmpty(fileName)) {
    // get a File object for the uploaded File
    File[] files = multiWrapper.getFiles(inputName);
    if (files != null && files.length > 0) {
    List<File> acceptedFiles = new ArrayList<File>(files.length);
    List<String> acceptedContentTypes = new ArrayList<String>(files.length);
    List<String> acceptedFileNames = new ArrayList<String>(files.length);
    String contentTypeName = inputName + "ContentType";//默认就是input名称+ContentType
    String fileNameName = inputName + "FileName";//默认就是input名称+FileName

    for (int index = 0; index < files.length; index++) {
    if (acceptFile(action, files[index], fileName[index], contentType[index], inputName, validation, ac.getLocale())) {
    acceptedFiles.add(files[index]);
    acceptedContentTypes.add(contentType[index]);
    acceptedFileNames.add(fileName[index]);
    }
    }

    if (!acceptedFiles.isEmpty()) {
    Map<String, Object> params = ac.getParameters();//添加到parameters中 这样就可以通过OGNL注入到action了

    params.put(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()]));
    params.put(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()]));
    params.put(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()]));
    }
    }

  • 相关阅读:
    SQL Server如何使用表变量
    Msys/MinGW与Cygwin/GCC(转)
    内存段划分:代码段、数据段、堆、栈
    Codeblocks+MinGW+wxWidgets搭建方法(转)
    Java GUI图形界面开发工具
    MinGW离线安装方法汇总(转)
    Linux系统的启动过程(转)
    详解VOLATILE在C++中的作用(转)
    C++虚函数与纯虚函数用法与区别(转)
    C++中值传递、指针传递和引用传递的比较 (转)
  • 原文地址:https://www.cnblogs.com/justbeginning/p/3679185.html
Copyright © 2011-2022 走看看