如果添加reqEntity.addPart("file", fileBody);
它来到android 5+在android 4上的空reqEntity.addPart("user", user);
完美无缺。任何帮助都会有用。
try{
String TAG ="File UPL";
// the file to be posted
String textFile = _zipFile;
Log.v(TAG, "textFile: " + textFile);
// the URL where the file will be posted
String postReceiverUrl = "http://my.com/file.php";
Log.v(TAG, "postURL: " + postReceiverUrl);
// new HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
File fle = new File(textFile);
FileBody fileBody = new FileBody(fle);
StringBody user = new StringBody(prefs.getString(PrefsName.USER_NAME, ""));
StringBody pass = new StringBody(prefs.getString(PrefsName.USER_PASSWORD, ""));
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("user", user);
reqEntity.addPart("password", pass);
reqEntity.addPart("file", fileBody);
final HttpEntity yourEntity = reqEntity.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 {
long totalSent;
File f = new File(_zipFile);
public ProgressiveOutputStream(OutputStream proxy) {
super(proxy);
totalSent = 0;
}
public void write(byte[] bts, int st, int end) throws IOException {
new_progres = true;
// FIXME Put your progress bar stuff here!
totalSent += end;
publishProgress("" + (int) ((totalSent / (float) f.length()) * 100));
Log.e("up", st + "" + " - " + end);
out.write(bts, st, end);
}
}
yourEntity.writeTo(new ProgressiveOutputStream(outstream));
}
};
ProgressiveEntity myEntity = new ProgressiveEntity();
httpPost.setEntity(myEntity);
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
// you can add an if statement here and do other actions based on the response
if (responseStr.equals("file_not_found")){
RESPONSE = responseStr;
}else
if (responseStr.equals("file uploaded succeeded")) {
RESPONSE = responseStr;
}else
if(responseStr.equals("file upload failed")) {
RESPONSE = responseStr;
}
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}