0

my problem is that I have a program which does make a file & stream it out to the user & I have two problem:

1) first how do change the file name & type (x.sql)

2) what if there's an exception while making the file & I want to redirect the user to an error page because my return type of my method is void when we want to write into outputstream in servlet !

my program is like this:

    @RequestMapping(value = "/test/{jobid}")
   public void dumpData(@PathVariable("jobid") long jobid, HttpServletResponse response) {
     try {

        response.setContentType("application/octet-stream");
        ServletOutputStream out = response.getOutputStream();
        String downloadOutput = "";
        // AM I ABLE TO SET FILENAME SO THE USER DOWNLOAD THE FILE WITH THAT NAME ?
        // DOES SETHEADER HELP ME IN THIS CASE ?

        ... (making downloadOutput String by values coming from somewhere)

        out.write(downloadOutput.getBytes("UTF-8"));
        out.flush();
        out.close();
     } catch(SomeException e){
         //WHEN THE RETURN TYPE IS VOID HOW TO REDIRECT USER TO ERROR PAGE IN CASE OF SOME PROBLEM ? 
        //(INFO:)IF WE DEFINE STRING AS RETURN TYPE THE PROGRAM WILL GET EXCEPTION 
     }
Mehdi
  • 3,795
  • 3
  • 36
  • 65
  • Once you start writing the content of the file to the response, you won't be able to redirect to an error page. If the error happens before that, you can do `response.sendRedirect(url)` – Sotirios Delimanolis Apr 28 '13 at 15:55

2 Answers2

2

Answer to your first question:

response.setHeader( "Content-Disposition", "attachment;filename=" + filename );
//for zip file
response.setContentType("application/zip");

Here is the list of mime type for various content types.

I found a solution here. You can change return type to string and return error view name in case of error otherwise return null. What I understood from the link is that returning null and return type void is similar.

Community
  • 1
  • 1
Amit Khanna
  • 489
  • 4
  • 16
  • the download type is sql & there's no reading from a file, that will be created by receiving data from somewhere. & what about my second question ? (I edited my question !) – Mehdi Apr 28 '13 at 05:40
  • content type is used by the browser to choose a default program to display the file. Since your file is .sql, I believe that you are expecting the user to save it or open in a text editor so "application/octet-stream" or "text/plain" should work. Sorry I'm not an expert in spring MVC, but still finding a solution for you. Will reply as soon as i get it. – Amit Khanna Apr 28 '13 at 05:56
  • for the first question, your answer solved my problem, second question answer is "returning null" so nothing will happen & the page will still remain at the same place, so please edit your answer & add the second answer & I'm going to rate it & choose it as the correct answer! ... thanks a lot! – Mehdi Apr 28 '13 at 09:26
1

What stops you from using response.sendRedirect(errorPage)?

apurvc
  • 740
  • 4
  • 12
  • coz this prob is bout streaming data 2 user, we don't need 2 redirect the user when he/she click on the download link & they have 2 stay @ the same page but what if there was an exception when the data was streaming Exceptions like heap-overflow or etc, so we do need to redirect the user to another page like an error page coz our user must know what is really going on inside the system so I figured out instead of making the method void, we can make it string & return null in this situation so that for the catch exceptions we can simple redirect to another page & everything is allright ! – Mehdi Apr 30 '13 at 11:13