服务端通知浏览器以下载的方式打开图片
更新:HHH   时间:2023-1-7


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo4 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //服务端通知浏览器以下载的方式打开图片
        response.setHeader("content-disposition","attachment;filename=d1.jpg");
        InputStream is = new FileInputStream(new File(""D:/TOOL/MyEclipseSpace/day04/WebRoot/images/cc.jpg"));
        OutputStream os = response.getOutputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        while( (len=is.read(buf))>0 ){
            os.write(buf,0,len);
        }
        is.close();
        os.close();
    }
}

返回web开发教程...