Skip to content

Commit 4fd20a5

Browse files
committed
Update java client with postFilesWithRequest. Cleanup .idea folder items.
1 parent f000146 commit 4fd20a5

File tree

17 files changed

+5410
-149
lines changed

17 files changed

+5410
-149
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ src/ServiceStackEclipse/**/.settings
4646

4747
src/AndroidClient/client/pom.xml
4848
.idea/
49+
src/AndroidClient/.idea/**/*
50+

src/AndroidClient/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/.idea/libraries
55
.DS_Store
66
/build
7+
.idea/

src/AndroidClient/.idea/compiler.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/AndroidClient/.idea/gradle.xml

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/AndroidClient/.idea/misc.xml

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/AndroidClient/.idea/modules.xml

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/AndroidClient/.idea/modules/android/AndroidClient.android.iml

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AndroidClient/.idea/modules/client/AndroidClient.client.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package net.servicestack.client;
2+
3+
public class FileUpload {
4+
private String fieldName;
5+
private String fileName;
6+
private String contentType;
7+
private byte[] fileBytes;
8+
9+
public FileUpload(String fieldName, String fileName, String contentType, byte[] fileBytes) {
10+
this.fieldName = fieldName;
11+
this.fileName = fileName;
12+
this.contentType = contentType != null ? contentType : "application/octet-stream";
13+
this.fileBytes = fileBytes;
14+
}
15+
16+
public String getFieldName() { return fieldName != null ? fieldName : "upload"; }
17+
public String getFileName() { return fileName; }
18+
public String getContentType() { return contentType; }
19+
public byte[] getFileBytes() { return fileBytes; }
20+
}

src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
import com.google.gson.JsonElement;
99
import com.google.gson.JsonObject;
1010

11-
import java.io.BufferedReader;
12-
import java.io.DataOutputStream;
13-
import java.io.IOException;
14-
import java.io.InputStream;
15-
import java.io.InputStreamReader;
16-
import java.io.UnsupportedEncodingException;
11+
import java.io.*;
1712
import java.lang.reflect.Field;
1813
import java.lang.reflect.Type;
1914
import java.net.CookieHandler;
@@ -683,4 +678,65 @@ public void clearCookies() {
683678
CookieManager cookieManager = (CookieManager) CookieHandler.getDefault();
684679
cookieManager.getCookieStore().removeAll();
685680
}
681+
682+
// Add these methods to JsonServiceClient class:
683+
684+
private static final String BOUNDARY = "---" + UUID.randomUUID().toString() + "---";
685+
686+
public <TResponse> TResponse postFilesWithRequest(String path, Object request, FileUpload[] files, Class<TResponse> responseType) {
687+
try {
688+
// Prepare multipart data
689+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
690+
DataOutputStream dos = new DataOutputStream(baos);
691+
692+
// Add request DTO fields
693+
for (Field field : Utils.getSerializableFields(request.getClass())) {
694+
Object value = field.get(request);
695+
if (value != null) {
696+
writeMultipartField(dos, field.getName(), Utils.stripQuotes(getGson().toJson(value)));
697+
}
698+
}
699+
700+
// Add files
701+
for (FileUpload file : files) {
702+
writeMultipartFile(dos, file);
703+
}
704+
705+
// End multipart
706+
dos.writeBytes("--" + BOUNDARY + "--\r\n");
707+
dos.flush();
708+
dos.close();
709+
710+
byte[] requestBody = baos.toByteArray();
711+
String contentType = "multipart/form-data; boundary=" + BOUNDARY;
712+
713+
return send(resolveUrl(path), HttpMethods.Post, requestBody, contentType, responseType);
714+
715+
} catch (IOException | IllegalAccessException e) {
716+
throw new RuntimeException(e);
717+
}
718+
}
719+
720+
private void writeMultipartField(DataOutputStream dos, String fieldName, String value) throws IOException {
721+
dos.writeBytes("--" + BOUNDARY + "\r\n");
722+
dos.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\"\r\n");
723+
dos.writeBytes("Content-Type: text/plain; charset=" + UTF8.name() + "\r\n");
724+
dos.writeBytes("\r\n");
725+
dos.writeBytes(value + "\r\n");
726+
}
727+
728+
private void writeMultipartFile(DataOutputStream dos, FileUpload file) throws IOException {
729+
dos.writeBytes("--" + BOUNDARY + "\r\n");
730+
dos.writeBytes("Content-Disposition: form-data; name=\"" + file.getFieldName() +
731+
"\"; filename=\"" + file.getFileName() + "\"\r\n");
732+
dos.writeBytes("Content-Type: " + file.getContentType() + "\r\n");
733+
dos.writeBytes("\r\n");
734+
dos.write(file.getFileBytes());
735+
dos.writeBytes("\r\n");
736+
}
737+
738+
// Convenience method for single file upload
739+
public <TResponse> TResponse postFileWithRequest(String path, Object request, FileUpload file, Class<TResponse> responseType) {
740+
return postFilesWithRequest(path, request, new FileUpload[]{file}, responseType);
741+
}
686742
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy