-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUpload.java
More file actions
43 lines (36 loc) · 1.43 KB
/
FileUpload.java
File metadata and controls
43 lines (36 loc) · 1.43 KB
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
import callconfig.CallConfig;
import okhttp3.*;
import java.io.IOException;
import java.util.Scanner;
public class FileUpload {
public static boolean oneFileGotUploaded = false;
public static void uploadFile(){
Scanner in = new Scanner(System.in);
String RESOURCE = "/upload-file";
System.out.println("Enter file:");
String filename = in.nextLine();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file","filename.txt",
RequestBody.create(filename, MediaType.parse("text/plain")))
.build();
Request request = new Request.Builder()
.url(CallConfig.BASE_URL + RESOURCE)
.post(requestBody)
.build();
try(Response response = CallConfig.HTTP_CLIENT.newCall(request).execute()){
String responseBody = response.body().string();
//System.out.println(responseBody);
if(response.isSuccessful()){
System.out.println("Response: " + responseBody);
oneFileGotUploaded = true;
}
else{
//System.out.println("Request failed: " + response.code());
System.out.println("Request error messege: " + responseBody);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}