статус 404 - когда я позвонил скачать API в angularjs - PullRequest
0 голосов
/ 07 сентября 2018

Я пытаюсь загрузить файл, расположенный на сервере.

Нажатие кнопки загрузки на download.html приводит к ошибке статуса 404.

Проверка сети в chrome devtools приводит к вызову на скачивание, но 404 Not Found для пути / api / download.

Что мне делать?

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>
...