Picture implementation default download instead of Open Picture (Java edition)

Source: Internet
Author: User

On the Web page, if the address of our hyperlink corresponds to a JPG file, TXT file, etc., when clicking the link, the browser default is to open these files instead of downloading, then how to implement the default download.

Can be achieved by writing a download.jsp yourself.

<span style= "FONT-SIZE:18PX;" ><%@ page contenttype= "text/html; Charset=utf-8 "%><%@ page import=" java.net.* "%><%//get file name and path  String filename = request.getparameter ( "FileName"); String filepath = request.getparameter ("filepath"); String displayfilename = urlencoder.encode (filename, "UTF-8"); try {response.setcontenttype ("Application/x-download" ); Response.setheader ("Content-disposition", "attachment;filename=\" "+ displayfilename +" \ ");    RequestDispatcher dis = application.getrequestdispatcher (filepath + filename);    if (dis! = null) {        dis.forward (request,response);    }    Response.flushbuffer ();} catch (Exception e) {    System.out.println ("Download canceled:" + filepath + filename);} out.clear ();    out = Pagecontext.pushbody ();%></span>
when we want to link the picture (or other browser by default open format, here take the picture as an example), the corresponding file name and address into the download.jsp filename and filepath parameters, the specific wording as follows


<a class= "blue-line-a" href= "/packtool/download.jsp? filename=wingui.exe&amp; filepath=http://127.0.0.1:8080/sharefolder/other/wingui.exe ">/other/WinGUI.exe</a>

In this way, we can implement the default download of the file, not the default browser open.

Picture implementation default download instead of Open Picture (Java edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.