我正在尝试编写一个servlet,它将从远程位置读取(下载)文件,并将其作为响应发送,或多或少像代理一样隐藏实际文件的下载.我来自PHP背景,我可以像调用file_get_contents一样完成它.
使用servlet/jsp实现这一目标的任何方便方法?
谢谢
app*_*uin 16
这个怎么样? FileUtils(Commons IO 2.5-SNAPSHOT API)
example/src_directory_path /是远程服务器的安装目录.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition", "filename=\"hoge.txt\"");
File srcFile = new File("/src_directory_path/hoge.txt");
FileUtils.copyFile(srcFile, resp.getOutputStream());
}
Run Code Online (Sandbox Code Playgroud)
你有什么想听的吗?
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream in = new URL( "http://remote.file.url" ).openStream();
IOUtils.copy(in, response.getOutputStream());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33696 次 |
| 最近记录: |