www.dextsolution.com
DEXTUPLOAD
JK
menu toggleProduct information > Downloading a file

Downloading a file

In general, if the server's response data to an HTTP GET/POST request is in a format that the browser cannot interpret, the browser downloads the target as an attachment.

For example, if the data downloaded through a hyperlink set in the Anchor(a) tag is a jpg file that the browser can interpret, the image is displayed in a new window (or tab) or the current window. Unlike that, in the case of a zip file, the image is displayed in a new window (or tab) or the current window. Generally, the download is performed immediately. If you want to download a file that the browser can interpret, such as a jpg file, directly to disk without displaying it in the browser, you can either rely on the browser's own functionality (context menu - Save As) or send a response header from the server that forces the browser to download it. It must be shipped. A new property has been added to HTML5 that forces files to be downloaded regardless of the target format (MIME), but it is not supported by all browsers (as of early 2016), so files cannot be downloaded purely through client settings.

The DEXTUploadJK product processes files or non-file data from the server so that they can be downloaded directly as attachments without the browser's interpretation.

# When exposing the file path as is
<a href="http://domain/path/music/Beethoven/Moonlight.mp3">Beethoven Moonlight Sonata</a>

# When using the download.do path to download without exposing the path of the actual file
<a href="http://domain/path/download.do?key=12345">Beethoven Moonlight Sonata</a>

In general, downloading using the DEXTUploadJK product means that the path to the target file is hidden or the path to the target file is not exposed on the web. Therefore, to download a file, you must provide a JSP page or servlet that can download the file.

Servet/JSP environment

Use the FileDownload class to download files.

public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
        response.setCharacterEncoding("UTF-8");
		
        try {
            String key = request.getParameter("key");
            
            // Obtain the path to the file corresponding to the key.
            File target = new File("File path");
            
            FileDownload fileDownload = new FileDownload();
            fileDownload.download(request, response, target, "File name to use when downloading", "application/octet-stream", false);
            
        } catch (Exception e) {
            throw new ServletException(e);
        }
    }
}

When downloading files, it is not recommended that clients request POST rather than GET. This is because the purpose of downloading a file is not to update the status by transmitting the client's data to the server. Additionally, when obtaining binary data in a Java web application environment, it is recommended to use a servlet rather than a JSP page. In the case of JSP pages, tags contained in the page may be sent along with the download data due to a developer's mistake. Of course, you can delete all elements on the page except for the indicator, but there is a high possibility that problems may occur because parts such as blank strings may remain due to developer mistakes. Therefore, it is recommended to define and use the servlet yourself.

Spring MVC or Spring Boot environment

The JKFileDownloadView class is provided to download files in the Spring environment.

@Controller
public class DownloadController {

    @RequestMapping(value = "/download.do", method = RequestMethod.GET)
    public ModelAndView download(@RequestParam(value = "key") String key) {
    
        // Obtain the path to the file corresponding to the key.
        File target = new File("File path");
		
        // Create a JKFileDownloadView object to download the file.
        JKFileDownloadView view = new JKFileDownloadView();
		
        view.setFile(target);
        view.setFilename("File name to use when downloading");
        view.setMime("application/octet-stream");
        view.setCharsetName("UTF-8");
		
        // Returns a view object that handles the download task.
        return new ModelAndView(view);
    }
    
}