2015-02-08 87 views

回答

2

in servlet你可以从一个特定的URL获取文件并将其发送到客户端这样的:

public void doGet(HttpServletRequest req, HttpServletResponse res) 
     throws ServletException, IOException, UnavailableException 
    { 

     int bytesRead = 0; 
     int count = 0; 
     byte[] buff = new byte[1]; 

     OutputStream out = res.getOutputStream(); 

     res.setContentType("application/contenttype");//i.e: contenttype=pdf,doc,etc"); 

     String fileURL = "http://someaddress/somefile.someextension"; 
     BufferedInputStream bis = null; 
     BufferedOutputStream bos = null; 

     res.setHeader("Content-disposition", 
         "attachment; filename=somefile.someextension;"); 

     URL url = new URL(fileURL); 
     bis = new BufferedInputStream(url.openStream()); 
     bos = new BufferedOutputStream(out); 
     while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) 
      { 
       bos.write(bytesRead); 
       bos.flush(); 

      } 
    } 

注意:您需要办理有关也有例外。

+0

我需要从远程服务器下载文件到我的服务器而不是客户机 – 2015-02-08 09:54:30

相关问题