2014-03-30 42 views
0

我有一台服务器可以让用户上传文件。用户上传的文件后,我发送POST请求到该服务器 在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>"); 
    } 

在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.) 
} 

现在,我现在要发送此filePart到其他服务器,我怎样才能使用HttpURLConnection来做到这一点?

回答

1

下面是将文件发送到另一台服务器的代码。感谢Mykong他的教程通过HttpURLConnection发送帖子。许多这样的方法是从教程衍生

// 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()); 

} 

然后,只需调用从接受上传数据你的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); 
+0

如果该文件是非常大的?有没有办法可以避免将filePart转换为String? – CSnerd

+0

是的,在sendPost方法中,您可以使用'FileInputStream.read()'一次读取几千字节的filePart文件,并使用多个'writeBytes()'调用将其发送到远程服务器。 – lreeder

+0

我更新了我的答案,以显示如何从本地文件读取并将其推送到远程。 – lreeder

相关问题