インターネットからファイルをダウンロードしたいのですが、そのファイルの URL があります。だから私はダウンロードサーブレットを書きました:
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String pathToDownload = request.getParameter("url");
URL url = new URL(pathToDownload);
URLConnection uc = url.openConnection();
String contentType = uc.getContentType();
int contentLength = uc.getContentLength();
InputStream is = uc.getInputStream();
response.setContentType(contentType);
// resp.setHeader("Content-Disposition", "attachment; filename*=\"utf-8''" + filename + "");
ServletOutputStream os = response.getOutputStream();
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
is.close();
os.close();
}
}
これで、保存するかどうかに関係なく、ユーザーがファイルをクリックしたときにポップアップを表示したいので、
resp.setHeader("Content-Disposition", "attachment; filename*=\"utf-8''" + filename + "");
しかし、ファイル名をインターネートのファイル名と同じにしたいので、上記のスニペットでさらに何が必要ですか?