private void upLoad() { // Bitmap bitmap; // Bitmap bmpCompressed; // for (int i = 0; i < list.size() - 1; i++) { // bitmap = BitmapFactory.decodeFile(list.get(i)); // bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 480, true); // ByteArrayOutputStream bos = new ByteArrayOutputStream(); // bmpCompressed.compress(Bitmap.CompressFormat.JPEG, 100, bos); // byte[] data = bos.toByteArray(); // Toast.makeText(UserAddMsgActivity.this, "发送成功" + data.toString(), Toast.LENGTH_SHORT).show(); // } final String url = "http://115.159.217.226/upload.php"; MultipartBody.Builder mbody = new MultipartBody.Builder().setType(MultipartBody.FORM); Bitmap bitmap = null; for (int i = 0; i < list.size() - 1; i++) { File file = new File(list.get(i)); String str = BitmapUtils.compressImageUpload(file.getPath());//得到压缩后的图片的路径 File compress_file = new File(str);//得到新图片 mbody.addFormDataPart("image"+i,file.getName(),RequestBody.create(MediaType.parse("application/octet-stream"),compress_file));//将图片加到mbody中(以二进制形式传输) BitmapUtils.deleteCacheFile();//清除缓存 } RequestBody requestBody = mbody.build(); Request request = new Request.Builder() .url(url) .post(requestBody) .build(); final okhttp3.OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder(); OkHttpClient okHttpClient = httpBuilder //设置超时 .connectTimeout(1000, TimeUnit.MINUTES) .readTimeout(1000, TimeUnit.MINUTES) .writeTimeout(1000, TimeUnit.MINUTES) .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e("aa", "uploadMultiFile() e=" + e); } public void onResponse(Call call, Response response) throws IOException { Log.i("bb", "uploadMultiFile() response=" + response.body().string()); Intent intent = new Intent(UserAddMsgActivity.this,MainActivity.class); startActivity(intent); } }); }