下载jar中classes下的文件

问题描述

项目之前被下载的文档是存放在构建后的项目包/{basedir}/manual_file路径下。之后因为项目改为轻门户后端,仅使用jar包方式启动,造成原本的下载文档不可用。对下载文档部分进行了调整,对过程进行一下记录。

原方案

package.xml中配置内容

复制代码
        <fileSet>
            <directory>${basedir}/manual_file</directory>
            <outputDirectory>manual_file</outputDirectory>
            <includes>
                <include>*.docx</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>

下载

复制代码
    @Override
    public void downloadManual(String projectPath, HttpServletRequest request, HttpServletResponse response) {
        String manualPath = StringUtils.join(new String[]{projectPath, "/manual_file/", "test.docx"});
        DownloadUtils.downloadByFilePath(manualPath, request, response);
    }

下载工具类

复制代码
package com.changkong.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadUtils {

    public static void downloadByFilePath(String path, HttpServletRequest request, HttpServletResponse response) {
        downloadFile(new File(path), request, response);
    }

    public static void downloadFile(File file, HttpServletRequest request, HttpServletResponse response) {
        InputStream fis = null;
        OutputStream outputStream = null;
        try {
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

            // 以流的形式下载文件。
            fis = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            // 清空response
            response.reset();

            String realname = filename;
            String agent = (String)request.getHeader("USER-AGENT");
            if(agent != null && agent.toLowerCase().indexOf("firefox") > 0)//火狐浏览器下采用base64编码
            {
                realname = "=?UTF-8?B?" + (new String(org.apache.commons.codec.binary.Base64.encodeBase64(realname.getBytes("UTF-8")))) + "?=";
            }
            else {
                realname = URLEncoder.encode(realname, "UTF-8");
            }

            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" +realname);
            response.addHeader("Content-Length", "" + file.length());
            outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (fis != null ) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
调整后的方案

test.docx当前位置

复制代码
/src/main/resources/test.docx

下载

复制代码
    @Override
    public void downloadManual(HttpServletRequest request, HttpServletResponse response){
        try {
            InputStream inputStream = LocationUtil.class.getResourceAsStream("/test.docx");
            File file = new File("test.docx");
            FileUtils.copyInputStreamToFile(inputStream, file);
            inputStream.close();
            DownloadUtils.downloadFile(file,request,response);
        } catch (Exception e) {
            log.error("文档不存在:{}", e.getMessage());
        }
    }
相关推荐
2301_803554528 分钟前
正向代理,反向代理,负载均衡还有nginx
java·nginx·负载均衡
要开心吖ZSH9 分钟前
软件设计师备考-(十六)数据结构及算法应用(重要)
java·数据结构·算法·软考·软件设计师
向上的车轮17 分钟前
基于Java Spring Boot的云原生TodoList Demo 项目,验证云原生核心特性
java·spring boot·云原生
程序员清风20 分钟前
快手一面:为什么要求用Static来修饰ThreadLocal变量?
java·后端·面试
逍遥德20 分钟前
Java8 Comparator接口 和 List Steam 排序使用案例
java·spring boot·list·排序算法
前行的小黑炭38 分钟前
Android :如何快速让布局适配手机和平板?
android·java·kotlin
_BugMan2 小时前
【IDEA】干活?一个IDEA即可,集成开发平台打造攻略
java·ide·intellij-idea
YA3333 小时前
java设计模式二、工厂
java·开发语言·设计模式
金色天际线-3 小时前
Nginx 优化与防盗链配置指南
java·后端·spring
我爱挣钱我也要早睡!4 小时前
Java 复习笔记
java·开发语言·笔记