首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过httpResponse spring boot下载Java文件

通过httpResponse spring boot下载Java文件
EN

Stack Overflow用户
提问于 2019-08-19 16:07:13
回答 1查看 335关注 0票数 0

我想从一个网站的服务器上下载一个pdf文件。当我按下一个按钮时,我会向spring boot服务器发送一个ajax请求。我创建了httpservletresponse的输出流,并将文件字节写入其中。在javascript中,我得到了pdf文件信息,但我不知道如何下载它们。我认为问题出在jquery中的调用。我不确定我是否使用了正确的内容类型。

以下是我的请求的请求和响应头:

代码语言:javascript
复制
Request-Headers
    Accept: application/json, text/javascript, */*; q=0.01
    Accept-Encoding: gzip, deflate, br
    Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
    Connection: keep-alive
    Content-Type: application/pdf
    Host: localhost:8080
    Referer: http://localhost:8080/arbeitsvorratsliste
    Sec-Fetch-Mode: cors
    Sec-Fetch-Site: same-origin
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3880.4 Safari/537.36
    X-Requested-With: XMLHttpRequest
代码语言:javascript
复制
Response-Headers
Cache-Control: no-cache
Content-Disposition: attachment; filename=44005001.25759.pdf
Content-Encoding: UTF-8
Content-Type: application/pdf;charset=ISO-8859-1
Date: Mon, 19 Aug 2019 07:38:36 GMT
Expires: 0
Transfer-Encoding: chunked
代码语言:javascript
复制
@GetMapping("/print.do")
public void doPrint()
{
    final HttpServletResponse response = getResponse();
    RequestUtils.setResponseHeaders(response, "44005001.25759.pdf");

    Path path = Paths.get(settings.getPrintPath() + "\\44005001.25759.pdf");
    try
    {
        ServletOutputStream out = response.getOutputStream();
        InputStream in = new ByteArrayInputStream(Files.readAllBytes(path));

        byte[] outputByte = new byte[4096];
        // copy binary contect to output stream
        while (in.read(outputByte, 0, 4096) != -1)
        {
            out.write(outputByte, 0, 4096);
        }
        in.close();
        out.flush();
        out.close();
    }
    catch (IOException e)
    {
        LOG.error("WorkListController.doPrint(): Error while read from file.", e);
    }
    finally
    {
    }
}
代码语言:javascript
复制
jquery:
myProject.sendRequest('print.do', 'GET', null, null, null, null, null, "application/pdf");
代码语言:javascript
复制
myProject.sendRequest = function (url, method, dataGetter, onSuccess, onError, onDone, cache, contentType, noAuth, _form) {
var params = {
        url: url,
        type: method,
        data: getData(),
        dataType: 'json',
        contentType: 'application/json;charset=utf-8'
    };

    if (contentType)
        params.contentType = contentType;

    jQuery.ajax(params).always(function (xhrOrData, status, xhrOrException) {
        var xhr = (xhrOrData && typeof xhrOrData.status === 'number' && xhrOrData.status >= 0) ? xhrOrData : xhrOrException;
        if (xhr && typeof xhr.status === 'number' && xhr.status >= 0) {
            var json = volante.removeJsonPrefix(xhr.responseText);
            try {
                json = JSON.parse(json);
            } catch (e) {
                // error: json is undefined
            }
            checkResponse(xhr.status, json);
        } else {
            execError('Es trat ein interner Fehler auf.');
        }
    }).done(execDone);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-12 17:35:44

我已经找到问题了,如果你想下载一个文件,你必须添加注释@PostMapping和@ResponseBody。在javascript中,我没有调用ajax请求,但我调用了表单提交。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57552696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档