1
我需要使用servlet將數據從外部服務器傳輸到我的主機。而不是上傳文件,我需要從另一臺服務器下載。 我試過正常下載Java程序,但它不能正常工作將數據從服務器傳輸到servlet
我需要使用servlet將數據從外部服務器傳輸到我的主機。而不是上傳文件,我需要從另一臺服務器下載。 我試過正常下載Java程序,但它不能正常工作將數據從服務器傳輸到servlet
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();
}
}
注意:您需要辦理有關也有例外。
我需要從遠程服務器下載文件到我的服務器而不是客戶機 – 2015-02-08 09:54:30