Skip to content

Instantly share code, notes, and snippets.

@gfrison
Created August 26, 2015 08:39
Show Gist options
  • Save gfrison/8268541eea88bfb76102 to your computer and use it in GitHub Desktop.
Save gfrison/8268541eea88bfb76102 to your computer and use it in GitHub Desktop.
rxnetty multi part problem
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Random;
import io.netty.handler.logging.LogLevel;
import io.reactivex.netty.protocol.http.client.HttpClient;
public class ConcatUpload
{
public static void main(String[] args) throws MalformedURLException
{
final URL url = new URL("http", "docs.spring.io", 80,
"/spring-xd/docs/current-SNAPSHOT/reference/html/images/spring-xd-admin-ui-jobs-view-module-details-button.png");
final String[] cType = new String[2];
HttpClient.newClient(url.getHost(), url.getPort()).enableWireLogging(LogLevel.DEBUG).createGet(url.getPath())//
.flatMap(response -> {
cType[0] = response.getHeader("Content-Type");
cType[1] = response.getHeader("Content-Length");
String boundary = new Random().nextInt(99999) + "";
byte[] header = ("--" + boundary + '\n' + "Content-Disposition: form-data; name=\"file\"; " +
"filename=\"code\"\nContent-Type: " + cType[0] + "\n\n").getBytes();
byte[] footer = ("\n\n--" + boundary + "--").getBytes();
return HttpClient.newClient("posttestserver.com", 80).enableWireLogging(LogLevel.DEBUG).createPost(
"/post.php?dir=qwer").addHeader("Content-Type",
"multipart/form-data; boundary=" + boundary).addHeader("Content-Length", Integer.valueOf(
cType[1]) + header.length + footer.length)//
.writeContent(response.getContent()).flatMap(yresp -> yresp.getContent().map(
bytess -> bytess.toString(Charset.defaultCharset()))).collect(() -> new StringBuilder(),
(sb, str) -> sb.append(str));
}).toBlocking().forEach(str -> System.out.println(str.toString()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment