src/main/java/org/edgegallery/appstore/infrastructure/files/LocalFileServiceImpl.java
public File downloadFile(String url, String parentPath, String targetAppstore) {
        if (!createParent(parentPath)) {
            LOGGER.error("create file parent fail");
            throw new FileOperateException("create file parent path failed.", ResponseConst.RET_MAKE_DIR_FAILED);
        }
        RestTemplate restTemplate = new RestTemplate();

        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.ALL);
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(list);

        try {
            ResponseEntity<byte[]> response = restTemplate
                .exchange(url + "?targetAppstore=" + targetAppstore, HttpMethod.GET, new HttpEntity<byte[]>(headers),
                    byte[].class);
            if (response.getStatusCode() != HttpStatus.OK) {
                LOGGER.error("download file error, response is {}", response.getBody());
                throw new AppException("download file from source appstore failed.",
                    ResponseConst.RET_DOWNLOAD_FROM_APPSTORE_FAILED);
            }

            byte[] result = response.getBody();
            if (result == null) {
                throw new AppException("download file from source appstore null.",
                    ResponseConst.RET_DOWNLOAD_FROM_APPSTORE_FAILED);
            }
            String fileName = Optional.ofNullable(response.getHeaders().get("Content-Disposition"))
                .orElseThrow(() -> new AppException("response header Content-Disposition is null",
                    ResponseConst.RET_DOWNLOAD_FROM_APPSTORE_FAILED)).get(0)
                .replace("attachment; filename=", "");
            File file = new File(parentPath + File.separator + fileName);
            if (!file.exists() && !file.createNewFile()) {
                LOGGER.error("create download file error");
                throw new FileOperateException("create download file error.", ResponseConst.RET_CREATE_FILE_FAILED);
            }

            try (InputStream inputStream = new ByteArrayInputStream(result);
                 OutputStream outputStream = new FileOutputStream(file)) {
                int len = 0;
                byte[] buf = new byte[1024];
                while ((len = inputStream.read(buf, 0, 1024)) != -1) {
                    outputStream.write(buf, 0, len);
                }
                outputStream.flush();
            }

            return file;
        } catch (RuntimeException | IOException e1) {
            LOGGER.error("download file error: {}", e1.getMessage());
            throw new AppException("download file failed.", ResponseConst.RET_DOWNLOAD_FROM_MESSAGE_FAILED);
        }
    }
/**
     * download file to parent path directory.
     *
     * @param url url
     * @param parentPath parent path
     * @return download file
     */
download file to parent path directory.
@param url url
@param parentPath parent path
@return download file
download file to parent path directory . @param url url @param parentPath parent path @return download file
public File downloadFile(String url, String parentPath, String targetAppstore) {
        if (!createParent(parentPath)) {
            LOGGER.error("create file parent fail");
            throw new FileOperateException("create file parent path failed.", ResponseConst.RET_MAKE_DIR_FAILED);
        }
        RestTemplate restTemplate = new RestTemplate();
        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.ALL);
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(list);
        try {
            ResponseEntity<byte[]> response = restTemplate
                .exchange(url + "?targetAppstore=" + targetAppstore, HttpMethod.GET, new HttpEntity<byte[]>(headers),
                    byte[].class);
            if (response.getStatusCode() != HttpStatus.OK) {
                LOGGER.error("download file error, response is {}", response.getBody());
                throw new AppException("download file from source appstore failed.",
                    ResponseConst.RET_DOWNLOAD_FROM_APPSTORE_FAILED);
            }
            byte[] result = response.getBody();
            if (result == null) {
                throw new AppException("download file from source appstore null.",
                    ResponseConst.RET_DOWNLOAD_FROM_APPSTORE_FAILED);
            }
            String fileName = Optional.ofNullable(response.getHeaders().get("Content-Disposition"))
                .orElseThrow(() -> new AppException("response header Content-Disposition is null",
                    ResponseConst.RET_DOWNLOAD_FROM_APPSTORE_FAILED)).get(0)
                .replace("attachment; filename=", "");
            File file = new File(parentPath + File.separator + fileName);
            if (!file.exists() && !file.createNewFile()) {
                LOGGER.error("create download file error");
                throw new FileOperateException("create download file error.", ResponseConst.RET_CREATE_FILE_FAILED);
            }
            try (InputStream inputStream = new ByteArrayInputStream(result);
                 OutputStream outputStream = new FileOutputStream(file)) {
                int len = 0;
                byte[] buf = new byte[1024];
                while ((len = inputStream.read(buf, 0, 1024)) != -1) {
                    outputStream.write(buf, 0, len);
                }
                outputStream.flush();
            }
            return file;
        } catch (RuntimeException | IOException e1) {
            LOGGER.error("download file error: {}", e1.getMessage());
            throw new AppException("download file failed.", ResponseConst.RET_DOWNLOAD_FROM_MESSAGE_FAILED);
        }
    }
public File downloadFile ( String url , String parentPath , String targetAppstore ) { if ( ! createParent ( parentPath ) ) { LOGGER . error ( "create file parent fail" ) ; throw new FileOperateException ( "create file parent path failed." , ResponseConst . RET_MAKE_DIR_FAILED ) ; } RestTemplate restTemplate = new RestTemplate ( ) ; List < MediaType > list = new ArrayList < > ( ) ; list . add ( MediaType . ALL ) ; HttpHeaders headers = new HttpHeaders ( ) ; headers . setAccept ( list ) ; try { ResponseEntity < byte [ ] > response = restTemplate . exchange ( url + "?targetAppstore=" + targetAppstore , HttpMethod . GET , new HttpEntity < byte [ ] > ( headers ) , byte [ ] . class ) ; if ( response . getStatusCode ( ) != HttpStatus . OK ) { LOGGER . error ( "download file error, response is {}" , response . getBody ( ) ) ; throw new AppException ( "download file from source appstore failed." , ResponseConst . RET_DOWNLOAD_FROM_APPSTORE_FAILED ) ; } byte [ ] result = response . getBody ( ) ; if ( result == null ) { throw new AppException ( "download file from source appstore null." , ResponseConst . RET_DOWNLOAD_FROM_APPSTORE_FAILED ) ; } String fileName = Optional . ofNullable ( response . getHeaders ( ) . get ( "Content-Disposition" ) ) . orElseThrow ( ( ) -> new AppException ( "response header Content-Disposition is null" , ResponseConst . RET_DOWNLOAD_FROM_APPSTORE_FAILED ) ) . get ( 0 ) . replace ( "attachment; filename=" , "" ) ; File file = new File ( parentPath + File . separator + fileName ) ; if ( ! file . exists ( ) && ! file . createNewFile ( ) ) { LOGGER . error ( "create download file error" ) ; throw new FileOperateException ( "create download file error." , ResponseConst . RET_CREATE_FILE_FAILED ) ; } try ( InputStream inputStream = new ByteArrayInputStream ( result ) ; OutputStream outputStream = new FileOutputStream ( file ) ) { int len = 0 ; byte [ ] buf = new byte [ 1024 ] ; while ( ( len = inputStream . read ( buf , 0 , 1024 ) ) != - 1 ) { outputStream . write ( buf , 0 , len ) ; } outputStream . flush ( ) ; } return file ; } catch ( RuntimeException | IOException e1 ) { LOGGER . error ( "download file error: {}" , e1 . getMessage ( ) ) ; throw new AppException ( "download file failed." , ResponseConst . RET_DOWNLOAD_FROM_MESSAGE_FAILED ) ; } }
download file to parent path directory.
download file to parent path directory .