|
|

楼主 |
发表于 2013/8/22 13:38:33
|
显示全部楼层
PeopleCode 处理压缩文件
PeopleSoft中对文件附件的处理都是单个文件处理的,虽然在8.52版本新增了MAddAttachment(URLDestination, DirAndFilePrefix, Prompts, &UserFileArray, &ActualSizeArray, &DetailedReturnCodeArrayName [, MaxSize [, PreserveCase[, UploadPageTitle[, AllowLargeChunks[, StopOnError]]]]]) Function 实现了一次上传多个附件的功能,但是在下载附件的时候,还是只能单个下载,
这样就给客户的操作带来的很多不便,这篇文章来说明一下如何在PeopleCode中调用Java类来实现对文件的打包- REM ** The file I want to compress;
- Local string &fileNameToZip = "c:\temp\blah.txt";
- REM ** The internal zip file's structure -- internal location of blah.txt;
- Local string &zipInternalPath = "my/internal/zip/folder/structure";
- Local JavaObject &zip = CreateJavaObject("java.util.zip.ZipOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\temp\compressed.zip", True));
- Local JavaObject &file = CreateJavaObject("java.io.File", &fileNameToZip);
- REM ** We will read &fileNameToZip into a buffer and write it out to &zip;
- Local JavaObject &buf = CreateJavaArray("byte[]", 1024);
- Local number &byteCount;
- Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileNameToZip);
- Local JavaObject &zipEntry = CreateJavaObject("java.util.zip.ZipEntry", &zipInternalPath | "/" | &file.getName());
- REM ** Make sure zip entry retains original modified date;
- &zipEntry.setTime(&file.lastModified());
- &zip.putNextEntry(&zipEntry);
- &byteCount = &in.read(&buf);
- While &byteCount > 0
- &zip.write(&buf, 0, &byteCount);
- &byteCount = &in.read(&buf);
- End-While;
- &in.close();
- &zip.flush();
- &zip.close();
复制代码 这样就是实现了对文件的打包,不过为了代码重用,我们可以将这段代码写成一个Function- Function AddFileToZip(&zipInternalPath, &fileNameToZip, &zip)
- Local JavaObject &file = CreateJavaObject("java.io.File", &fileNameToZip);
- REM ** We will read &fileNameToZip into a buffer and write it out to &zip;
- Local JavaObject &buf = CreateJavaArray("byte[]", 1024);
- Local number &byteCount;
- Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileNameToZip);
- Local JavaObject &zipEntry = CreateJavaObject("java.util.zip.ZipEntry", &zipInternalPath | "/" | &file.getName());
- REM ** Make sure zip entry retains original modified date;
- &zipEntry.setTime(&file.lastModified());
- &zip.putNextEntry(&zipEntry);
- &byteCount = &in.read(&buf);
- While &byteCount > 0
- &zip.write(&buf, 0, &byteCount);
- &byteCount = &in.read(&buf);
- End-While;
- &in.close();
- End-Function;
复制代码 然后每次使用的时候,只需要调用这个Function就可以了。- Local JavaObject &zip = CreateJavaObject("java.util.zip.ZipOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\temp\compressed.zip", True));
- AddFileToZip("folder1", "c:\temp\file1.txt", &zip);
- AddFileToZip("folder1", "c:\temp\file2.txt", &zip);
- AddFileToZip("folder2", "c:\temp\file1.txt", &zip);
- AddFileToZip("folder2", "c:\temp\file2.txt", &zip);
- &zip.flush();
- &zip.close();
复制代码 OK,这样,就实现了我们的需求,希望这篇文章对大家有用。
|
|