• JAVA代码实现文件下载,网络URL的下载到本地指定目录,删除。文件的上传


    先是上代码

    package org.jeecgframework.web.oct.oa.purchase.service.impl;
    
    import org.junit.Test;
    import java.io.*;
    import java.net.URL;
    import java.net.URLConnection;
    
    
    /**
     * @author :wuqp(wuqp@octvision.com)
     * @date :Created in 2022/11/24 10:38
     * @description:ceshi
     * @modified By:
     * @version: 1.0$
     */
    public class TestUpload {
        /**
         * @从制定URL下载文件并保存到指定目录
         * @param filePath 文件将要保存的目录
         * @param method 请求方法,包括POST和GET
         * @param url 请求的路径
         * @return
         */
    @Test
        public  void testwqp() throws IOException {
    
            String urlPath =  "http://文件下载的地址";   //文件URL地址
            String cookie = "JEECGINDE~~~~~~~~~~ECw";
            URL url = new URL(urlPath);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("Cookie", cookie);
            conn.setDoInput(true);
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            InputStream inputStream = conn.getInputStream();
        byte[] buffer = new byte[1024*1024];
        int len;
        java.io.ByteArrayOutputStream byteArrayOutputStream = new java.io.ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, len);
        }
        byteArrayOutputStream.close();
        File file = new File("D:/Data/octOaContactFileTemp/" + "png.png");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(byteArrayOutputStream.toByteArray());
        fileOutputStream.close();
        inputStream.close();
        System.out.println("下载成功:" + "D:/Data/octOaContactFileTemp/" + "png.png");
        }
    
    @Test
        public void deleteFile()
        {
            String filePath="D:/Data/octOaContactFileTemp/png.png";
            boolean flag = false;
            File file = new File(filePath);
            // 路径为文件且不为空则进行删除
            if (file.isFile() && file.exists())
            {
                file.delete();
                flag = true;
            }
    //        return flag;
        }
    
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
  • 相关阅读:
    基于RM编译码的协作MIMO系统误码率matlab仿真,对比不同RM编译码参数
    AMEYA360:永铭固液混合铝电解电容帮助企业级固态硬盘稳定运行
    .NET 9 预览版6发布
    信息系统项目管理师必背核心考点(六十)项目集管理
    2022年夏季《移动软件开发》实验报告六
    k8s集群搭建操作步骤
    IDR 学习笔记
    [linux]、ssh服务详解
    所有字母异位词
    腾讯云服务器如何手动搭建java web环境?
  • 原文地址:https://blog.csdn.net/qq_31647655/article/details/128134938