我的功能
public static String postFile(String url,String fileName, String userName) throws Exception {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
final File file = new File(fileName);
ContentBody cfile=new FileBody(file);
builder.addPart("file",cfile);
builder.addTextBody("userName", userName);
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();
}
我的 gradle 依赖项
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile "org.apache.httpcomponents:httpmime:4.3.6"
compile "org.apache.httpcomponents:httpclient:4.3.6"
compile "org.apache.httpcomponents:httpcore:4.2.4"
}
构建运行错误消息:
Error:Execution failed for task ':app:packageDebug'.
APK META-INF/DEPENDENCIES 中复制的重复文件 文件1:/home/bekir/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar 文件2:/home/bekir/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar } } 安卓 { 包装选项{ 排除“META-INF/DEPENDENCIES” 您可以忽略 build.gradle 中的这些文件: 来源2:/home/bekir/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpmime/4.3.6/cf8bacbf0d476c7f2221f861269365b66447f7ec/httpmime-4.3.6.jar 来源1:/home/bekir/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar 存档路径:META-INF/DEPENDENCIES :app:package调试 错误:打包 APK /home/bekir/AndroidStudioProjects/uploadfile/app/build/outputs/apk/app-debug-unaligned.apk 期间出现重复文件 :应用程序:validateDebugSigning :app:processDebugJavaRes 最新 :app:dexDebug 最新 :app:preDexDebug 最新 :app:compileDebugJava 最新 :app:generateDebugSources 最新 :app:processDebugResources 最新 :app:processDebugManifest 最新 :app:mergeDebugResources 最新 :app:generateDebugResources 最新 :app:generateDebugResValues 最新 :app:mergeDebugAssets 最新 :app:generateDebugAssets 最新 :app:generateDebugBuildConfig 最新 :app:compileDebugRenderscript 最新 :app:compileDebugAidl 最新 :应用程序:准备调试依赖项 :app:prepareComAndroidSupportSupportV42103库最新 :app:prepareComAndroidSupportAppcompatV72103库最新 :app:preReleaseBuild :应用程序:预调试构建 :app:compileDebugNdk 最新 :app:检查调试清单 :应用程序:预构建
警告:发布时会忽略依赖项 org.apache.httpcomponents:httpclient:4.3.6,因为它可能与 Android 提供的内部版本冲突。 如果出现问题,请用jarjar重新打包,更改类包 如果出现问题,请用jarjar重新打包,更改类包 警告:依赖 org.apache.httpcomponents:httpclient:4.3.6 在发布时被忽略,因为它可能与 Android 提供的内部版本冲突。 如果出现问题,请用jarjar重新打包,更改类包 警告:调试时会忽略依赖项 org.apache.httpcomponents:httpclient:4.3.6,因为它可能与 Android 提供的内部版本冲突。 如果出现问题,请用jarjar重新打包,更改类包 信息:Gradle 任务 [:app:assembleDebug] 警告:调试时会忽略依赖项 org.apache.httpcomponents:httpclient:4.3.6,因为它可能与 Android 提供的内部版本冲突。
Error:Execution failed for task ':app:packageDebug'.
APK META-INF/DEPENDENCIES 中复制的重复文件 文件1:/home/bekir/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar 文件2:/home/bekir/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.3.3/f91b7a4aadc5cf486df6e4634748d7dd7a73f06d/httpcore-4.3.3.jar
对于重复文件错误,请使用:
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
}
它不仅可以使用 apache 的库来解决许多“重复文件” 并且依赖错误可以忽略。