简短版 - org.apache...MultipartEntity
已弃用,其升级MultipartEntityBuilder
在我们的在线论坛中显示不足。我们来解决这个问题。如何注册回调,以便我的(Android)应用可以在上传文件时显示进度条?
长版 - 这是MultipartEntityBuilder
的“缺少简单的例子”:
public static void postFile(String fileName) throws Exception {
// Based on: https://stackoverflow.com/questions/2017414/post-multipart-request-with-android-sdk
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(SERVER + "uploadFile");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("file", new FileBody(new File(fileName)));
builder.addTextBody("userName", userName);
builder.addTextBody("password", password);
builder.addTextBody("macAddress", macAddress);
post.setEntity(builder.build());
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
// response.getStatusLine(); // CONSIDER Detect server complaints
entity.consumeContent();
client.getConnectionManager().shutdown();
} // FIXME Hook up a progress bar!
我们需要修复FIXME
。 (额外的好处是可中断的上传。)但是(请纠正我是否我错了),所有在线示例似乎都不尽如人意。
这个,http://pastebin.com/M0uNZ6SB,例如,将文件上传为“二进制/八位字节流”;不是“multipart / form-data”。我需要真正的领域。
此示例File Upload with Java (with progress bar)显示了如何覆盖*Entity
或*Stream
。那么也许我可以告诉MultipartEntityBuilder
到.create()
一个被覆盖的实体来计量其上传进度?
因此,如果我想覆盖某些东西,并用一个每1000字节发送一个信号的计数流替换内置流,也许我可以扩展FileBody
部分,并覆盖它的{{1} }和/或getInputStream
。
但是,当我尝试writeTo
时,我会得到臭名昭着的class ProgressiveFileBody extends FileBody {...}
。
因此,当我围绕我的java.lang.NoClassDefFoundError
文件进行探索,寻找缺失的Def时,有人可以检查我的数学,并指出一个我忽略的更简单的修复方法吗?
答案 0 :(得分:66)
获奖代码(以壮观的Java-Heresy(tm)风格)是:
public static String postFile(String fileName, String userName, String password, String macAddress) throws Exception {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(SERVER + "uploadFile");
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
final File file = new File(fileName);
FileBody fb = new FileBody(file);
builder.addPart("file", fb);
builder.addTextBody("userName", userName);
builder.addTextBody("password", password);
builder.addTextBody("macAddress", macAddress);
final HttpEntity yourEntity = builder.build();
class ProgressiveEntity implements HttpEntity {
@Override
public void consumeContent() throws IOException {
yourEntity.consumeContent();
}
@Override
public InputStream getContent() throws IOException,
IllegalStateException {
return yourEntity.getContent();
}
@Override
public Header getContentEncoding() {
return yourEntity.getContentEncoding();
}
@Override
public long getContentLength() {
return yourEntity.getContentLength();
}
@Override
public Header getContentType() {
return yourEntity.getContentType();
}
@Override
public boolean isChunked() {
return yourEntity.isChunked();
}
@Override
public boolean isRepeatable() {
return yourEntity.isRepeatable();
}
@Override
public boolean isStreaming() {
return yourEntity.isStreaming();
} // CONSIDER put a _real_ delegator into here!
@Override
public void writeTo(OutputStream outstream) throws IOException {
class ProxyOutputStream extends FilterOutputStream {
/**
* @author Stephen Colebourne
*/
public ProxyOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(int idx) throws IOException {
out.write(idx);
}
public void write(byte[] bts) throws IOException {
out.write(bts);
}
public void write(byte[] bts, int st, int end) throws IOException {
out.write(bts, st, end);
}
public void flush() throws IOException {
out.flush();
}
public void close() throws IOException {
out.close();
}
} // CONSIDER import this class (and risk more Jar File Hell)
class ProgressiveOutputStream extends ProxyOutputStream {
public ProgressiveOutputStream(OutputStream proxy) {
super(proxy);
}
public void write(byte[] bts, int st, int end) throws IOException {
// FIXME Put your progress bar stuff here!
out.write(bts, st, end);
}
}
yourEntity.writeTo(new ProgressiveOutputStream(outstream));
}
};
ProgressiveEntity myEntity = new ProgressiveEntity();
post.setEntity(myEntity);
HttpResponse response = client.execute(post);
return getContent(response);
}
public static String getContent(HttpResponse response) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
String content = "";
while ((body = rd.readLine()) != null)
{
content += body + "\n";
}
return content.trim();
}
# NOTE ADDED LATER: as this blasterpiece gets copied into various code lineages,
# The management reminds the peanut gallery that "Java-Heresy" crack was there
# for a reason, and (as commented) most of that stuff can be farmed out to off-
# the-shelf jar files and what-not. That's for the java lifers to tool up. This
# pristine hack shall remain obviousized for education, and for use in a pinch.
# What are the odds??
答案 1 :(得分:6)
不能感谢Phlip足够的解决方案。以下是添加进度条支持的最终内容。我在AsyncTask中运行它 - 下面的进展使您可以将进度发布回AsyncTask中的方法,该方法为在AsyncTask中运行的类调用AsyncTask.publishProgress()。进度条并非完全平滑,但至少它会移动。在三星S4上传一个4MB图像文件后,它正在移动4K块。
class ProgressiveOutputStream extends ProxyOutputStream {
long totalSent;
public ProgressiveOutputStream(OutputStream proxy) {
super(proxy);
totalSent = 0;
}
public void write(byte[] bts, int st, int end) throws IOException {
// FIXME Put your progress bar stuff here!
// end is the amount being sent this time
// st is always zero and end=bts.length()
totalSent += end;
progress.publish((int) ((totalSent / (float) totalSize) * 100));
out.write(bts, st, end);
}
答案 2 :(得分:2)
首先:非常感谢原来的问题/答案。由于HttpPost现在已弃用,我使用此article的其他输入重新设计了它,并创建了一个微型库:https://github.com/licryle/HTTPPoster
它将整个包装在ASync任务中;使用MultipartEntityBuilder& HttpURLConnection,让你听一听回调。
使用:
dependencies
{
compile project(':libs:HTTPPoster')
}
您需要一个类来实现HttpListener
接口,以便您可以收听回调。它在HTTPListener
中有四个回调:
配置ASyncTask&启动它。这是一个快速用法:
HashMap<String, String> mArgs = new HashMap<>();
mArgs.put("lat", "40.712784");
mArgs.put("lon", "-74.005941");
ArrayList<File> aFileList = getMyImageFiles();
HttpConfiguration mConf = new HttpConfiguration(
"http://example.org/HttpPostEndPoint",
mArgs,
aFileList,
this, // If this class implements HttpListener
null, // Boundary for Entities - Optional
15000 // Timeout in ms for the connection operation
10000, // Timeout in ms for the reading operation
);
new HttpPoster().execute(mConf);
希望可以帮助:)感觉也随意提出改进!它是最近的,我根据需要扩展它。
干杯