2015-02-08 82 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

相關問題