Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring boot中公开资源(如可下载链接)_Java_Spring Boot_Cordova Plugins - Fatal编程技术网

Java 如何在Spring boot中公开资源(如可下载链接)

Java 如何在Spring boot中公开资源(如可下载链接),java,spring-boot,cordova-plugins,Java,Spring Boot,Cordova Plugins,实际上,我有一个spring启动服务器,资源文件夹(src/main/resources/public/myfilefolder/myfiles.apk)中有很多文件。我的要求是用cordova文件传输库下载一个文件。我的第一个想法是将资源链接直接放在组件中: var fileTransfer = new FileTransfer(); var fileURL = "///storage/emulated/0/DCIM/myFile"; var uri = encodeURI("htt

实际上,我有一个spring启动服务器,资源文件夹(src/main/resources/public/myfilefolder/myfiles.apk)中有很多文件。我的要求是用cordova文件传输库下载一个文件。我的第一个想法是将资源链接直接放在组件中:

    var fileTransfer = new FileTransfer();
var fileURL =  "///storage/emulated/0/DCIM/myFile";
var uri = encodeURI("http://myserver/src/main/resources/public/myfilefolder/myfiles.apk");

fileTransfer.download(
    uri,
    fileURL,
    function(entry) {
        alert("download complete: " + entry.toURL());
    },
    function(error) {
        alert("download error source " + error.source);
        alert("download error target " + error.target);
        alert("download error code" + error.code);
    },
    false,
    {

      /*
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }

        */
    }

但是很明显,这并不能很好地工作,因为服务器认为这是一个控制器路由。有没有办法公开此资源以供下载?

您可以在springboot应用程序中公开get请求,您可以在其中传递文件名,它将根据输入下载文件,您可以根据应用程序要求添加下载限制

@GetMapping("/downloadResource")
public ResponseEntity<ByteArrayResource> downloadResource(
        @RequestParam(defaultValue = DEFAULT_FILE_NAME) String fileName) throws IOException {

    MediaType mediaType = MediaTypeUtils.getMediaTypeForFileName(this.servletContext, fileName);

    MediaType mediaType = // Get media type of file
    System.out.println("fileName: " + fileName);
    System.out.println("mediaType: " + mediaType);

    Path path = Paths.get(DIRECTORY + "/" + DEFAULT_FILE_NAME);
    byte[] data = Files.readAllBytes(path);
    ByteArrayResource resource = new ByteArrayResource(data);

    return ResponseEntity.ok()
            // Content-Disposition
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + path.getFileName().toString())
            // Content-Type
            .contentType(mediaType) //
            // Content-Lengh
            .contentLength(data.length) //
            .body(resource);
}
@GetMapping(“/downloadResource”)
公共响应下载资源(
@RequestParam(defaultValue=默认文件名)字符串文件名)引发IOException{
MediaType MediaType=MediaTypeUtils.getMediaTypeForFileName(this.servletContext,fileName);
MediaType MediaType=//获取文件的媒体类型
System.out.println(“文件名:“+fileName”);
System.out.println(“mediaType:+mediaType”);
Path Path=Path.get(目录+“/”+默认文件名);
字节[]数据=文件。readAllBytes(路径);
ByteArrayResource资源=新的ByteArrayResource(数据);
返回ResponseEntity.ok()
//内容配置
.header(HttpHeaders.CONTENT_处置,“附件;文件名=“+path.getFileName().toString())
//内容类型
.contentType(mediaType)//
//内容长度
.contentLength(data.length)//
.机构(资源);
}

我终于拿到了。感谢您的回答,这是我的解决方案:

在spring引导服务器中,您需要以下方法:

@GetMapping(value="/file_download",

produces = MediaType.APPLICATION_OCTET_STREAM_VALUE
  )
@ResponseBody
public FileSystemResource downloadFile(@RequestParam String filename) throws IOException{

//to consume http://youserver/file_download?filename=mytest

try{


  System.out.println("in file download " +filename);

   String path = "src/main/resources/public/makingAPK/"+filename+"/"+filename+".apk"; //path of your file


    return new FileSystemResource(new File(path));
}

catch(Exception e){System.out.println("error in file_download "+e); return null;}

}
在您的cordova插件传输中:

$scope.DownloadFileFromServer = function(url){ //file server location


   var fileURL =  "/storage/emulated/0/DCIM/thefile.apk";

try{

var fileTransfer = new FileTransfer();
var uri = encodeURI("http://yourserver/yourCtrlName/file_download?filename=yourfilename");

fileTransfer.download(
    uri,
    fileURL,
    function(entry) {
        alert("download complete: " + entry.toURL());
    },
    function(error) {
        alert("download error source " + error.source);
        alert("download error target " + error.target);
        alert("download error code" + error.code);
    },
    false,
    {

      /*
        headers: {
            "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
        }

        */
    }
);

URL是
http://myserver/myfilefolder/myfiles.apk
。文件:是的,男人。。。。这就是解决办法,泰,拯救我的一天