1
我有一個servlet發送文件給客戶端。 是否可以添加重定向以開始下載並將用戶重定向到anoteher html頁面? 這是我的代碼:如何在一個請求中發送文件和重定向?
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
System.out.println("debut transfert fichier a " + request.getRemoteUser() + "/" + request.getRemoteAddr());
response.setContentType("text/plain");
response.setHeader("Content-Disposition",
"attachment;filename=my_file.pdf");
// ServletContext ctx = getServletContext();
File jnlp = new File(PATH);
InputStream is = new FileInputStream(jnlp);
int read = 0;
byte[] bytes = new byte[BYTES_DOWNLOAD];
OutputStream os = response.getOutputStream();
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
// not workinggetServletContext().getRequestDispatcher("/merci.html").forward(request, response);
os.flush();
os.close();
is.close();
HttpSession session = request.getSession();
//
} catch (Exception ex) {
System.out.println("Erreur inconnu lors transfert :" + ex);
}
謝謝。