上传下载文件

下载文件

spring resttemplate下载文件

String suffix = url.substring(url.lastIndexOf("."));
if(CheckUtils.isEmpty(filename)) {
    filename = url.substring(url.lastIndexOf("/") + 1);
} else if(!CheckUtils.isEmpty(suffix)) {
    filename = filename.substring(0, filename.lastIndexOf("."));
    filename += suffix;
}
OutputStream outputStream = null;
byte[] result = restTemplate.getForObject(url, byte[].class);
File file = new File(downloadLocation, filename);
if (!file.getParentFile().exists()) {
    file.getParentFile().mkdirs();
}
if (!file.exists()) {
    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
try {
    outputStream = new FileOutputStream(file);
    outputStream.write(result);
    outputStream.flush();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (outputStream != null) {
        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容