package com.hh.holyolm.util;

import static com.squareup.okhttp.MultipartBuilder.FORM;

import android.util.Log;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

public class HttpUtil {
    public static final MediaType MEDIA_TYPE_ZIP
            = MediaType.parse("application/zip; charset=utf-8");

    public static void okHttpUploadFile(String url,File file) {
        OkHttpClient client;
        client = new OkHttpClient();
        RequestBody fileBody = RequestBody.create(MEDIA_TYPE_ZIP, file);

        MultipartBuilder builder=new MultipartBuilder();
        builder.type(FORM);
        //builder.addFormDataPart("storeFileName",file.getName());
        builder.addFormDataPart("file",file.getName(),fileBody);
        RequestBody requestBody = builder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                String str = response.body().string();
                Log.d("okHttpUploadFile", "onResponse: "+str);
                Log.i("zy", str);
            }

            public void onFailure(Call call, IOException e) {

            }

            public void onResponse(Call call, Response response) throws IOException {

                String str = response.body().string();
                Log.d("okHttpUploadFile", "onResponse: "+str);
                Log.i("zy", str);
            }
        });
    }

    public static void uploadFile(String url,File file) throws Exception {
        //String url = "{域名}/aviationmaintenance/storage/postObject";/
        //String storeFileName = "anoy_dptTemplate1test.xls";
        //String path = "C:\\Users\\User\\Desktop\\dptTemplate1.xls";
        //File file = new File(path);
        String storeFileName =file.getName();

        HttpRequest httpRequest = cn.hutool.http.HttpUtil.createPost(url);

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("userfile", file);
        //paramMap.put("storeFileName", storeFileName);
        httpRequest.form(paramMap);
        HttpResponse response=httpRequest.execute();
        Log.d("uploadFile", "response: "+response.body().toString());
    }

}
