html - Posting File to WebServer with Java -


I try to post a file on an ASP.NET Web API (C #) server with a local Java application I am here. Actually I am trying to reproduce the following HTML code in Java SE:

  & lt; form name = "form1" method = "post" enctype = "multipart / form-data" action = "http: // localhost: 50447 / API / files /" & gt; & Lt; Div & gt; & Lt; label = "image1" & gt; For the image file & lt; / Label & gt; & Lt; Input name = "image1" type = "file" /> & Lt; / Div & gt; & Lt; Div & gt; & Lt; Input type = "submit" value = "submit" /> & Lt; / Div & gt;   

What is the easiest way to do this? I would like to avoid using Apache. Something like this:

  string urlToConnect = "http: // localhost: 50447 / API / file /"; String paratosed = ""; Upload file file to file = new file ("C: / User / AA / Desktop / Sample_synand.pdf"); String border = long.TextString (system syntimeimilis ()); // Just generate some unique random value URL Connection Connection = Faucet; Try {connection = new URL (urlToConnect). openConnection (); } Grip (IOException E2) {// TODO Auto-generated Cache Block e2.printStackTrace (); } connection.setDoOutput (true); // It sets the request method for posting Connection.setRequestProperty ("content-type", "multipart / form-data; limit =" + limit); Printr writer = null; {Author = new print Viartr (new Autputstrym water (try.getOutputStream (), try the "UTF-8"); author Kprintelan ( "-" + range); Writer.println ( "Content-Dispojhsn: form-data ; name = \ "Prmtosed \" "); Writerkprintln (" content-type: text / plain; charset = UTF -8 "); writer.println (); writer.println (paramToSend); author Kprintelan (" - "+ range); Writer.println (" content-Dispojhsn: form-data; name = \ "file to Uplod \" filename = \ "Sample_signedkpdf \" "); Writer.println (" content-type : Text / plain; charset = ut F -8 "); writer.println (); Bfredder reader = null; {Try {reader = new BufferedReader (Try new InputStreamReader (new FileInputStream (fileToUpload)," UTF-8 ");} catch (unsupported Ancodingakspshn | Failnotfoundakseshn e L) ElkprintStackTrace {();} try {(the string line, (line Reedrkredlain = ())! = null;) {author. Printlain (line);}} catch (IOException e) {E.printStackTrace ();}} Finally {try (reader! = Tap) {reader.close ();} Hold (IOException logOrIgnore) {}} Writer. Println ("-" + + "-"); } Grip (unsupported encoding exception E2) {e2.printStackTrace (); } Hold (IOException e2) {e2.printStackTrace (); } Finally {if (Author! = Null) the author Close (); } // Whenever you request any status, the connection is executed freely. int responseCode = 0; {ResponseCode = (try a (HttpURL connection connection)). GetResponseCode (); } Hold (IOException e) {e.printStackTrace (); } System.out.println (responseCode);   

Use only URLConnections. This code is not working properly. This file sends but some content is lost and I do not know why .. My HTML The sample works perfectly. .

Can you help me?

Thank you for your attention, Best regards

You are using reader and author combinations, while you are transferring binary data. When working with binary data, you should use the stream instead of

So any Bfred Reader, use the normal Inputstrym the buffer to read the file and wrap up the output stream from the URL connection .

Read the bytes instead of the string in the reading loop. Therefore replace the starting portion of the BufferedReader with the end just before the boundary:

  OutputStream Output = connection.getOutputStream (); In InputStream file = new FileInputStream (fileToUpload); Try {byte [buffer = new byte] [4096]; Integer length; While ((length = file in. Read (buffer)) gt; 0) {output.write (buffer, 0, length); } Output.flush (); } Finally {try (fileIn! = Null) {fileIn.close (); } Hold (IOException logOrIgnore) {}}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -