• 超链接指向的资源如果能被浏览器解析,则点击后显示;不能的话则展示下载提示框 ``` <!DOCTYPE html>

      图片

    1. - 针对以上问题,我们想让浏览器弹窗提示下载,无论什么类型
    2. - download.html

    <!DOCTYPE html>

    1. <!-- 超链接指向的资源如果能被浏览器解析,则点击后显示;不能的话则展示下载提示框-->
    2. <a href="/WebDemo_war_exploded/downloadServlet?filename=图标.png">图片</a>

    1. - DownloadServlet
    2. > response.setHeader("content-disposition","attachment;filename="+filename);

    package com.code.web;

    import com.code.web.util.DownloadUtils;

    import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException;

    @WebServlet(“/downloadServlet”) public class DownloadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    1. ServletContext servletContext = this.getServletContext();
    2. // 文件地址
    3. String filename = request.getParameter("filename");
    4. String realPath = servletContext.getRealPath("/imgs/" + filename);
    5. // 设置响应体文件类型
    6. String mimeType = servletContext.getMimeType(realPath);
    7. response.setHeader("content-type",mimeType);
    8. // 防止中文乱码
    9. String header = request.getHeader("user-agent");
    10. filename = DownloadUtils.getileName(header, filename);
    11. // 打开响应体方式
    12. response.setHeader("content-disposition","attachment;filename="+filename);
    13. // 响应体图片字节数据
    14. FileInputStream fileInputStream = new FileInputStream(realPath);
    15. ServletOutputStream outputStream = response.getOutputStream();
    16. byte[] buff = new byte[1024*8];
    17. int len = 0;
    18. while ((len = fileInputStream.read(buff)) != -1){
    19. outputStream.write(buff,0,len);
    20. }
    21. fileInputStream.close();
    22. }
    23. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    24. this.doPost(request, response);
    25. }

    }

    1. <a name="xSeNm"></a>
    2. #### 防止中文名乱码工具类

    package com.code.web.util;

    import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Base64;

    public class DownloadUtils {

    1. public static String getileName(String agent,String filename) throws UnsupportedEncodingException {
    2. if (agent.contains("MSIE")) {
    3. // IE浏览器
    4. filename = URLEncoder.encode(filename, "utf-8");
    5. filename = filename.replace("+", " ");
    6. } else if (agent.contains("Firefox")) {
    7. // 火狐浏览器
    8. Base64.Encoder encoder = Base64.getEncoder();
    9. String str = encoder.encodeToString(filename.getBytes("utf-8"));
    10. filename = "=?utf-8?B?" + str + "?=";
    11. } else {
    12. // 其它浏览器
    13. filename = URLEncoder.encode(filename, "utf-8");
    14. }
    15. return filename;
    16. }

    } ```