将文件上传到 Java 中没有临时文件的 HTTP 服务器内存?

Upload a file to an HTTP Server Memory in Java without temporal files?

如何在 Java 中不使用临时文件直接将文件上传到 HTTP 服务器内存? 我目前在 tomcat 上使用 Struts 2 和 apache ServletFileUpload,但我还没有找到实现此目的的方法。

在 apache 中,class FileUploadBase parseRequest(RequestContext ctx)... 正在创建临时文件

public List<FileItem> parseRequest(RequestContext ctx)
  ...
  Streams.copy(item.openStream(), fileItem.getOutputStream(), true);

基于此文档 here 我编写了以下 servlet 以在不创建临时文件的情况下上传到字节数组。

import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.IOUtils;
@WebServlet("/upload")
public class UploadByteArrayServlet extends HttpServlet {
    private static final long serialVersionUID = 1123445L;
    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 1; // 1MB
    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 1; // 1MB
    protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        byte[] bytes =  "".getBytes();
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if(isMultipart){
            ServletFileUpload upload = new ServletFileUpload();
            upload.setFileSizeMax(MAX_FILE_SIZE);
            upload.setSizeMax(MAX_REQUEST_SIZE);
            try {
                // Parse the request
                FileItemIterator iter = upload.getItemIterator(request);
                while (iter.hasNext()) {
                    FileItemStream item = iter.next();
                    String name = item.getFieldName();
                    InputStream stream = item.openStream();
                    if (item.isFormField()) {
                        System.out.println("Form field " + name + " with value "
                            + Streams.asString(stream) + " detected.");
                    } else {
                        System.out.println("File field " + name + " with file name "
                            + item.getName() + " detected.");
                        //loads only the first file
                        if(bytes.length==0)
                            bytes = IOUtils.toByteArray(item.openStream());
                    }
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            }       
        } else {
            response.getWriter().print("is not a multipart request");
            response.setStatus(200);
        }
        response.getWriter().print("bytes uploaded in file" + bytes.length);
        //TODO store in session etc...
    }    
}