将数据从服务器传输到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