GenericPortlet - 独立于 FileUpload 平台

GenericPortlet - FileUpload platform independent

简短问题:如何在保持代码门户平台独立的 javax.portlet.GenericPortlet:serveResource 方法实现中处理文件上传?

长问题:我正在创建 javax.portlet.GenericPortlet 的实现。此 portlet 必须部署在 WebSphere Portal 和 Liferay Portal 上。在此 portlet 的 serveResource 方法中,我读取了一个 multipart/form-data 请求。据我所知,在 liferay 中我可以这样处理文件上传:

// save the request attached file from the client
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(resourceRequest);
String fileName = uploadRequest.getFileName(paramFile);
InputStream inputStream = uploadRequest.getFileAsStream(paramFile, true);

此代码片段的问题在于它依赖于 Liferay 门户,显然它将在另一个门户版本上爆发。

我看过图书馆 apache commons fileupload but I cannot figure out how to use it, particularly in the serveResource method. I found many examples with the file upload in javax.portlet.GenericPortlet:processAction,但 none 在 serveResource。 例如,我如何使用简单的

boolean isMultipart = PortletFileUpload.isMultipartContent(request);

在我的 serveResource 方法中?

最后我自己找到了解决办法。

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

public class MyPortlet extends GenericPortlet {

    @Override
    public void serveResource(ResourceRequest request, ResourceResponse resourceResponse) throws PortletException, IOException {

        final HttpServletRequest originalHttpServletRequest = portalService.getOriginalHttpServletRequest(request);
        final boolean multipartContent = FileUploadBase.isMultipartContent(new ServletRequestContext(originalHttpServletRequest));
        if (multipartContent) {
            // Create a factory for disk-based file items
            DiskFileItemFactory factory = new DiskFileItemFactory();
            // Set factory constraints
            factory.setSizeThreshold(yourMaxMemorySize);
            factory.setRepository(yourTempDirectory);
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            // Set overall request size constraint
            upload.setSizeMax(yourMaxRequestSize);

            List<FileItem> items = upload.parseRequest(originalHttpServletRequest);

            // Process the uploaded items
            Iterator<FileItem> iter = items.iterator();
            while (iter.hasNext()) {
                FileItem thisItem = (FileItem) iter.next();
                final String fieldName = thisItem.getFieldName();
                // if an element is a form field
                if (thisItem.isFormField()) {
                    if (fieldName.equals("yourfieldformparam")) {
                        String value = thisItem.getString();
                        // Do something with the value
                    }
                } else {
                    // if its an attachment you can do...
                    String fileName = item.getName();
                    String contentType = item.getContentType();
                    boolean isInMemory = item.isInMemory();
                    long sizeInBytes = item.getSize();

                    File fileOut = File.createTempFile(IMieiPagamentiConstants.FILE_PREFIX, null);
                    // delete on jvm exit
                    fileOut.deleteOnExit();
                    // write the file
                    thisItem.write(fileOut);

                    //...
                }
            }
        }
    }
}

portalService.getOriginalHttpServletRequest(request); 是一种为 ResourceRequest 提供 HttpServletRequest 下门户特定的方法。例如,在 Liferay 中:

@Override
    public HttpServletRequest getOriginalHttpServletRequest(PortletRequest request) {
        return PortalUtil.getOriginalServletRequest(getHttpServletRequest(request));
    }