JavaWeb-Response-下载指定路径下的文件

 

下载文件思路

1、获取下载文件的路径

2、下载的文件名

3、设置想办法让浏览器能够支持下载我们需要的东西

4、获取下载文件的输入流

5、创建缓冲区

6、获取OutputStream对象

7、将FileOutStream流写入到buffer缓冲区

8、使用OutputStream将缓冲区中的数据输出到客户端

实现过程

新建一个javaweb项目

第一步:

image.png

第二步:填写项目组织唯一标识符合项目唯一标识符

image.png

第三步:配置maven仓库

image.png

第四步:添加项目名和保存路径

image.png

具体实现-pom文件

pom.xml文件中的内容

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>java-00-servlet-liu</artifactId>
        <groupId>com.dmsd.servlet</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>response</artifactId>
    <packaging>war</packaging>

    <name>response Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>response</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

具体实现-web.xml-在web.xml中配置servlet

web.xml文件:设置当进入到down路径下时下载指定文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
    <!--配置servlet -->
    <servlet>
        <servlet-name>file</servlet-name>
        <servlet-class>com.dmsd.servlet.FileServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>file</servlet-name>
        <url-pattern>/down</url-pattern>
    </servlet-mapping>
</web-app>

代码实现-FileServlet

package com.dmsd.servlet;

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;
import java.net.URLEncoder;

public class FileServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取下载文件的路径
        //String realPath = this.getServletContext().getRealPath("/1.jpg");
        String realPath ="F:\\Java学习\\JavaWeb\\java-00-servlet-liu\\response\\target\\classes\\流.jpg";//访问固定路径下的文件
        //2、下载的文件名,截取最后一个/符号,剩下的就是文件名
        String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
        //3、设置javaweb下载设置响应头--想办法让浏览器能够支持下载我们需要的东西,
        //设置响应头以下载的方式返回到浏览器,通过URLEncoder进行编码转换,使其可以识别中文
        resp.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));
        //4、获取下载文件的输入流
        FileInputStream in = new FileInputStream(realPath);
        //5、创建缓冲区
        int len = 0;
        byte[] buffer = new byte[1024];
        //6、获取OutputStream对象
        ServletOutputStream out = resp.getOutputStream();
        //7、将FileOutStream流写入到buffer缓冲区
        //使用OutputStream将缓冲区中的数据输出到客户端
        while((len = in.read(buffer))>0){
            out.write(buffer,0,len);
        }
        //关闭流
        in.close();
        out.close();
        //8、
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

实现效果:

在路径:http://localhost:8080/response_war/  后输入down(web.xml已经配置好),即可实现文件下载

 

整体目录结构

image.png

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值