在项目中经常用到上传下载功能,Web Dynpro提供了两个控件,用于上传下载:FileUpload和FileDownload。
这两个控件对应的接口是:IWDFileUpload和IWDFileDownload。
下面说说这两个控件和他们的接口。
FileUpload和FileDownload。在Element Properties中,可以进行data和resource数据绑定,对应于IWDFileUpload中的方法是: bindData(IWDAttributeInfo attributeInfo)
bindData(String path)
bindResource(IWDAttributeInfo attributeInfo)
bindResource(String path)
bindData方法在NW2004S中已不推荐使用,推荐使用bindResource。bindData方法绑定的Attribute为binary类型(二进制),而bindResource方法绑定的Attribute为com.sap.ide.webdynpro.uielementdefinitions.Resource类型。
1·使用bindData方法:
第一步,设计时绑定控件Attribute,在控件初始化前,声明Attribute的类型
- wdContext.getNodeInfo().getAttribute("FileData").getModifiableSimpleType();
第二步,创建上传事件。
- onActionUploadFile{
- // get attribute info for context attribute "FileUpload"
- IWDAttributeInfo attInfo = wdContext.getNodeInfo().getAttribute(IPrivateFileUploadView.IContextElement.FILE_DATA);
- // get modifiable binary type from the attribute info
- // requires type cast
- IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) attInfo.getModifiableSimpleType();
- IPrivateFileUploadView.IContextElement element = wdContext.currentContextElement();
- //if a file in the FileUpload field exists
- if (element.getFileData() != null) {
- try {
- //获取文件类型
- String mimeType = binaryType.getMimeType().toString();
- //获取文件数据
- byte[] file = element.getFileData();
- //获取文件大小
- String fileSize = this.getFileSize(file));
- //获取文件扩展名
- String fileExtension = binaryType.getMimeType().getFileExtension();
- //文件名
- String fileName = binaryType.getFileName();
- } catch (Exception e) {
- throw new WDRuntimeException(e);
- }
- }
- private String getFileSize(byte[] file) {
- double size = file.length;
- String unit = "";
- if (size < 1024) {
- unit = " Bytes";
- } else if (size < 1048576) {
- size = size / 1024;
- unit = " KB";
- } else if (size < 1073741824) {
- size = size / 1024 / 1024;
- unit = " MB";
- }
- DecimalFormat myFormatter = new DecimalFormat("###.##");
- return myFormatter.format(size) + unit;
- }
- }
2·使用bindResource方法。
- onActionUploadFile{
- IPrivateFileUploadView.IContextElement element = wdContext.currentContextElement();
- // if a file in the FileUpload field exists
- if (element.getFileResource() != null) {
- //获取文件数据
- IWDResource resource = element.getFileResource();
- InputStream inst = resource.read(true);
- //获取文件类型
- String fileType = resource.getResourceType();
- //获取文件大小
- String fileSize = this.getFileSize(resource);
- //获取文件扩展名
- String fileExtension = resource.getResourceType().getFileExtension();
- //获取文件名
- String fileName = resource.getResourceName();
- //获取文件相对路径
- String filePath = resource.getUrl(0);
- }
- }
- private String getFileSize(IWDResource resource) {
- InputStream stream = null;
- DecimalFormat myFormatter = new DecimalFormat("###.##");
- double size = 0;
- String unit = "";
- try {
- stream = resource.read(false);
- size = stream.available();
- if (size < 1024) {
- unit = " Bytes";
- } else if (size < 1048576) {
- size = size / 1024;
- unit = " KB";
- } else if (size < 1073741824) {
- size = size / 1024 / 1024;
- unit = " MB";
- }
- } catch (IOException e) {
- throw new IOException(e);
- } finally {
- if (stream != null) {
- try {
- stream.close();
- } catch (IOException e) {
- throw new IOException(e);
- }
- }
- }
- return myFormatter.format(size) + unit;
- }
注意:
1·使用IWDResource 做文件上传时候,这个文件的生命周期是在当前的session内,当当前的session被销毁或者关闭时,上传的文件同时也被清除,他的getUrl方法所获取的地址已经不存在。其实IWDResource 上传的文件是放在WAS的公共目录下(app-当前应用pool)。
个人感觉这是WD2004S的一个BUG,看在07的版本中是否会做修改。
2·文件名的问题:
不管是用bindData还是用bindResource,在以上的方法中获取的文件名只局限于文件名是英文,如果文件名是中文,则bindResource方法是无法上传。而bindData提供了一个比较隐形的获取文件名的地方,就是在控件绑定数据Attribute的同时把Element Properties中的fileName也绑定一个FileNameAttribute,这个FileNameAttribute不需要手动设值,而是在触发上传事件的时候,框架自动设定上,不管是英文中文或者别的语言都没有问题。于是,可以轻而易举得到你想要的文件名。
了解了上传,相信下载也很简单了。