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()]));
    }
    }

  • 相关阅读:
    [NHibernate]条件查询Criteria Query
    [JQuery]用InsertAfter实现图片走马灯展示效果
    [NHibernate]HQL查询
    [NHibernate]基本配置与测试
    [HTML/CSS]margin属性用法
    [HTML/CSS]盒子模型,块级元素和行内元素
    [Asp.net MVC]Asp.net MVC5系列——布局视图
    [c#基础]值类型和引用类型的Equals,==的区别
    用中间件实现读负载均衡的数据库群集
    论数据库连接池对中间件性能的重要性
  • 原文地址:https://www.cnblogs.com/justbeginning/p/3679185.html
Copyright © 2011-2022 走看看