package com.yp;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentDisposition;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
public class fileservletjava extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.获取下载文件的路径
String realpath=this.getServletContext().getRealPath("/1.JPG");//项目下面的根目录
System.out.print("文件下载的路径是:"+realpath);
//2.下载文件的名字是什么
String filename=realpath.substring(realpath.lastIndexOf("\\")+1);
//3.想办法让浏览器支持下载我们需要的东西
resp.setHeader("Content-disposition","attachment;filename"+filename);
//4.获取下
javaweb-外网访问之图片的下载
最新推荐文章于 2022-05-19 18:59:32 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)