2012-11-19 29 views
7

Việc đối phó với một loại HTTP request tôi gửi là multipart/form-data trông giống như sau:Thư viện và ví dụ về phân tích cú pháp multipart/form-data từ inputstream

--------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 

Tôi đã sử dụng apache để gửi và nhận các yêu cầu HTTP, nhưng tôi dường như không thể tìm thấy cách dễ dàng để sử dụng nó để phân tích cú pháp trên để dễ dàng truy cập các trường biểu mẫu.

Tôi không muốn phát minh lại bánh xe, vì vậy tôi đang tìm một thư viện cho phép tôi làm điều gì đó tương tự như:

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

gợi ý Bất kỳ?

Trả lời

10

Ví dụ mã này sử dụng constructor chấp nhận:

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(); 
     } 
    } 
} 
+0

mã tốt tuy nhiên cho đến nay bạn chỉ in tiêu đề và nội dung dưới dạng chuỗi, anh ấy yêu cầu một cái gì đó như multipart.get ("bin"); – dendini

0

Ví dụ mã mà không sử dụng phương pháp phản đối. nhập com.google.common.net.MediaType; nhập 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); 
    ... 
} 
Các vấn đề liên quan