【问题标题】:Configure dispatcher Servlet to connect to other host配置调度程序 Servlet 以连接到其他主机
【发布时间】:2012-10-19 14:25:07
【问题描述】:

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

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

【问题讨论】:

  • 您能给我们看一些代码并描述您尝试过的内容吗?
  • 这个问题太笼统了!您的“调度程序 servlet”到底是什么?您想如何连接到远程主机(HTTP、FTP、CIFS 等)?

标签: java file unix upload dispatcher


【解决方案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();

【讨论】:

  • 您正在使用一个简单的 java 代码执行此操作。这也可以单独运行。没有发现任何问题。
  • @artbristol : 传递给写入的内容不是要注意字符编码吗?
  • /test/upload/files../files值> -------------------------------------------- -----------------------这就是我现在正在使用的..即调度程序 servlet 中的路径
  • 这里不需要使用Writer。只需将文件的字节直接写入OutputStream即可。
  • abcd.xxx.com:8080/test/upload/files../files ---------------- 如果你看到 abcd.xxx.com:8080/ ……我需要我的文件上传到其他主机,这是 abcd ......这就是我认为应该是的......因为我希望我的文件被上传到其他主机..
猜你喜欢
  • 1970-01-01
  • 2017-11-08
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 2015-03-01
  • 2018-08-15
相关资源
最近更新 更多