简体   繁体   English

如何通过 HTTP POST 将 JSON 文件发送到在 JAVA 中的端口(例如 9090)上运行的服务器?

[英]How to send a JSON file via HTTP POST to a server running at port (say 9090) in JAVA?

I am trying to build an JAVA app (running at port 9090) which receives a JSON file from HTTP POST and converts it to an XML and sends it over to another application (APP2) which runs at a different port and further processing is done for that.我正在尝试构建一个 JAVA 应用程序(在端口 9090 上运行),它从 HTTP POST 接收一个 JSON 文件并将其转换为 XML 并将其发送到另一个在不同端口上运行的应用程序(APP2),并为那。

I have implemented all code accept the one to receive a JSON from HTTP POST as I am not sure how to start implementing it.我已经实现了所有代码都接受一个从 HTTP POST 接收 JSON 的代码,因为我不知道如何开始实现它。

Any resource/link would be very helpful.任何资源/链接都会非常有帮助。

PS Don't ask me why this is being done in this in complex way, this is what my company expects. PS 不要问我为什么要以这种复杂的方式完成这项工作,这是我公司所期望的。

The following code is implemented for APP2.以下代码为APP2实现。

SERVER SIDE:服务器端:

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 SIDE:客户端:

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!");
    }
}

The thing I am asking about how to implement HTTP POST to send a file to APP1 which runs at a different port and in turn sends it to APP2.我要问的是如何实现 HTTP POST 将文件发送到在不同端口运行的 APP1,然后将其发送到 APP2。

I am not sure on how to receive a file from HTTP POST我不确定如何从 HTTP POST 接收文件

I suppose you have some html form with file input type and correct enctype我想你有一些带有文件输入类型和正确 enctype 的 html 表单

... enctype="multipart/form-data"> <input type="file" id="input">

later in POST method handler you have to extract that file from request稍后在 POST 方法处理程序中,您必须从请求中提取该文件

Part filePart = request.getPart("file");

The only thing that I would recommend don't do the file extraction manually.我唯一建议不要手动提取文件。 Obviously use some ready to use libraries显然使用一些现成的库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM