Problems downloading file using content

I want my program to have a popup save as before downloading the file, but when I start my servlet, it automatically starts downloading the file. What am I missing here?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream outputStream = response.getOutputStream(); FileInputStream fis=new FileInputStream("E:/sound.mp3"); response.setContentLength(fis.available()); response.setContentType("audio/basic"); response.addHeader("content-disposition", "attachment;filename=abc.mp3"); while(true){ int read = fis.read(); if(read==-1)break; outputStream.write(read); } fis.close(); } 
+6
source share
4 answers

Your program is not desktop / standalone, as it is a servlet running on a server. When you run it in Eclipse by right-clicking and run as β†’ run on server , Eclipse actually opens a web page to display the results. Therefore, your program is now a web application, and Eclipse (or the page that opens) is a client . The client saves the information you sent, not your program. Got?

The content-disposition header is only for specifying the name of the file to download. Browser settings determine whether the Save As window opens or not. You cannot control it.

For example, in Google Chrome, in Setting / Advanced Setting / Downloads there is an Ask where to save each file before downloading option. Only if this option is selected, it will open the desired dialog box. Otherwise, it will save it in the default location (also defined in the browser settings). Similar options exist for all browsers.

Also note that depending on the content-type header, the browser will try to display the content, rather than loading it . For example, the browser will try to display texts and html. But then you can force the download by setting the header to a non-displayable type:

response.setContentType("application/octet-stream");

If you do not want to create a web application: since your program runs on a server It simply sends information and is executed. This is a client program that decides what to do with it. In this case, the client is a browser (or Eclipse opens a browser page). Headers, such as the content-disposition header, are for browsers. If you want to create your client (Swing client, Android app, iPhone app) that is NOT a browser, then the client will receive information from the server and decide what to do with it (display it or save it in any way), even ignoring the headers HTTP

+6
source

Try to find here: http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm

print the main statement in your code and put run(new FileChooserTest(), 250, 110); into your own code. If I did this, I would do an int with the name saveStatus and 3 finals that are 0, 1 and 2 with the name waiting , save and cancel . Then I would do a while loop in your other programming to see if saveStatus equal to waiting to pause your program (but not the dialog). Subsequently, I would do an if statement to find out if saveStatus save is equal. If so, download it, and if not, do not. Just like that.

+2
source

Your problem is Mime-Type. Some types (especially those known to a particular handler) will be loaded directly by most browsers. This helps a little to use the application / binary, but even then some browsers can be configured to load it or interpret the file name extension in the placement handler.

I think most solutions use javascript on the page before the download link.

+2
source

You need to execute the dialog manually, for example. ( http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html ). After the user selects the file, you can start downloading the HTTP request (to your servlet) and save the file to the desired path.

-1
source

Source: https://habr.com/ru/post/977670/


All Articles