先是上代码
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*/
@Testpublic 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");}@Testpublic 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;}}