1
準(zhǔn)備3個(gè)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
導(dǎo)入3個(gè)jar包
image.png
MainActivity
package com.example.xeonrnc.upload_demo;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.apache.http.Header;
import java.io.File;
import java.io.FileNotFoundException;
public class MainActivity extends AppCompatActivity {
private Button upload_btn;
private Button choose_file;
public static String filePath;
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 {
//檢測(cè)是否有寫的權(quán)限
int permission = ActivityCompat.checkSelfPermission(activity,
"android.permission.WRITE_EXTERNAL_STORAGE");
if (permission != PackageManager.PERMISSION_GRANTED) {
// 沒有寫的權(quán)限讥裤,去申請(qǐng)寫的權(quán)限,會(huì)彈出對(duì)話框
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);
verifyStoragePermissions(this);
upload_btn=(Button)findViewById(R.id.upload_btn);
choose_file=(Button)findViewById(R.id.choose_file);
upload_btn.setOnClickListener(new ButtonClickListener());
choose_file.setOnClickListener(new ButtonClickListener());
}
class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.choose_file:
chooseFile();
break;
case R.id.upload_btn:
uploadFile();
break;
}
}
}
//選擇文件
public void chooseFile(){
Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent,1);
}
//選擇文件完成以后會(huì)觸發(fā)的onActivityResult方法
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
switch (requestCode){
case 1:
if(resultCode==RESULT_OK){
Uri uri=data.getData();
//如果API>=19
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
filePath=getRealPathFromUriAboveApi19(this,uri);
}else{
filePath=getRealPathFromUriBelowAPI19(this,uri);
}
Log.d("test","filePath: "+filePath);
TextView show_file_path=(TextView)findViewById(R.id.show_file_path);
show_file_path.setText("文件路徑:"+filePath);
}
break;
}
super.onActivityResult(requestCode,resultCode,data);
}
//點(diǎn)擊上傳按鈕會(huì)觸發(fā)的方法
public void uploadFile(){
AsyncHttpClient client=new AsyncHttpClient();
RequestParams params=new RequestParams();
try {
// String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/c.jpg";
// filePath="/storage/emulated/0/Pictures/hz.png";
params.put("ui.png",new File(filePath),"image/jpeg");
} catch (FileNotFoundException e){
e.printStackTrace();
}
//client.post參數(shù)解釋:如果用Fragment,那么把this改為getContext(), params 表示發(fā)請(qǐng)求的參數(shù)
//Upload_Servlet是服務(wù)器接收端servlet盈匾,使用url訪問這個(gè)servlet
client.post(this, "http://192.168.43.173:8080/my_upload/servlet/Upload_Servlet", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
System.out.println(new String(bytes));
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
System.out.println(new String(bytes));
}
});
}
//==========================================================================
//因?yàn)橹苯佑檬謾C(jī)自帶的文件管理器去選擇一個(gè)文件的話居砖,返回的并不是真實(shí)路徑
//所以下面的代碼只是用來獲取文件的真實(shí)路徑
private static String getRealPathFromUriBelowAPI19(Context context, Uri uri) {
return getDataColumn(context, uri, null, null);
}
private static String getRealPathFromUriAboveApi19(Context context, Uri uri) {
filePath = null;
if (DocumentsContract.isDocumentUri(context, uri)) {
// 如果是document類型的 uri, 則通過document id來進(jìn)行處理
String documentId = DocumentsContract.getDocumentId(uri);
if (isMediaDocument(uri)) { // MediaProvider
// 使用':'分割
String id = documentId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=?";
String[] selectionArgs = {id};
filePath = getDataColumn(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection, selectionArgs);
} else if (isDownloadsDocument(uri)) { // DownloadsProvider
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(documentId));
filePath = getDataColumn(context, contentUri, null, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())){
// 如果是 content 類型的 Uri
filePath = getDataColumn(context, uri, null, null);
} else if ("file".equals(uri.getScheme())) {
// 如果是 file 類型的 Uri,直接獲取圖片對(duì)應(yīng)的路徑
filePath = uri.getPath();
}
return filePath;
}
private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
String path = null;
String[] projection = new String[]{MediaStore.Images.Media.DATA};
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndexOrThrow(projection[0]);
path = cursor.getString(columnIndex);
}
} catch (Exception e) {
if (cursor != null) {
cursor.close();
}
}
return path;
}
private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
}
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"
android:orientation="vertical">
<Button
android:id="@+id/choose_file"
android:text="選擇文件"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/show_file_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/upload_btn"
android:text="上傳文件"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
配置AndroidManifest.xml
application添加屬性 android:usesCleartextTraffic="true"
添加聯(lián)網(wǎng)和sdcard讀寫權(quán)限
<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" />
防止android9閃退
<uses-library android:name="org.apache.http.legacy" android:required="false" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xeonrnc.upload_demo">
<!--允許聯(lián)網(wǎng)和sdcard讀寫-->
<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" />
<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>
<!--解決android9.0上傳閃退問題-->
<uses-library android:name="org.apache.http.legacy" android:required="false" />
</application>
</manifest>
3
服務(wù)器接收端servlet
新建一個(gè)web項(xiàng)目燕锥,項(xiàng)目名為my_upload
Upload_Servlet
package upload;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class Upload_Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Upload_Servlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload sfu=new ServletFileUpload(factory);
try{
List<FileItem> list=sfu.parseRequest(request);
for(FileItem fileItem:list){
if(fileItem.isFormField()){
String fieldName=fileItem.getFieldName();
if("description".equals(fieldName)){
String desc=new String(fileItem.getString().getBytes("UTF-8"),"GBK");
System.out.println("description="+desc);
}
}else {
String name=fileItem.getName();
// /pic 表示當(dāng)前項(xiàng)目所在的路徑下的pic文件夾的路徑
// 相當(dāng)于:
// C:\apache-tomcat-9.0.20\webapps\my_upload\ upload
String path=getServletContext().getRealPath("/upload");
System.out.println(path);
String extNameString=name.substring(name.lastIndexOf("."));
//隨機(jī)獲取uuid ,作為文件上傳到服務(wù)器以后的文件名
String uuid=UUID.randomUUID().toString();
//uuid+擴(kuò)展名
name=uuid+extNameString;
path=path+"/"+name;
System.out.println(path);
fileItem.write(new File(path));
}
}
}catch (Exception e) {
// TODO: handle exception
}
doGet(request, response);
}
}
web.xml (設(shè)置一下映射)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>upload</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Upload_Servlet</servlet-name>
<servlet-class>upload.Upload_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Upload_Servlet</servlet-name>
<url-pattern>/servlet/Upload_Servlet</url-pattern>
</servlet-mapping>
</web-app>
4
修改一下tomcat的訪問ip地址:
編輯 tomcat 的C:\apache-tomcat-9.0.20\conf 目錄下的 server.xml 文件
修改 為本機(jī)的 ip地址悯蝉,我這里用手機(jī)熱點(diǎn)
image.png
5
如果你的遠(yuǎn)程服務(wù)器,啟動(dòng)tomcat的情況下托慨,那么把項(xiàng)目打包成war包鼻由,發(fā)布到服務(wù)器的tomcat的webapps的目錄下就可以了,然后把IP地址改為公網(wǎng)ip