0

I want to send a large file between two servlets in different servers. This file can be considired as a large String. So, I cannot use doGet since the string is larger. What is the best way please ?

I read that SendRedirect is the only way that I can use since the servlet are in two different servers. And in another time I cannot use SendRedirect because I Cannot send a large String or a file by URL. So, what is solution ?

Mehdi
  • 2,160
  • 6
  • 36
  • 53
  • you could stream the file to the servlet. Example: http://stackoverflow.com/questions/55709/streaming-large-files-in-a-java-servlet – mjspier Dec 22 '15 at 10:30

1 Answers1

-1

Encode you file : base64

byte[] bt= 
// Conversion B64
String result=DatatypeConverter.printBase64Binary(bt);

put it in a doPost as parameter

use java.net.URLConnection:

String urlParameters  = "my_file=my_long_file_inbase64_here";
byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
int    postDataLength = postData.length;
String request        = "http://otherserver.com/myservlet";
URL    url            = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();           
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
conn.setRequestProperty( "charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
   wr.write( postData );
}

from this: Java - sending HTTP parameters via POST method easily

just decode it at the other side

byte [] byteArray=DatatypeConverter.parseBase64Binary(result);

You can also use multipart post (no need)

You get your data in doPost: doGet and doPost in Servlets

public void doPost(HttpServletRequest req, HttpServletResponse res)
       throws ServletException, IOException {
mydata_serialized = req.getParameter("my_file");
Community
  • 1
  • 1
  • Can I send a post by using a SendRedirect ? I don't think – Mehdi Dec 22 '15 at 10:54
  • Thank you for your example. If I understand, you create a connection between two servlets and you transfert the data in base 64. But, your code to send the data. How can i receive this post in the other servlet ? I mean, I need also a connection ? – Mehdi Dec 22 '15 at 11:34
  • and the receiver, receive the post value in doPost method ? – Mehdi Dec 22 '15 at 11:59
  • yes : see my answer. Good luck ! – guillaume girod-vitouchkina Dec 22 '15 at 12:51
  • after sending there is is no redirection to doPost. If I print something like System.out.println("hello") in doPost method. hello is not printed. There are a configuration in web.xml file ? – Mehdi Dec 22 '15 at 12:58
  • Terrible answer. Base64 makes it larger. The answer is simply to use POST instead of GET. No need to massage the data format forth and back to an inefficient format. Base64 has a completely different purpose (being able to embed binary data in character based bodies, however a `String` is in first place already character based!). Moreover, HTTP offers with `multipart/form-data` already possiblity to efficiently send binary files. See the duplicate for a concrete example on that. – BalusC Dec 23 '15 at 11:13
  • @BalusC . Not so terrible. Yes, it is larger, but very convenient to manipulate, and there are almost always other informations to transmit with the binary. And http and libraries are not always reliable. – guillaume girod-vitouchkina Dec 23 '15 at 11:20