mysql - Java从服务器端下载文件保存到本地,不是下载的图片?
巴扎黑
巴扎黑 2017-04-18 09:53:30
[Java讨论组]
public static void downloadFile(String urlPath, String filePath) throws IOException {
        URL url = new URL(urlPath);
        String name = urlPath.substring(urlPath.lastIndexOf("=")+1);
        String filePaths = filePath+"/"+name;
        File dirFile = new File(filePath);
        if (!dirFile.exists()) {
            dirFile.mkdir();
        }
        URLConnection connection = url.openConnection();
        InputStream in = connection.getInputStream();
        FileOutputStream os = new FileOutputStream(filePaths);
        byte[] buffer = new byte[4 * 1024];
        int read;
        while ((read = in.read(buffer)) > 0) {
            os.write(buffer, 0, read);
        }
        os.close();
        in.close();
    }

    public static void main(String[] args) {
        String urlPath = "http://10.192.32.111:80/production-jcw/getDownload.do?fileName=49e23d3a-5e11-432d-9f33-91c93130b37b.dwg";
        String filePath = "C:/Users/MrLi/Desktop/";
        try {
            downloadFile(urlPath, filePath);
            System.out.println("下载成功");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("下载失败");
        }
    }
巴扎黑
巴扎黑

全部回复(0)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号