Java Apache HttpClient 使用 InputStream 上传文件时出错
Java Apache HttpClient error uploading files with InputStream
输入流也有同样的问题。您能否分享有关您的修复的更多详细信息。
谢谢,
戒日
link你的问题
扩展的代码org.apache.http.entity.mime.content.InputStreamBody
将是这样的。在创建 InputStreamBodyExtended
之前,您需要以某种方式计算正确的内容长度
public class InputStreamBodyExtended extends InputStreamBody {
private long contentLength = -1;
public InputStreamBodyExtended(InputStream in, String filename, long contentLength) {
super(in, filename);
this.contentLength = contentLength;
}
public InputStreamBodyExtended(InputStream in, ContentType contentType, long contentLength) {
super(in, contentType);
this.contentLength = contentLength;
}
public InputStreamBodyExtended(InputStream in, ContentType contentType,
String filename, long contentLength) {
super(in, contentType, filename);
this.contentLength = contentLength;
}
@Override
public long getContentLength() {
return contentLength;
}
}
另一个选项是org.apache.http.entity.mime.content.ByteArrayBody
,如果事先不知道大小是多少(!!!你必须确保输入流的内容适合JVM的内存):
InputStream inputStream = // get your input stream somehow
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
byte buff[] = new byte[4096];
while( -1 != (i = inputStream.read(buff))){
baos.write(buff, 0, i);
}
ByteArrayBody bab = new ByteArrayBody(baos.toByteArray(), "fileName1");
我是这样解决的。
public class CustomInputStreamBody extends InputStreamBody {
private InputStream inputStream;
private BufferedReader bufferedReader = null;
StringBuilder stringBuilder = null;
public CustomInputStreamBody(InputStream in,ContentType contentType){
super(in,contentType);
this.inputStream=in;
}
@Override
public long getContentLength() {
int length=0;
byte[] bytes=null;
try {
bytes = IOUtils.readBytesFromStream(inputStream);
// iterate to get the data and append in StringBuilder
System.out.println("___________"+bytes.length);
}catch (IOException ioe){
ioe.printStackTrace();
}
return bytes.length;
}
如果您知道您的 contentLength-
,还有另一种简单的方法可以覆盖 InputStreamBody.getContentLength
而无需创建我们自己的 ContentBody
实现
InputStreamBody inputStreamBody = new InputStreamBody(inputStream, ContentType.APPLICATION_OCTET_STREAM, fileName){
@Override
public long getContentLength(){return contentLength;}
};
MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addPart("dataAsStream", inputStreamBody)
.build();
输入流也有同样的问题。您能否分享有关您的修复的更多详细信息。
谢谢, 戒日
link你的问题
扩展的代码org.apache.http.entity.mime.content.InputStreamBody
将是这样的。在创建 InputStreamBodyExtended
public class InputStreamBodyExtended extends InputStreamBody {
private long contentLength = -1;
public InputStreamBodyExtended(InputStream in, String filename, long contentLength) {
super(in, filename);
this.contentLength = contentLength;
}
public InputStreamBodyExtended(InputStream in, ContentType contentType, long contentLength) {
super(in, contentType);
this.contentLength = contentLength;
}
public InputStreamBodyExtended(InputStream in, ContentType contentType,
String filename, long contentLength) {
super(in, contentType, filename);
this.contentLength = contentLength;
}
@Override
public long getContentLength() {
return contentLength;
}
}
另一个选项是org.apache.http.entity.mime.content.ByteArrayBody
,如果事先不知道大小是多少(!!!你必须确保输入流的内容适合JVM的内存):
InputStream inputStream = // get your input stream somehow
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
byte buff[] = new byte[4096];
while( -1 != (i = inputStream.read(buff))){
baos.write(buff, 0, i);
}
ByteArrayBody bab = new ByteArrayBody(baos.toByteArray(), "fileName1");
我是这样解决的。
public class CustomInputStreamBody extends InputStreamBody {
private InputStream inputStream;
private BufferedReader bufferedReader = null;
StringBuilder stringBuilder = null;
public CustomInputStreamBody(InputStream in,ContentType contentType){
super(in,contentType);
this.inputStream=in;
}
@Override
public long getContentLength() {
int length=0;
byte[] bytes=null;
try {
bytes = IOUtils.readBytesFromStream(inputStream);
// iterate to get the data and append in StringBuilder
System.out.println("___________"+bytes.length);
}catch (IOException ioe){
ioe.printStackTrace();
}
return bytes.length;
}
如果您知道您的 contentLength-
,还有另一种简单的方法可以覆盖InputStreamBody.getContentLength
而无需创建我们自己的 ContentBody
实现
InputStreamBody inputStreamBody = new InputStreamBody(inputStream, ContentType.APPLICATION_OCTET_STREAM, fileName){
@Override
public long getContentLength(){return contentLength;}
};
MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addPart("dataAsStream", inputStreamBody)
.build();