-2

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

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

4

1 回答 1

-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();
于 2012-10-19T14:31:05.380 回答