當前位置:首頁 » 文件管理 » retrofit圖文上傳

retrofit圖文上傳

發布時間: 2022-09-26 03:25:21

『壹』 android retrofit 2.0 怎麼同時上傳多張

/**
* 上傳一張圖片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs);

『貳』 retrofit可以上傳大文件嗎

1.單張圖片的上傳

?

1
2
3
4
5
6
7
8
9
10

/**
* 上傳一張圖片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs);

2.多張圖片的上傳

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

/**
* 上傳三張圖片
* @param description
* @param imgs
* @param imgs1
* @param imgs3
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs,
@Part("file\"; filename=\"image.png\"")RequestBody imgs1,
@Part("file\"; filename=\"image.png\"")RequestBody imgs3);

注意:目前是提供傳3張,要想多上傳目前我發現的方法就是想要多傳一張,就多增加一個參數
@Part("file\"; filename=\"image.png\"")RequestBody imgs,以此類推。
大家看到上面覺得寫法很漏,但是用於能力有限,只能想到這樣。用Java中的可變參數解決之後,就只能傳一張。不能多張。

?

1
2
3
4

@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody ...imgs);

調用:
Call<String> call = apiManager.uploadImage( m[0],requestBody1,requestBody2,null);
這樣寫看上去很是高端,不幸的是只能傳一張
3.最後是實現胡過程
3.1創建FileUploadService介面
?

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

public interface FileUploadService {
/**
* 上傳一張圖片
* @param description
* @param imgs
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs);

/**
* 上傳三張圖片
* @param description
* @param imgs
* @param imgs1
* @param imgs3
* @return
*/
@Multipart
@POST("/upload")
Call<String> uploadImage(@Part("fileName") String description,
@Part("file\"; filename=\"image.png\"")RequestBody imgs,
@Part("file\"; filename=\"image.png\"")RequestBody imgs1,
@Part("file\"; filename=\"image.png\"")RequestBody imgs3);
}

3.2創建Retrofit對象
?

1
2
3
4
5
6
7

private static final Retrofit sRetrofit = new Retrofit .Builder()
.baseUrl(ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
// .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 使用RxJava作為回調適配器
.build();

private static final FileUploadService apiManager = sRetrofit.create(FileUploadService.class);

3.3調用上傳的方法
?

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

public static void upload(String path){

String descriptionString = "hello, this is description speaking";

String[] m = new String[2];
m[0]= "share.png";
m[1]= "Screenshot_20160128-140709.png";
File[] ssssss= new File[2];
File file1 = new File("/storage/emulated/0/sc/share.png");
File file = new File("/storage/emulated/0/Pictures/ScreenShots/Screenshot_20160128-140709.png");
ssssss[0]=file;
ssssss[0]=file1;
RequestBody requestBody[] = new RequestBody[3];
RequestBody requestBody1 =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
RequestBody requestBody2 =
RequestBody.create(MediaType.parse("multipart/form-data"), file1);
requestBody[0]=requestBody1;
requestBody[1]=requestBody2;
Call<String> call = apiManager.uploadImage( m[0],requestBody1,requestBody2,null);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Response<String> response, Retrofit retrofit) {
Log.v("Upload", response.message());
Log.v("Upload", "success");
}

@Override
public void onFailure(Throwable t) {
Log.e("Upload", t.toString());
}
});

}

4.伺服器段代碼:
伺服器用的是struts接收:

@Controller
public class GetToken extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = 1L;
private File[] file;
private String[] fileName;
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileName() {
return fileName;
}
public void setFileName(String[] fileName) {
this.fileName = fileName;
}

@Action("/upload")
public void login() {
System.out.println("------"+Arrays.toString(file));
System.out.println("------"+Arrays.toString(fileName));
}

}

『叄』 初學者問,怎樣通過url傳遞動態變化的參數

其實我們用 POST 的場景相對較多,絕大多數的服務端介面都需要做加密、鑒權和校驗,GET 顯然不能很好的滿足這個需求。使用 POST 提交表單的場景就更是剛需了,怎麼做呢?

@FormUrlEncoded
@POST("/")
Call<ResponseBody> submit(
@Field("name") String name,
@Field("occupation") String occupation);12345

其實也很簡單,我們只需要定義上面的介面就可以了,我們用 Field 聲明了表單的項,這樣提交表單就跟普通的函數調用一樣簡單直接了。

與Query一樣,如果你有很多Field參數,不要怕,趕緊試試FieldMap吧。

