18034727885
2026-04-27 3a4c1bbb2cf4fb1cdda8a5061b0674f30e7456b4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import android.text.TextUtils
import com.example.basecore.util.ThreadUtils
import com.example.smart.bean.ImageListResponse
import com.example.smart.bean.UploadResponseBean
import com.example.smart.image.ImageProvider
import com.example.smart.oss.IOssCallBack
import com.example.smart.reqeust.RetrofitClient
import com.example.smart.reqeust.RetrofitClient.okHttpClientHeader
import com.example.smart.reqeust.interceptor.HeaderInterceptor
import com.example.smart.util.StatusProvider
import com.google.gson.Gson
import com.youth.banner.util.LogUtils
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import java.io.File
import java.io.IOException
 
 
object FileUploader {
 
 
    /**
     * 单个文件
     */
    fun uploadFile(path: String, callback: ImageProvider.IImageCallback, end: () -> Unit) {
 
        val file = File(path)
        if (!file.exists()) {
            LogUtils.e("File does not exist")
            return
        }
 
        val requestBody = RequestBody.create(
            "multipart/form-data".toMediaTypeOrNull(), file
        )
 
        val multipartBody = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", file.name, requestBody)
            .build()
 
        val token = StatusProvider.getToken()
        if (TextUtils.isEmpty(token)) {
            LogUtils.e("Login first")
            return
        }
 
        val request = Request.Builder()
            .url(RetrofitClient.serverUrl + "public/file/upload")
            .addHeader(HeaderInterceptor.authorization, "Bearer $token")
            .post(multipartBody)
            .build()
 
 
//        val client = OkHttpClient()
        val client = okHttpClientHeader()
        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                ThreadUtils.runOnUiThread {
                    end()
                    callback.onUploadFailed()
                }
            }
 
            override fun onResponse(call: Call, response: Response) {
                response.body?.let {
                    val string = it.string()
 
                    try {
                        val bean = Gson().fromJson(string, UploadResponseBean::class.java)
 
                        //net img address
                        //http://47.93.28.249:8181/public/file/download/
                        //entity.name
 
                        ThreadUtils.runOnUiThread {
                            end()
                            if (bean.data != null) {
                                bean.data.localPath = path
                                callback.onUploadSuccess(listOf(bean.data))
                            } else {
                                callback.onUploadFailed()
                            }
                        }
 
 
                    } catch (e: Exception) {
                        ThreadUtils.runOnUiThread {
                            end()
                            callback.onUploadFailed()
                        }
                    }
                }
            }
 
        })
    }
 
 
    /**
     * 多个文件
     */
    fun uploadFile(
        paths: List<String>, callback: ImageProvider.IImageCallback,
        end: () -> Unit
    ) {
        val token = StatusProvider.getToken()
        if (TextUtils.isEmpty(token)) {
            LogUtils.e("Login first")
            return
        }
 
        val builder = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
 
        for (path in paths) {
            if (TextUtils.isEmpty(path)) {
                LogUtils.d("file not exist = $path")
                continue
            }
            val file = File(path)
            val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
            builder.addFormDataPart("files", file.name, body)
        }
        val multipartBody = builder.build()
 
        val request = Request.Builder()
            .url(RetrofitClient.serverUrl + "public/file/uploadMultiple")
            .addHeader(HeaderInterceptor.authorization, "Bearer $token")
            .post(multipartBody)
            .build()
 
 
//        val client = OkHttpClient()
        val client = okHttpClientHeader()
        client.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                ThreadUtils.runOnUiThread {
                    end()
                    callback.onUploadFailed()
                }
            }
 
            override fun onResponse(call: Call, response: Response) {
                response.body?.let {
                    val string = it.string()
                    try {
                        val bean = Gson().fromJson(string, ImageListResponse::class.java)
                        ThreadUtils.runOnUiThread {
                            end()
 
                            if (bean.data != null) {
                                //此处可以根据文件名称设置本地路径
                                callback.onUploadSuccess(bean.data)
                            } else {
                                callback.onUploadFailed()
                            }
                        }
                    } catch (e: Exception) {
                        ThreadUtils.runOnUiThread {
                            end()
 
                            callback.onUploadFailed()
                        }
                    }
                }
            }
 
        })
    }
 
}