配置调度程序Servlet以连接到其他主机

时间:2012-10-19 14:25:07

标签: java file unix upload dispatcher

我有一个上传程序,可以上传文件并将其保存到部署了此应用程序的指定主机上。

我想将此文件保存到其他主机。 有人可以指导我必须对我的调度员servlet进行相同或任何其他替代方案的更改吗?

1 个答案:

答案 0 :(得分:-1)

我建议在Servlet中与其他主机建立新连接,并使用连接上传文件。

 int contentChar = null;
 FileInputStream inputStream = new FileInputStream(getfileRootDir()+ "/"+fileId));

 URL oracle = new URL("other server url");
 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
 OutputStream outPutStream = connection.getOutputStrea()
 while ((contentChar = inputStream.read()) != -1) {
      outPutStream.write(contentLine );
 }
 inputStream .close();
 outPutStream.close(); 
 connection.close();

如果您想从效率角度使用BufferredReader / Writer,您可能需要写如下:

   String contentLine = null;
   BufferedReader reader = new BufferedReader(
                                    new FileReader(getfileRootDir()+ "/"+fileId));

   URL oracle = new URL("other server url");
   HttpURLConnection connection = (HttpURLConnection)url.openConnection();
   OutputStream outPutStream = connection.getOutputStrea()
   Writer streamWriter = new BufferedWriter(new OutputStreamWriter(outPutStream ));
   while ((contentLine = reader.readLine()) != null) {
        streamWriter.write(contentLine );
   }
   reader.close();       
   streamWriter.close();
   outPutStream .close(); 
   connection.close();