Part & PartMap(POST請求)
這個是用來上傳文件的。有了 Retrofit,媽媽再也不用擔心文件上傳費勁了~~~

public interface FileUploadService {
@Multipart
@POST("upload")
Call<ResponseBody> upload(@Part("description") RequestBody description, @Part MultipartBody.Part file);
}1234567

如果你需要上傳文件,和我們前面的做法類似,定義一個介面方法,需要注意的是,這個方法不再有 @FormUrlEncoded 這個註解,而換成了 @Multipart,後面只需要在參數中增加 Part 就可以了。也許你會問,這里的 Part 和 Field 究竟有什麼區別,其實從功能上講,無非就是客戶端向服務端發起請求攜帶參數的方式不同,並且前者可以攜帶的參數類型更加豐富,包括數據流。

也正是因為這一點,我們可以通過這種方式來上傳文件,下面我們就給出這個介面的使用方法:

『肆』 android retrofit上傳二進制流 byte[] img

以下是圖片上傳方式:
介面寫法:
Java code?
1
2
3

@Multipart
@POST("/user/addLicenseInfo")
void addLicenseInfo(@QueryMap Map<String, Object> options, @Part("file") TypedFile file, Callback<JsonElement> response);

實現寫法:
Java code?
1
2
3
4
5

API api = mRegisterActivity.createAPI();
Map<String, Object> options = new HashMap<String, Object>();
options.put("mobile",photoNumber);
TypedFile typedImage = new TypedFile(getMIMEType(pictureFile), pictureFile);
api.addLicenseInfo(options,typedImage,new Callback<JsonEleme

『伍』 為什麼Retrofit以Mutipart上傳參數時,String參數會多一對雙引號

「@Part(「data」) String des」在Post請求中默認的Content-Type類型是「application/json」,這就說明我們在介面中不能再使用@Part註解了

@Multipart
@POST("userPhoto")
Observable<BaseHttpResult<String>> uploadMultipleTypeFile(@PartMap Map<String, RequestBody> params);

Map<String, RequestBody> bodyMap = new HashMap<>();
bodyMap.put("photo"; filename=""+file.getName(), RequestBody.create(MediaType.parse("image/png"),file));
bodyMap.put("userId", toRequestBody(userId));
bodyMap.put("serialNumber", toRequestBody(serialNumber));

public static RequestBody toRequestBody(String value) {

『陸』 retrofit 怎麼設置長連接 文件類型

想實現這一目標其實很簡單,隨便打開一個文件夾,將查看方式改為大圖標,然後打開文件夾選項,打開查看選項卡,點擊應用到文件夾,就可以了,具體操作步驟如下: 1、隨便打開一個文件夾,點擊紅色方框中的向下三角,更多選項, 將查看類型改為大圖標; 2、「工具」下拉菜單,選擇文件夾選項; 3、選擇查看選項卡,點擊應用到文件夾, 4、在彈出的提示框中選擇「是(Y)」,然後打開另外一個文件夾,看看自己的目的是否已經達到。

『柒』 怎麼解決retrofit上傳文件導致內存不足

保存文件,重要的備份。然後查看資源管理器,幹掉內存大的,或者重啟。

『捌』 android retrofit 上傳進度requestbody writeto 為什麼會調用兩次

用Retrofit發送網路請求和解析json的實例Retrofit是Android的一個非常好用的開源HTTPRequest。現在介紹一下Retrofit是如何使用的。。。。首先是導入Retrofit包,dependencies{compilefileTree(dir:'libs',include:['*.jar'])compile

熱點內容
sql注入的過程 發布:2024-10-09 16:24:25 瀏覽:194
命令行ftp初始賬號密碼 發布:2024-10-09 16:24:24 瀏覽:290
腳本怎麼歸檔 發布:2024-10-09 16:08:07 瀏覽:296
雲平台搭建伺服器 發布:2024-10-09 16:03:47 瀏覽:636
用阿里雲搭建正向代理伺服器 發布:2024-10-09 15:53:07 瀏覽:506
手機qq空間緩存清理緩存 發布:2024-10-09 15:51:49 瀏覽:353
pc泰拉瑞亞伺服器ip 發布:2024-10-09 15:45:18 瀏覽:798
安卓怎麼延時 發布:2024-10-09 15:37:51 瀏覽:453
android音源 發布:2024-10-09 14:55:19 瀏覽:119
預編譯sql怎麼模糊查詢 發布:2024-10-09 14:31:24 瀏覽:217