MESSI19901025 2012-09-30 11:02
浏览 599
已采纳

struts2上传文件的时候,action里面获取上传文件的文件名和类型的原理是什么?

在jsp页面代码:

 

<s:file name="upload" label="上传的文件" />

 action中,声明代码:

 

private File upload;
private String uploadContentType; // 文件的内容类型
private String uploadFileName; // 上传文件名

这里有个疑问, 文件名和文件类型是如何获取的?

大家科普一下吧

  • 写回答

1条回答

  • jinnianshilongnian 2012-09-30 11:19
    关注

    约定,在struts2内部的的FileUploadInterceptor完成的

    String[] fileName = multiWrapper.getFileNames(inputName);[color=red]//得到请求的所有文件名[/color]

                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";[color=red]//默认就是input名称+ContentType[/color]
                        String fileNameName = inputName + "FileName";[color=red]//默认就是input名称+FileName[/color]
    
                        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()]));
                        }
                    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)