牛工写的后台接口
xutils3上传 准备
implementation 'org.xutils:xutils:3.9.0'
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
}
}
上传
package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import org.xutils.ex.HttpException;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ProgressDialog dialogShibie;
List<File> fileArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fileArrayList = new ArrayList<>();
dialogShibie = new ProgressDialog(this);
upload();
}
public List<File> saveBitmap() {
Log.e("wy", "开始保存");
//生成一个文件,存储我们将来拍照的照片
// String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/a/" ;
String sdPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/a/";
// String sdPath = "/mnt/sdcard/Internal Memory" ;
// File file = new File(sdPath);
// Log.e("wy", "绝对文件路径: " + file.getAbsoluteFile());
// Log.e("wy", "文件名: " + file.getName());
// if (!file.exists()) {
// file.mkdirs();
// Log.e("wy", "创建文件夹,路径:" + file.getPath());
// }
// String mFileName = System.currentTimeMillis() + ".jpg";
String mFileName = "a.jpg";
String mFileName1 = "b.jpg";
Log.e("wy", "保存文件名称: " + mFileName);
File f = new File(sdPath, mFileName);
File f2 = new File(sdPath, mFileName1);
fileArrayList.clear();
fileArrayList.add(f);
fileArrayList.add(f2);
return fileArrayList;
}
private void upload() {
//拍照获取的图片
List<File> mFile = saveBitmap();
// 存到本地的图片
// String sdPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/a/test.jpg";
// File mFile=new File(sdPath);
dialogShibie.setTitle("上传中");
dialogShibie.show();
// Log.e("wy1", "开始上传图片的时间: " + TimeUtil.stampToDate(System.currentTimeMillis() + ""));
RequestParams params = new RequestParams("http://192.168.176.119/uploadFile");
// params.setConnectTimeout(5000);
// 有上传文件时使用multipart表单, 否则上传原始文件流.
params.setMultipart(true);
// 上传文件方式 2 /mnt/internal_sd/FtrendCrashLog/th.jpg
params.addBodyParameter("files", mFile);
// params.addBodyParameter("file", bitmapUse);
Log.e("wy", "params: " + params);
org.xutils.common.Callback.Cancelable cancelable
= x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Log.e("wy1", "上传图片成功: " );
dialogShibie.dismiss();
// Toast.makeText(x.app(), "查到任务ID", Toast.LENGTH_SHORT).show();
Log.e("wy", "result: " + result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
//Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
if (ex instanceof HttpException) { // 网络错误
HttpException httpEx = (HttpException) ex;
int responseCode = httpEx.getCode();
String responseMsg = httpEx.getMessage();
String errorResult = httpEx.getResult();
Log.e("wy", "responseCode " +responseCode);
if (responseCode == 404) {
dialogShibie.dismiss();
Toast.makeText(x.app(), "ip地址错误", Toast.LENGTH_LONG).show();
}
// ...
} else { // 其他错误
// ...
}
// dialogShibie.dismiss();
Log.e("wy", "请求上传图片地址错误: " + ex.getMessage());
// Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}
@Override
public void onFinished() {
// dialog.dismiss();
Log.e("wy", "本次请求完全结束 ");
}
});
}
}
|