美文网首页优秀案例
使用http请求框架实现文件上传到服务器(Android-asy

使用http请求框架实现文件上传到服务器(Android-asy

作者: Xeon_CC | 来源:发表于2019-06-30 20:55 被阅读0次

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

导入3个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 {
            //检测是否有写的权限
            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);
        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);
    }
    //选择文件完成以后会触发的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);
    }
 
    //点击上传按钮会触发的方法
    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参数解释:如果用Fragment,那么把this改为getContext(),  params 表示发请求的参数
        //Upload_Servlet是服务器接收端servlet,使用url访问这个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));
            }
 
 
        });
 
    }
 
 
 
 
 
 
 
    //==========================================================================
    //因为直接用手机自带的文件管理器去选择一个文件的话,返回的并不是真实路径
    //所以下面的代码只是用来获取文件的真实路径
    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来进行处理
            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,直接获取图片对应的路径
            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"
添加联网和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" />

防止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">
    <!--允许联网和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

服务器接收端servlet
新建一个web项目,项目名为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 表示当前项目所在的路径下的pic文件夹的路径
                    //   相当于:
                    //  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("."));
                    //随机获取uuid ,作为文件上传到服务器以后的文件名
                    String uuid=UUID.randomUUID().toString();
                    //uuid+扩展名
                    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 (设置一下映射)

<?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 文件
修改 为本机的 ip地址,我这里用手机热点


image.png

5

如果你的远程服务器,启动tomcat的情况下,那么把项目打包成war包,发布到服务器的tomcat的webapps的目录下就可以了,然后把IP地址改为公网ip

相关文章

  • 使用http请求框架实现文件上传到服务器(Android-asy

    1 准备3个jar包httpcore-4.4.3.jar,android-async-http-1.4.8.jar...

  • c#中上传文件

    最近在项目中需要上传excel文件到服务器。由于之前使用 httphelper 框架来发送 http 请求和服务端...

  • mac 连接linux服务器常用命令

    mac连接linux服务器 scp命令实现本地文件、文件夹上传到 linux服务器 服务器上的文件、文件夹传输到本地

  • Node.js 之 http静态服务器实现

    http静态服务器实现 涉及模块 http path fs 静态服务器定义 能够根据需要请求的文件,原封不动...

  • Jmeter正则提取器的使用

    场景需求 Jmeter某次请求测试中需要使用上一次请求的数据。上传文件时,第一次请求将文件上传到服务器,并接收到服...

  • 三种服务器

    web服务器和应用服务器以及web应用框架: web服务器:负责处理http请求,响应静态文件,常见的有Apach...

  • 答辩常问问题文档

    如何实现前后台数据传输?使用http通信实现的了前后台数据传输,客户端发送http请求到服务器端,服务器端根据请求...

  • 网络03

    NSURLSession实现文件上传 实现文件上传的步骤 文件上传设置请求体的格式 使用NSURLSession上...

  • 2022-07-11 Android HttpURLConnec

    背景 Android预览相机获取相机数据流转成图片文件通过HTTP上传到服务器 方案 在AsyncTask中使用H...

  • 『SpringMVC』实现文件上传功能

    这两天个人项目,需要上传文件功能,我后台使用的SpringMVC框架进行请求转发,在此整理下功能实现过程。 想要上...

网友评论

    本文标题:使用http请求框架实现文件上传到服务器(Android-asy

    本文链接:https://www.haomeiwen.com/subject/qnficctx.html