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

转载于:https://www.cnblogs.com/justbeginning/p/3679185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值