Ich versuche eine JAVA-App (läuft am Port 9090) zu erstellen, die eine JSON-Datei von HTTP POST empfängt und in eine XML konvertiert und sie an eine andere Anwendung (APP2) sendet, die an einem anderen Port läuft und weiterverarbeitet wird dafür gemacht.Wie sende ich eine JSON-Datei über HTTP POST an einen Server, der am Port läuft (zB 9090) in JAVA?
Ich habe alle Code akzeptieren den einen JSON von HTTP POST erhalten, da ich nicht sicher bin, wie man es zu implementieren beginnt.
Jede Ressource/Link wäre sehr hilfreich.
P.S. Fragen Sie mich nicht, warum das auf komplexe Weise gemacht wird, das erwartet meine Firma.
Der folgende Code ist für APP2 implementiert.
Server-Seite:
public class ServerSide{
static String JsonPath = "src/TestApp.json";
static String XMLPath = "TestApp";
static String Port = "Port1";
public static void main(String[] args) throws IOException, SAXException {
// if (args.length != 1) {
// System.err.println(
// "No argument specified");
// System.exit(1);
// }
ConfigReader reader = new ConfigReader();
int port = Integer.parseInt(reader.getPropValues("Port1"));
System.out.println(port);
//JSON to XML conversion
try {
JsonToXML.JsonConversion(JsonPath,XMLPath);
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ServerSocket ssock = new ServerSocket(port);
Socket socket = ssock.accept();
System.out.println("Server Started");
//The InetAddress specification
InetAddress IA = InetAddress.getByName("localhost");
//Specify the file
System.out.println("This is the connection ");
File file = new File("Sahil.xml");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
//Get socket's output stream
OutputStream os = socket.getOutputStream();
//Read File Contents into contents array
byte[] contents;
long fileLength = file.length();
System.out.println(" This is file length "+ fileLength);
long current = 0;
long start = System.nanoTime();
while(current!=fileLength){
int size = 10000;
if(fileLength - current >= size){
System.out.println(" This is the file length "+fileLength+" current : "+current+"size : "+size);
current += size;
}
else{
size = (int)(fileLength - current);
current = fileLength;
System.out.println(" This is the current "+current);
}
contents = new byte[size];
bis.read(contents, 0, size);
os.write(contents);
System.out.println("Sending file ... "+(current*100)/fileLength+"% complete!");
}
os.flush();
//File transfer done. Close the socket connection!
socket.close();
ssock.close();
System.out.println("File sent succesfully!");
}
}
Client-Seite:
public class ClientSide{
public static void main(String[] args) throws Exception{
String line;
//Initialize socket
InetAddress host = InetAddress.getLocalHost();
Socket socket = new Socket(host.getHostName(), 4355);
byte[] contents = new byte[10000];
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//No of bytes read in one read() call
while ((line = br.readLine()) != null){
System.out.println(line);
}
socket.close();
System.out.println("File saved successfully!");
}
}
Die Sache, über die ich frage, wie HTTP POST zu implementieren, eine Datei zu APP1 zu senden, die an einem laufen anderen Port und wiederum an APP2.
können Sie uns Ihren Code geben? – Vincent
Um sicherzustellen, dass Ihr Server funktioniert, können Sie Jmeter verwenden und den Client emulieren – yurko