库和解析从输入流的multipart / form-data的的实例(Library and exa

2019-07-03 23:49发布

以一种HTTP请求我送的响应是一个多/表单数据看起来是这样的:

--------boundary123
Content-Disposition: form-data; name="json"
Content-Type: application/json

{"some":"json"}
--------boundary123
Content-Disposition: form-data; name="bin"
Content-Type: application/octet-stream

<file data>

--------boundary123

我一直在使用Apache来发送和接收HTTP请求,但我似乎无法找到用它来分析上面的表单字段的轻松访问一个简单的方法。

我宁愿不推倒重来,所以我正在寻找一个图书馆,让我做类似的东西:

MultipartEntity multipart = new MultipartEntity(inputStream);
InputStream bin = multipart.get("bin");

有什么建议?

Answer 1:

见Apache的百科全书文件上传

如果您在使用Spring MVC,看到这个http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch16s08.html



Answer 2:

使用弃用构造实施例的代码:

import java.io.ByteArrayInputStream;

import org.apache.commons.fileupload.MultipartStream;

public class MultipartTest {

    // Lines should end with CRLF
    public static final String MULTIPART_BODY =
            "Content-Type: multipart/form-data; boundary=--AaB03x\r\n"
            + "\r\n"
            + "----AaB03x\r\n"
            + "Content-Disposition: form-data; name=\"submit-name\"\r\n"
            + "\r\n"
            + "Larry\r\n"
            + "----AaB03x\r\n"
            + "Content-Disposition: form-data; name=\"files\"; filename=\"file1.txt\"\r\n"
            + "Content-Type: text/plain\r\n"
            + "\r\n"
            + "HELLO WORLD!\r\n"
            + "----AaB03x--\r\n";

    public static void main(String[] args) throws Exception {

        byte[] boundary = "--AaB03x".getBytes();

        ByteArrayInputStream content = new ByteArrayInputStream(MULTIPART_BODY.getBytes());

        @SuppressWarnings("deprecation")
        MultipartStream multipartStream =
                new MultipartStream(content, boundary);

        boolean nextPart = multipartStream.skipPreamble();
        while (nextPart) {
            String header = multipartStream.readHeaders();
            System.out.println("");
            System.out.println("Headers:");
            System.out.println(header);
            System.out.println("Body:");
            multipartStream.readBodyData(System.out);
            System.out.println("");
            nextPart = multipartStream.readBoundary();
        }
    }
}


Answer 3:

实施例的代码,而无需使用弃用方法。 进口com.google.common.net.MediaType; 进口org.apache.commons.fileupload.RequestContext;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

public class SimpleRequestContext implements RequestContext {
    private final Charset charset;
    private final MediaType contentType;
    private final byte[] content;

    public SimpleRequestContext(Charset charset, MediaType contentType, byte[] content) {
        this.charset = charset;
        this.contentType = contentType;
        this.content = content;
    }

    public String getCharacterEncoding() {
        return charset.displayName();
    }

    public String getContentType() {
        return contentType.toString();
    }

    @Deprecated
    public int getContentLength() {
        return content.length;
    }

    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(content);
    }
}

{
  ...
  Charset encoding = UTF_8;
  RequestContext requestContext = new SimpleRequestContext(encoding, contentType, body.getBytes());
  FileUploadBase fileUploadBase = new PortletFileUpload();
  FileItemFactory fileItemFactory = new DiskFileItemFactory();
  fileUploadBase.setFileItemFactory(fileItemFactory);
  fileUploadBase.setHeaderEncoding(encoding.displayName());
  List<FileItem> fileItems = fileUploadBase.parseRequest(requestContext);
  ...
}


文章来源: Library and examples of parsing multipart/form-data from inputstream