File download
Generally, when the response data of the server to the HTTP GET/POST request is in a format that the browser can not interpret, the browser downloads the target like an attached file.
For example, if the data downloaded via the hyperlink set in the Anchor(a) tag is a jpg file that can be interpreted by the browser, the image is displayed in another window(or tab) or in the current window. However, in the case of a zip file in a different way than that, in general, immediately perform the download. If you want to download a file directly to the disk without processing the file that the browser can interpret like jpg file, it can depend on the browser's own functions(context menu > "Save as a name"). A response header that allows browsers to be forcibly downloaded from them. In HTML 5, an attribute for forcibly downloading the file is added regardless of the target type(mime type), but it is not supported by all browsers (standard in early 2016), so you cannot download a file with pure client settings alone.
DEXTUploadNJ products can set a file to be directly downloaded in the form of attached without browser interpretation.
// It is a case to publish the link of the resource. <a href="http://domain/path/music/Beethoven/Moonlight.mp3">Beethoven Moonlight Sonata</a> // It is a case to use web application for hiding the resource path. <a href="http://domain/path/download.do?key=12345">Beethoven Moonlight Sonata</a>
Generally, downloading using the DEXTUploadNJ product means concealing the path of the target file, or the path of the linked file is an environment not published on the Web. Therefore, you must write a JSP page or a servlet to download a file with the DEXTUploadNJ.
- DEXTUploadNJ + Servlet
-
Use the FileDownload class to download the file.
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"); // Get the path of the file corresponding to key. // ... omission File target = new File("file path"); FileDownload dextnj = new FileDownload(); dextnj.download(request, response, target, "file name to use when downloading", "application/octet-stream", false); } catch (Exception e) { throw new ServletException(e); } } }When downloading files, clients do not recommend requesting with POST, not GET. The reason for downloading the file is not to send the client's data to the server and update the state. In addition, in the Java Web application environment, when trying to obtain binary data, we recommend that you define and use the servlet directly rather than the JSP page. In the case of a JSP page, there is a high possibility that the tag included in the page is overlapped with the download data. Of course, it is said to delete all elements except for the display unit on the page, but because there is a possibility that a part like an empty character string may remain due to a developer's mistake. Therefore, it is recommended that you define the servlet directly and use it.
- DEXTUploadNJ + Spring Web Framework
-
Provide the DEXTUploadNJFileDownloadView view class 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) { // Get the path of the file corresponding to key . // ... omission File target = new File("file path"); // Create a DEXTUploadNJFileDownloadView object to download the file. DEXTUploadNJFileDownloadView view = new DEXTUploadNJFileDownloadView(); view.setFile(target); view.setFilename("file name to use when you download"); view.setMime("application/octet-stream"); view.setCharsetName("UTF-8"); // Return the view object that handles the download task. return new ModelAndView(view); } }