状态404-当我在angularjs中调用下载api时

时间:2018-09-07 01:31:39

标签: angularjs spring-boot download

我正在尝试下载服务器上的文件。

点击download.html上的下载按钮会导致状态404错误。

在chrome的devtools中检查网络会导致调用下载,但/ api /下载路径找不到404。

我该怎么办?

FileDownloadController

    @RequestMapping(method = RequestMethod.GET)
    public void fildDownload(HttpServletRequest request, 
                             HttpServletResponse response) throws Exception {

    request.setCharacterEncoding("UTF-8");

    @SuppressWarnings("resource")
    BoneCPDataSource dataSource = new BoneCPDataSource();
    Date date = new Date();
    System.out.println(dataSource.getJdbcUrl());
    try {
        String fileURL = dataSource.getJdbcUrl();
        fileURL += "/";
        String savePath = "C:/Users/me/Downloads";

        String fileName = "info.mv.db";

        String downloadFileName = fileName + "_" + date.toString();

        InputStream in = null;
        OutputStream os = null;
        File file = null;
        boolean skip = false;
        String client = "";

        try {
            file = new File(savePath, fileName);
            in = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            skip = true;
        }

        client = request.getHeader("User-Agent");

        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Description", "Database File");

        if (!skip) {
            if (client.indexOf("MSIE") != -1) {
                 response.setHeader("Content-Disposition", "attachment; filename=\""
                            + java.net.URLEncoder.encode(downloadFileName, "UTF-8").replaceAll("\\+", "\\ ") + "\"");

                } else if (client.indexOf("Trident") != -1) {
                    response.setHeader("Content-Disposition", "attachment; filename=\""
                            + java.net.URLEncoder.encode(downloadFileName, "UTF-8").replaceAll("\\+", "\\ ") + "\"");
                } else {

                    response.setHeader("Content-Disposition",
                            "attachment; filename=\"" + new String(downloadFileName.getBytes("UTF-8"), "ISO8859_1") + "\"");
                    response.setHeader("Content-Type", "application/octet-stream; charset=utf-8");
                }
                response.setHeader("Content-Length", "" + file.length());
                os = response.getOutputStream();
                byte b[] = new byte[(int) file.length()];
                int leng = 0;
                while ((leng = in.read(b)) > 0) {
                    os.write(b, 0, leng);
                }
            } else {
                response.setContentType("text/html;charset=UTF-8");
                System.out.println("not found file");
            }
            in.close();
            os.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

download.service.js

angular.module('testApp')
.factory('fileDownload', ['$resource', function ($resource) {
    return $resource('/api/download',{

    }, {
        downloadFile: {
            method: 'GET'
            }
        }
    );
}]);

download.controller.js

angular.module('testApp')
.controller('controller', function($rootScope, $scope, $http, fileDownload){
$scope.serverInfoDownload = function(){
        /* */
        fileDownload.downloadFile(function (success){
            console.log("success", success);
        }, function(error){
            console.log("error", error);
        });
}

download.html

<button type="submit" class="btn btn-large btn-danger" ng-click="serverInfoDownload()">download</button>

0 个答案:

没有答案