解决MultipartEntity无法获取内容数据问题:Multipart form entity does not implement #getContent()
来源:互联网 发布:骂人网络用语字母 编辑:程序博客网 时间:2024/06/10 21:26
最近在使用httppost和CloseableHttpClient进行文件上传的时候碰到了问题,就是MultipartEntity无法获取内容数据。报错提示如下:
Exception in thread "main" java.lang.UnsupportedOperationException: Multipart form entity does not implement #getContent()
at org.apache.http.entity.mime.MultipartFormEntity.getContent(MultipartFormEntity.java:92)
下面是MultipartEntity的源码展示:
The MultipartEntity.java example source code
/* * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */package org.apache.http.entity.mime;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.charset.Charset;import java.util.Iterator;import java.util.List;import java.util.Random;import org.apache.http.annotation.ThreadSafe;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.entity.mime.content.ContentBody;import org.apache.http.message.BasicHeader;import org.apache.http.protocol.HTTP;import org.apache.james.mime4j.field.Fields;import org.apache.james.mime4j.message.Message;/** * Multipart/form coded HTTP entity consisting of multiple * body parts. * * * @since 4.0 */@ThreadSafepublic class MultipartEntity implements HttpEntity { /** * The pool of ASCII chars to be used for generating a multipart boundary. */ private final static char[] MULTIPART_CHARS = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" .toCharArray(); private final Message message; private final HttpMultipart multipart; private final Header contentType; private long length; private volatile boolean dirty; // used to decide whether to recalculate length public MultipartEntity( HttpMultipartMode mode, final String boundary, final Charset charset) { super(); this.multipart = new HttpMultipart("form-data"); this.contentType = new BasicHeader( HTTP.CONTENT_TYPE, generateContentType(boundary, charset)); this.dirty = true; this.message = new Message(); org.apache.james.mime4j.message.Header header = new org.apache.james.mime4j.message.Header(); this.message.setHeader(header); this.multipart.setParent(message); if (mode == null) { mode = HttpMultipartMode.STRICT; } this.multipart.setMode(mode); this.message.getHeader().addField(Fields.contentType(this.contentType.getValue())); } public MultipartEntity(final HttpMultipartMode mode) { this(mode, null, null); } public MultipartEntity() { this(HttpMultipartMode.STRICT, null, null); } protected String generateContentType( final String boundary, final Charset charset) { StringBuilder buffer = new StringBuilder(); buffer.append("multipart/form-data; boundary="); if (boundary != null) { buffer.append(boundary); } else { Random rand = new Random(); int count = rand.nextInt(11) + 30; // a random size from 30 to 40 for (int i = 0; i < count; i++) { buffer.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]); } } if (charset != null) { buffer.append("; charset="); buffer.append(charset.name()); } return buffer.toString(); } public void addPart(final String name, final ContentBody contentBody) { this.multipart.addBodyPart(new FormBodyPart(name, contentBody)); this.dirty = true; } public boolean isRepeatable() { List<?> parts = this.multipart.getBodyParts(); for (Iterator<?> it = parts.iterator(); it.hasNext(); ) { FormBodyPart part = (FormBodyPart) it.next(); ContentBody body = (ContentBody) part.getBody(); if (body.getContentLength() < 0) { return false; } } return true; } public boolean isChunked() { return !isRepeatable(); } public boolean isStreaming() { return !isRepeatable(); } public long getContentLength() { if (this.dirty) { this.length = this.multipart.getTotalLength(); this.dirty = false; } return this.length; } public Header getContentType() { return this.contentType; } public Header getContentEncoding() { return null; } public void consumeContent() throws IOException, UnsupportedOperationException{ if (isStreaming()) { throw new UnsupportedOperationException( "Streaming entity does not implement #consumeContent()"); } } public InputStream getContent() throws IOException, UnsupportedOperationException { throw new UnsupportedOperationException( "Multipart form entity does not implement #getContent()"); } public void writeTo(final OutputStream outstream) throws IOException { this.multipart.writeTo(outstream); }}
使用 org.apache.http.entity.mime.MultipartEntity
writeTo(java.io.OutputStream) 方法把内容写入到一个 java.io.OutputStream中
, 然后把数据转换成 String
or byte[]
:
// import java.io.ByteArrayOutputStream;// import org.apache.http.entity.mime.MultipartEntity;// ...// MultipartEntity entity = ...;// ...ByteArrayOutputStream out = new ByteArrayOutputStream(entity.getContentLength());// write content to streamentity.writeTo(out);// either convert stream to stringString string = out.toString();// or convert stream to bytesbyte[] bytes = out.toByteArray();
Note: this only works for multipart entities both smaller than 2Gb, the maximum size of a byte array in Java, and small enough to be read into memory.
阅读全文
0 1
- 解决MultipartEntity无法获取内容数据问题:Multipart form entity does not implement #getContent()
- multipart/form-data 获取不到hidden数据
- 解决multipart/form-data上传的问题
- 解决当FORM的ENCTYPE="multipart/form-data" 时request.getParameter()获取不到值的问题
- springMvc 完美解决 multipart/form-data 方式提交请求 在 Filter 中 ServletRequest.getParameter方法 获取不到参数的问题
- springMvc 完美解决 multipart/form-data 方式提交请求 在 Filter 中 ServletRequest.getParameter方法 获取不到参数的问题
- springMvc 完美解决 multipart/form-data 方式提交请求 在 Filter 中 ServletRequest.getParameter方法 获取不到参数的问题
- MultipartEntity 和 UrlEncodedFormEntity || application/x-www-form-urlencoded 和 multipart/form-data
- Could not find class 'org.apache.http.entity.mime.MultipartEntity'
- post数据丢失问题 enctype="multipart/form-data"
- does not implement doesNotRecognizeSelector/methodSignatureForSelector
- The ServiceClass object does not implement the required method in the following form
- The ServiceClass object does not implement the required method in the following form: OMElement sayH
- The ServiceClass object does not implement the required method in the following form: OMElement...
- 简单解决 multipart/form-data 文件上传表单中 传递参数无法获取的办法! (解决办法:动态更改表单提交URL)
- 简单解决 multipart/form-data 文件上传表单中 传递参数无法获取的办法! (解决办法:动态更改表单提交URL)
- form enctype:"multipart/form-data",method:"post" 提交表单,后台获取不到数据
- FORM的ENCTYPE="multipart/form-data" 时request.getParameter()值为null问题的解决
- Scala学习六:Scala中的特殊字符
- softmax分类器
- EasyJSWebView二次加载无效的解决方案
- qt 消息处理机制深入分析(基于window平台)
- Android8
- 解决MultipartEntity无法获取内容数据问题:Multipart form entity does not implement #getContent()
- Android9
- springMVC系列(四)——springMVC拦截器
- iOS StoryBoard的奇淫巧技
- Android10
- 使用Hibernate,一对多和多对一的实现(双向).
- 趣文:Python程序员的进化史
- Android11
- google webRTC服务器搭建(CentOS7.3)