当前位置:   article > 正文

下载jar中classes下的文件

下载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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

下载

    @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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5

下载工具类

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();
                }
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
调整后的方案

test.docx当前位置

/src/main/resources/test.docx
  • 1

下载

    @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());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/58705
推荐阅读
相关标签
  

闽ICP备14008679号