1
准备3个jar包httpcore-4.4.3.jar,android-async-http-1.4.8.jar,httpclient-4.3.6.jar
链接:https://pan.baidu.com/s/1CK1NPfHBtppF-lxR-RcUmQ
密码:f16a
2
MainActivity
package com.example.xeonrnc.download_demo;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.FileAsyncHttpResponseHandler;
import org.apache.http.Header;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
//指定要下载的文件的url
String url="http://192.168.43.173:8080/my_upload/pic/22.png";
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
public static void verifyStoragePermissions(Activity activity) {
try {
//检测是否有写的权限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 没有写的权限,去申请写的权限,会弹出对话框
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.download);
verifyStoragePermissions(this);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadClick();
}
});
}
public void uploadClick(){
AsyncHttpClient client=new AsyncHttpClient();
client.get(url, new FileAsyncHttpResponseHandler(this) {
@Override
public void onFailure(int i, Header[] headers, Throwable throwable, File file) {
}
@Override
public void onSuccess(int i, Header[] headers, File file) {
System.out.println(file.getAbsolutePath());
//将下载的文件保存到本地sdcard的picture文件夹,并重命名为abc.png
File path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES+"/abc.png");
try {
InputStream in=new FileInputStream(file);
OutputStream out=new FileOutputStream(path);
byte[] bytes=new byte[100];
int len=-1;
while((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
out.close();
in.close();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
System.out.println();
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.xeonrnc.download_demo">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!--<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"-->
<!--tools:ignore="ProtectedPermissions" />-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="下载"
android:id="@+id/download"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>