http方式上传:
package com.example.chapter14.util;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpUtil {
private final static String TAG = "HttpUtil";
private final static int CONNECT_TIMEOUT = 15000;
private final static int READ_TIMEOUT = 15000;
// 兼容https开头的调用地址
private static void compatibleSSL(String callUrl) throws Exception {
if (callUrl.toLowerCase().startsWith("https")) {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
}
}
// 对指定接口地址发起GET调用
public static String get(String callUrl, Map<String, String> headers) {
String resp = ""; // 应答内容
try {
Log.d(TAG, "请求地址:"+callUrl);
compatibleSSL(callUrl); // 兼容https开头的调用地址
URL url = new URL(callUrl); // 根据网址字符串构建URL对象
// 打开URL对象的网络连接,并返回HttpURLConnection连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); // 设置请求方式
setConnHeader(conn, headers);// 设置HTTP连接的头部信息
conn.connect(); // 开始连接
// 打印HTTP调用的应答内容长度、内容类型、压缩方式
Log.d(TAG, String.format("应答内容长度=%d, 内容类型=%s, 压缩方式=%s",
conn.getContentLength(), conn.getContentType(), conn.getContentEncoding()) );
// 对输入流中的数据解压和字符编码,得到原始的应答字符串
resp = getUnzipString(conn);
// 打印HTTP调用的应答状态码和应答报文
Log.d(TAG, String.format("应答状态码=%d, 应答报文=%s", conn.getResponseCode(), resp) );
conn.disconnect(); // 断开连接
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
// 从指定url获取图片
public static Bitmap getImage(String callUrl, Map<String, String> headers) {
Bitmap bitmap = null; // 位图对象
try {
Log.d(TAG, "请求地址:"+callUrl);
compatibleSSL(callUrl); // 兼容https开头的调用地址
URL url = new URL(callUrl); // 根据网址字符串构建URL对象
// 打开URL对象的网络连接,并返回HttpURLConnection连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); // 设置请求方式
setConnHeader(conn, headers);// 设置HTTP连接的头部信息
conn.connect(); // 开始连接
// 打印图片获取的应答内容长度、内容类型、压缩方式
Log.d(TAG, String.format("应答内容长度=%d, 内容类型=%s, 压缩方式=%s",
conn.getContentLength(), conn.getContentType(), conn.getContentEncoding()) );
// 对输入流中的数据解码,得到位图对象
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
// 打印图片获取的应答状态码和位图大小
Log.d(TAG, String.format("应答状态码=%d, 位图大小=%s", conn.getResponseCode(), bitmap.getByteCount()) );
conn.disconnect(); // 断开连接
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
// 对指定接口地址发起POST调用
public static String post(String callUrl, String req, Map<String, String> headers) {
String resp = ""; // 应答内容
try {
Log.d(TAG, "请求地址:"+callUrl+", 请求报文="+req);
compatibleSSL(callUrl); // 兼容https开头的调用地址
URL url = new URL(callUrl); // 根据网址字符串构建URL对象
// 打开URL对象的网络连接,并返回HttpURLConnection连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST"); // 设置请求方式
setConnHeader(conn, headers);// 设置HTTP连接的头部信息
conn.setRequestProperty("Content-Type", "application/json"); // 请求报文为json格式
conn.setDoOutput(true); // 准备让连接执行输出操作。默认为false,POST方式需要设置为true
//conn.setDoInput(true); // 准备让连接执行输入操作。默认为true
conn.connect(); // 开始连接
OutputStream os = conn.getOutputStream(); // 从连接对象中获取输出流
os.write(req.getBytes()); // 往输出流写入请求报文
// 打印HTTP调用的应答内容长度、内容类型、压缩方式
Log.d(TAG, String.format("应答内容长度=%s, 内容类型=%s, 压缩方式=%s",
conn.getHeaderField("Content-Length"), conn.getHeaderField("Content-Type"),
conn.getHeaderField("Content-Encoding")) );
// 对输入流中的数据解压和字符编码,得到原始的应答字符串
resp = getUnzipString(conn);
// 打印HTTP调用的应答状态码和应答报文
Log.d(TAG, String.format("应答状态码=%d, 应答报文=%s", conn.getResponseCode(), resp) );
conn.disconnect(); // 断开连接
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
// 把文件上传给指定的URL
public static String upload(String uploadUrl, String uploadFile, Map<String, String> headers) {
String resp = ""; // 应答内容
// 从本地文件路径获取文件名
String fileName = uploadFile.substring(uploadFile.lastIndexOf("/"));
String end = "\r\n"; // 结束字符串
String hyphens = "--"; // 连接字符串
String boundary = "WUm4580jbtwfJhNp7zi1djFEO3wNNm"; // 边界字符串
try (FileInputStream fis = new FileInputStream(uploadFile)) {
Log.d(TAG, "上传地址:"+uploadUrl+", 上传文件="+uploadFile);
compatibleSSL(uploadUrl); // 兼容https开头的调用地址
URL url = new URL(uploadUrl); // 根据网址字符串构建URL对象
// 打开URL对象的网络连接,并返回HttpURLConnection连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST"); // 设置请求方式
setConnHeader(conn, headers);// 设置HTTP连接的头部信息
conn.setDoOutput(true); // 准备让连接执行输出操作。默认为false,POST方式需要设置为true
//conn.setDoInput(true); // 准备让连接执行输入操作。默认为true
conn.setRequestProperty("Connection", "Keep-Alive"); // 连接过程要保持活跃
// 请求报文要求分段传输,并且各段之间以边界字符串隔开
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// 根据连接对象的输出流构建数据输出流
DataOutputStream ds = new DataOutputStream(conn.getOutputStream());
// 以下写入请求报文的头部
ds.writeBytes(hyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; "
+ "name=\"file\";filename=\"" + fileName + "\"" + end);
ds.writeBytes(end);
// 以下写入请求报文的主体
byte[] buffer = new byte[1024];
int length;
// 先将文件数据写入到缓冲区,再将缓冲数据写入输出流
while ((length = fis.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
// 以下写入请求报文的尾部
ds.writeBytes(hyphens + boundary + hyphens + end);
ds.close(); // 关闭数据输出流
// 打印HTTP调用的应答内容长度、内容类型、压缩方式
Log.d(TAG, String.format("应答内容长度=%s, 内容类型=%s, 压缩方式=%s",
conn.getHeaderField("Content-Length"), conn.getHeaderField("Content-Type"),
conn.getHeaderField("Content-Encoding")) );
// 对输入流中的数据解压和字符编码,得到原始的应答字符串
resp = getUnzipString(conn);
// 打印HTTP上传的应答状态码和应答报文
Log.d(TAG, String.format("应答状态码=%d, 应答报文=%s", conn.getResponseCode(), resp) );
conn.disconnect(); // 断开连接
} catch (Exception e) {
e.printStackTrace();
}
return resp;
}
// 设置HTTP连接的头部信息
private static void setConnHeader(HttpURLConnection conn, Map<String, String> headers) {
conn.setConnectTimeout(CONNECT_TIMEOUT); // 设置连接的超时时间,单位毫秒
conn.setReadTimeout(READ_TIMEOUT); // 设置读取应答数据的超时时间,单位毫秒
conn.setRequestProperty("Accept", "*/*"); // 设置数据格式
conn.setRequestProperty("Accept-Language", "zh-CN"); // 设置文本语言
conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); // 设置编码格式
// 设置用户代理,包括操作系统版本、浏览器版本等等
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0");
if (headers != null) {
for (Map.Entry<String, String> item : headers.entrySet()) {
conn.setRequestProperty(item.getKey(), item.getValue());
}
}
}
// 把输入流中的数据按照指定字符编码转换为字符串。处理大量数据需要使用本方法
private static String isToString(InputStream is, String charset) {
String result = "";
// 创建一个字节数组的输出流对象
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
int i = -1;
while ((i = is.read()) != -1) { // 循环读取输入流中的字节数据
baos.write(i); // 把字节数据写入字节数组输出流
}
byte[] data = baos.toByteArray(); // 把字节数组输出流转换为字节数组
result = new String(data, charset); // 将字节数组按照指定的字符编码生成字符串
} catch (Exception e) {
e.printStackTrace();
}
return result; // 返回转换后的字符串
}
// 从HTTP连接中获取已解压且重新编码后的应答报文
private static String getUnzipString(HttpURLConnection conn) throws IOException {
String contentType = conn.getContentType(); // 获取应答报文的内容类型(包括字符编码)
String charset = "UTF-8"; // 默认的字符编码为UTF-8
if (contentType != null) {
if (contentType.toLowerCase().contains("charset=gbk")) { // 应答报文采用gbk编码
charset = "GBK"; // 字符编码改为GBK
} else if (contentType.toLowerCase().contains("charset=gb2312")) { // 采用gb2312编码
charset = "GB2312"; // 字符编码改为GB2312
}
}
String contentEncoding = conn.getContentEncoding(); // 获取应答报文的压缩方式
InputStream is = conn.getInputStream(); // 获取HTTP连接的输入流对象
String result = "";
if (contentEncoding != null && contentEncoding.contains("gzip")) { // 应答报文使用gzip压缩
// 根据输入流对象构建压缩输入流
try (GZIPInputStream gis = new GZIPInputStream(is)) {
// 把压缩输入流中的数据按照指定字符编码转换为字符串
result = isToString(gis, charset);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 把输入流中的数据按照指定字符编码转换为字符串
result = isToString(is, charset);
}
return result; // 返回处理后的应答报文
}
}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_file_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择待上传的图片"
android:textColor="@color/black"
android:textSize="17sp" />
<TextView
android:id="@+id/tv_file_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
UrlConstant
package com.example.myapplication.constant;
public class UrlConstant
{
// 以下是HTTP接口调用的服务地址,根据实际情况修改IP和端口
public static final String REQUEST_URL = "https://dldir1.qq.com/weixin/Windows/WeChatSetup.exe";
//public static final String REQUEST_URL = "http://192.168.1.2:6001/NetServer";
// 检查应用更新的服务地址
public static final String CHECK_UPDATE_URL = REQUEST_URL;
// 上传文件的服务地址
public static final String UPLOAD_URL = REQUEST_URL + "/uploadServlet";
// 获取旅游图片的服务地址
public static final String GET_PHOTO_URL = REQUEST_URL + "/getPhoto";
// 根据经纬度查询详细地址的网址(天地图)
public static final String GET_ADDRESS_URL = "https://api.tianditu.gov.cn/geocoder?postStr={'lon':%f,'lat':%f,'ver':1}&type=geocode&tk=145897399844a50e3de2309513c8df4b";
// 请求图片验证码的网址
public static final String IMAGE_CODE_URL = "http://yx12.fjjcjy.com/Public/Control/GetValidateCode?time=";
}
DateUtil
package com.example.myapplication.util;
import android.annotation.SuppressLint;
import android.text.TextUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
@SuppressLint("SimpleDateFormat")
public class DateUtil {
// 获取当前的日期时间
public static String getNowDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
return sdf.format(new Date());
}
// 获取当前的时间
public static String getNowTime() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(new Date());
}
// 获取当前的时间(精确到毫秒)
public static String getNowTimeDetail() {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
return sdf.format(new Date());
}
// 获取指定格式的日期时间
public static String getNowDateTime(String formatStr) {
String format = formatStr;
if (TextUtils.isEmpty(format)) {
format = "yyyyMMddHHmmss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
public static String formatDate(long time) {
Date date = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
// 重新格式化日期字符串
public static String convertDateString(String strTime) {
String newTime = strTime;
try {
SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = oldFormat.parse(strTime);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
newTime = newFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return newTime;
}
}
FileUtil
package com.example.myapplication.util;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class FileUtil {
private final static String TAG = "FileUtil";
// 把字符串保存到指定路径的文本文件
public static void saveText(String path, String txt) {
// 根据指定的文件路径构建文件输出流对象
try (FileOutputStream fos = new FileOutputStream(path)) {
fos.write(txt.getBytes()); // 把字符串写入文件输出流
} catch (Exception e) {
e.printStackTrace();
}
}
// 从指定路径的文本文件中读取内容字符串
public static String openText(String path) {
String readStr = "";
// 根据指定的文件路径构建文件输入流对象
try (FileInputStream fis = new FileInputStream(path)) {
byte[] b = new byte[fis.available()];
fis.read(b); // 从文件输入流读取字节数组
readStr = new String(b); // 把字节数组转换为字符串
} catch (Exception e) {
e.printStackTrace();
}
return readStr; // 返回文本文件中的文本字符串
}
// 把位图数据保存到指定路径的图片文件
public static void saveImage(String path, Bitmap bitmap) {
// 根据指定的文件路径构建文件输出流对象
try (FileOutputStream fos = new FileOutputStream(path)) {
// 把位图数据压缩到文件输出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
} catch (Exception e) {
e.printStackTrace();
}
}
// 从指定路径的图片文件中读取位图数据
public static Bitmap openImage(String path) {
Bitmap bitmap = null; // 声明一个位图对象
// 根据指定的文件路径构建文件输入流对象
try (FileInputStream fis = new FileInputStream(path)) {
// 从文件输入流中解码位图数据
bitmap = BitmapFactory.decodeStream(fis);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap; // 返回图片文件中的位图数据
}
// 检查文件是否存在,以及文件路径是否合法
public static boolean checkFileUri(Context ctx, String path) {
boolean result = true;
File file = new File(path);
if (!file.exists() || !file.isFile() || file.length() <= 0) {
result = false;
}
try
{
Uri uri = Uri.parse(path); // 根据指定路径创建一个Uri对象
// 兼容Android7.0,把访问文件的Uri方式改为FileProvider
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
// // 通过FileProvider获得文件的Uri访问方式
// uri = FileProvider.getUriForFile(ctx,
// ctx.getPackageName()+".fileProvider", new File(path));
}
}
catch (Exception e) // 该路径可能不存在
{
e.printStackTrace();
result = false;
}
return result;
}
// 把指定uri保存为存储卡文件
public static void saveFileFromUri(Context ctx, Uri src, String dest) {
try (InputStream is = ctx.getContentResolver().openInputStream(src);
OutputStream os = new FileOutputStream(dest);) {
int byteCount = 0;
byte[] bytes = new byte[8096];
while ((byteCount = is.read(bytes)) != -1){
os.write(bytes, 0, byteCount);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 从content://media/external/file/这样的Uri中获取文件路径
public static String getPathFromContentUri(Context context, Uri uri) {
String path = uri.toString();
if (path.startsWith("content://")) {
String[] proj = new String[]{ // 媒体库的字段名称数组
MediaStore.Video.Media._ID, // 编号
MediaStore.Video.Media.TITLE, // 标题
MediaStore.Video.Media.SIZE, // 文件大小
MediaStore.Video.Media.MIME_TYPE, // 文件类型
MediaStore.Video.Media.DATA // 文件大小
};
try (Cursor cursor = context.getContentResolver().query(uri,
proj, null, null, null)) {
cursor.moveToFirst(); // 把游标移动到开头
if (cursor.getString(4) != null) {
path = cursor.getString(4);
}
Log.d(TAG, cursor.getLong(0) + " " + cursor.getString(1)
+ " " + cursor.getLong(2) + " " + cursor.getString(3)
+ " " + cursor.getString(4));
} catch (Exception e) {
e.printStackTrace();
}
}
return path;
}
}
MainActivity
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.task.UploadTask;
import com.example.myapplication.task.UploadTask.OnUploadListener;
import com.example.myapplication.constant.UrlConstant;
import com.example.myapplication.util.DateUtil;
import com.example.myapplication.util.FileUtil;
@SuppressLint("SetTextI18n")
public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnUploadListener
{
private final static String TAG = "HttpUploadActivity";
private int CHOOSE_CODE = 3; // 只在相册挑选图片的请求码
private TextView tv_file_path;
private String mFilePath; // 图片文件的路径
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_file_path = findViewById(R.id.tv_file_path);
findViewById(R.id.btn_file_select).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_file_select)
{
// 创建一个内容获取动作的意图(准备跳到系统相册)
Intent albumIntent = new Intent(Intent.ACTION_GET_CONTENT);
albumIntent.setType("image/*"); // 类型为图像
startActivityForResult(albumIntent, CHOOSE_CODE); // 打开系统相册
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK && requestCode == CHOOSE_CODE) // 从相册回来
{
if (intent.getData() != null) // 从相册选择一张照片
{
Uri uri = intent.getData(); // 获得已选择照片的路径对象
// 获得图片的临时保存路径
mFilePath = String.format("%s/%s.jpg", getExternalFilesDir(Environment.DIRECTORY_PICTURES), "photo_"+ DateUtil.getNowDateTime());
FileUtil.saveFileFromUri(this, uri, mFilePath); // 保存为临时文件
tv_file_path.setText("上传文件的路径为:" + mFilePath);
UploadTask task = new UploadTask(); // 创建文件上传线程
task.setOnUploadListener(this); // 设置文件上传监听器
task.execute(mFilePath); // 把文件上传线程加入到处理队列
}
}
}
// 在文件上传结束后触发
public void finishUpload(String result)
{
// 以下拼接文件上传的结果描述
String desc = String.format("上传文件的路径:%s\n上传结果:%s\n预计下载地址:%s%s",
mFilePath, (TextUtils.isEmpty(result))?"失败":result,
UrlConstant.REQUEST_URL, mFilePath.substring(mFilePath.lastIndexOf("/")));
tv_file_path.setText(desc);
Log.d(TAG, desc);
}
}