Skip to content

Instantly share code, notes, and snippets.

@gusthavosouza
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusthavosouza/9ae92d91ad12a634ded9 to your computer and use it in GitHub Desktop.
Save gusthavosouza/9ae92d91ad12a634ded9 to your computer and use it in GitHub Desktop.
package com.sighra.apps.datasul.bean;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
public class SendArchiveHttpPost {
Socket socket = null;
public static void main(String[] args) throws IOException {
//Constantes
final int PORT = 10132;
final String HOST = "192.168.1.101";
//Stream
BufferedReader reader = null;
BufferedOutputStream bos = null;
FileInputStream fos = null;
try {
File file = new File("archive.xml");
URL url = new URL("http", HOST , PORT, "/app");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.addRequestProperty("filename", file.getName());
httpURLConnection.connect();
System.out.println(file.length());
fos = new FileInputStream(file);
bos = new BufferedOutputStream(httpURLConnection.getOutputStream());
byte [] content = new byte[2048];
int read = 0;
int lidos = 0;
while ((read = fos.read(content, 0,content.length))!= -1) {
httpURLConnection.getOutputStream().write(content,0, read);
httpURLConnection.getOutputStream().flush();
lidos+=read;
}
content = new byte[1024];
read = 0;
reader = new
BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line = "";
while((line = reader.readLine()) != null){
System.out.println(new String(line));
}
System.out.println(lidos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try{fos.close();}catch(Exception e){}
try{bos.close();}catch(Exception e){}
try{reader.close();}catch(Exception e){}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment