We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,894 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

illegal state exception in getOutputStream and dispatcher.forward

Hello Everyone,

Im needing a bit of help here.

First, Im trying to let client download a file from server and save it on client's pc.

When client clicks on button "DOWNLOAD", it will call a servlet that will download the file.

Expected outcome is that a SAVE AS DIALOG BOX will pop up so that client can choose output destination folder.

This is my CODE

path = getServletContext().getRealPath("/")+ "files/";
		
		String filename = path + "users.txt";
		String disHeader = "Attachment;Filename=\"users.txt\"";
		
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition",disHeader);
		
		System.out.println(filename);
		File fileToDownload = new File(filename);
		FileInputStream fis = new FileInputStream(fileToDownload);
		
		try {
			//response.reset();
			
			ServletOutputStream fos = response.getOutputStream();
			
			System.out.println("after ng get output stream");
			
			int i;
			
			while ((i = fis.read()) != -1) {
				fos.write(i);
				//response.getWriter().write(i);
			}
			
			fis.close();
		}
		catch (Exception e) {
			System.out.println(e);
		}
		
		System.out.println("ntapos ang whikle");
		
		//response.reset();
		
		infoMsg = "* Files were copied to C:/Temp/Backup/";
		
		request.setAttribute("infoMsg", infoMsg);
		
		destinationPage = "/erf_home.jsp";
		
		dispatcher = getServletContext().getRequestDispatcher(destinationPage);
		
		
		System.out.println("dispatcher nkuha");
		
		//response.reset();
		dispatcher.forward(request, response);

What happens is that I get IllegalStateException because I use the getOutputStream and the issue a forward request.

If I comment

dispatcher.forward(request, response);

All works well but the page is not redirected to the next page.

If I use

response.reset()

save dialog box does not appear.

I know that the code is in error.

Can you please give suggestions on what i can do so that save dalog box will appear and after download page will be redirected to another page (HOME PAGE)

Thanks for all your help.

1
Contributor
0
Replies
1
View
Eric Cute
Posting Whiz in Training
252 posts since Jun 2010
Reputation Points: 55
Solved Threads: 20
Skill Endorsements: 2

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0580 seconds using 2.71MB