简体   繁体   English

如何接收文件并将其发送到其他服务器

[英]How to receive a file and send it to different servers

I have a server that can let user to upload the file. 我有一台可以让用户上传文件的服务器。 After the user uploads the file, I send a post request to this server In doGet Method, I did this: 用户上传文件后,我向该服务器发送了发布请求。在doGet方法中,我这样做:

  if (path.endsWith("/upload")) {
    out.println("<html>");
    out.println("<body>");
    out.print("<form action=\"/MySelf/upload\" method=\"POST\" enctype=\"multipart/form-data\">");
    out.print("<input name=\"file\" type=\"file\" size=\"50\">");
    out.print("<input name=\"submit\" type=\"submit\" value=\"submit\">");
    out.print("</form>");
    out.println("</body>");
    out.println("</html>");
    }

In doPost method, I can do this: 在doPost方法中,我可以这样做:

if (path.endsWith("/upload")) {
    Part filePart = request.getPart("file"); 
    String filename = getFilename(filePart);

    //Wanna send this filePart to other servers(for example:127.0.0.1:8888,127.0.0.1:8889 etc.)
}

Now, I wanna send this filePart to other servers now, how can I use HttpURLConnection to do this? 现在,我想立即将此filePart发送到其他服务器,如何使用HttpURLConnection做到这一点?

Here's code to send the file to another server. 这是将文件发送到另一台服务器的代码。 Thanks to Mykong for his tutorial on sending post via HttpURLConnection. 感谢Mykong提供的有关通过HttpURLConnection发送帖子的教程。 Much of this method is derived from that tutorial 该方法的大部分内容来自该教程

 // HTTP POST request, sends data in filename to the hostUrl
 private void sendPost(String hostUrl, String filename) throws Exception {

    URL url = new URL(hostUrl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "testUA");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    // Send post request
    con.setDoOutput(true);
    DataOutputStream remoteStream = new DataOutputStream(con.getOutputStream());

    byte[] fileBuffer = new byte[1024];
    FileInputStream partFile = new FileInputStream(filename);
    BufferedInputStream bufferedStream = new BufferedInputStream(partFile);

    //read from local filePart file and write to remote server
    int bytesRead = -1;
    while((bytesRead = bufferedStream.read(fileBuffer)) != -1)
    {
       remoteStream.write(fileBuffer, 0, bytesRead);
    }

    bufferedStream.close();

    remoteStream.flush();
    remoteStream.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + hostUrl);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
          new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    //read server repsonse
    while ((inputLine = in.readLine()) != null) {
       response.append(inputLine);
    }
    in.close();

    //print result
    System.out.println("Host responded: ");
    System.out.println(response.toString());

 }

Then just call this method from the part of your servlet that accepts the upload data: 然后从您的servlet接受上传数据的部分中调用此方法:

if (path.endsWith("/upload")) {
    Part filePart = request.getPart("file"); 
    String filename = getFilename(filePart);

   //send it to a server
   sendPost("http://127.0.0.1:8888", filename);

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

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