Java servlet read a file and send it as response
jakarta-ee
Solution
How about this one? FileUtils (Commons IO 2.5-SNAPSHOT API)
example /src_directory_path/ is mount directory of remote server.
@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());
}
Was this by any chance what you wanted to hear?
@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());
}
Problem
I'm trying to write a servlet which will read (download) file from a remote location and simply send it as response, acting more-or-less like a proxy to hide the actual file from getting downloaded. I'm from a PHP background where I can do it as simply as calling file_get_contents. Any handy way to achieve this goal using servlet/jsp? Thanks