当前位置: 首页 > news >正文

下载远程服务器文件

  • 业务需求:下载某云盘的视频文件存储到本地

  • 测试代码
@RequestMapping("testVideo")public String test() {try {SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd/");//组装本地保存地址StringBuilder filePath = new StringBuilder(StorePathUtil.getStorePath());// \\192.168.20.2\store\stu\upload\video\msyk\2023\08\01\EN123123JD\1filePath.append("upload").append(BaseConstant.SEPERATOR).append("video").append(BaseConstant.SEPERATOR).append("msyk").append(BaseConstant.SEPERATOR).append(DATE_FORMAT.format(new Date())).append(BaseConstant.SEPERATOR).append("asdasda").append(BaseConstant.SEPERATOR).append("1").append(BaseConstant.SEPERATOR);for (int i = 0; i < 3; i++) {//保存文件名称String fileName = "测试"+i+".mp4";SiteInfoBean bean = new SiteInfoBean("http://vjs.zencdn.net/v/oceans.mp4",filePath.toString(),fileName  , 5);//启动线程,并回调处理信息SiteFileFetch fileFetch = new SiteFileFetch(bean, result -> System.out.println("downsucc" + result));fileFetch.start();}}catch (Exception e) {e.printStackTrace();}return "succ";}

  • 主要下载类
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.function.Consumer;/*** @author user* @version $$ Revision: 1.0 $$, $$ Date: 2023-08-01 15:45 $$*/
public class SiteFileFetch extends Thread {SiteInfoBean siteInfoBean = null; // 文件信息 Beanlong[] nStartPos; // 开始位置long[] nEndPos; // 结束位置FileSplitterFetch[] fileSplitterFetch; // 子线程对象long nFileLength; // 文件长度boolean bFirst = true; // 是否第一次取文件boolean bStop = false; // 停止标志File tmpFile; // 文件下载的临时信息DataOutputStream output; // 输出到文件的输出流Consumer consumer;public SiteFileFetch(SiteInfoBean bean, Consumer consumer) throws IOException {siteInfoBean = bean;this.consumer = consumer;tmpFile = new File(bean.getSFilePath() + File.separator + bean.getSFileName() + ".info");if (tmpFile.exists()) {bFirst = false;read_nPos();}else {nStartPos = new long[bean.getNSplitter()];nEndPos = new long[bean.getNSplitter()];}}@Overridepublic void run() {// 获得文件长度// 分割文件// 实例 FileSplitterFetch// 启动 FileSplitterFetch 线程// 等待子线程返回try {if (bFirst) {nFileLength = getFileSize();if (nFileLength == -1) {System.err.println("File Length is not known!");}else if (nFileLength == -2) {System.err.println("File is not access!");}else {for (int i = 0; i < nStartPos.length; i++) {nStartPos[i] = (long) (i * (nFileLength / nStartPos.length));}for (int i = 0; i < nEndPos.length - 1; i++) {nEndPos[i] = nStartPos[i + 1];}nEndPos[nEndPos.length - 1] = nFileLength;}//判断文件夹是否存在File file = new File(siteInfoBean.getSFilePath());if (!file.exists()) {file.mkdirs();}}// 启动子线程fileSplitterFetch = new FileSplitterFetch[nStartPos.length];for (int i = 0; i < nStartPos.length; i++) {fileSplitterFetch[i] = new FileSplitterFetch(siteInfoBean.getSSiteURL(),siteInfoBean.getSFilePath() + File.separator + siteInfoBean.getSFileName(), nStartPos[i],nEndPos[i], i);Utility.log("Thread " + i + " , nStartPos = " + nStartPos[i] + ", nEndPos = " + nEndPos[i]);fileSplitterFetch[i].start();}//            fileSplitterFetch[nPos.length - 1] = new FileSplitterFetch(siteInfoBean.getSSiteURL(),//                    siteInfoBean.getSFilePath() + File.separator + siteInfoBean.getSFileName(), nPos[nPos.length - 1],//                    nFileLength, nPos.length - 1);//            Utility.log("Thread " + (nPos.length - 1) + ",nStartPos = " + nPos[nPos.length - 1] + ",nEndPos = "//                    + nFileLength);//            fileSplitterFetch[nPos.length - 1].start();// 等待子线程结束//int count = 0;// 是否结束 while 循环boolean breakWhile = false;while (!bStop) {write_nPos();Utility.sleep(500);breakWhile = true;for (int i = 0; i < nStartPos.length; i++) {if (!fileSplitterFetch[i].bDownOver) {breakWhile = false;break;}}if (breakWhile) {break;}//count++;//if(count>4)// siteStop();}System.err.println("文件下载结束!");tmpFile.delete();consumer.accept("下载地址:" + siteInfoBean.getSSiteURL() + ",保存地址:" + siteInfoBean.getSFilePath()+ siteInfoBean.getSFileName());}catch (Exception e) {e.printStackTrace();}finally {}}// 获得文件长度public long getFileSize() {int nFileLength = -1;try {URL url = new URL(siteInfoBean.getSSiteURL());HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();httpConnection.setRequestProperty("User-Agent", "NetFox");int responseCode = httpConnection.getResponseCode();if (responseCode >= 400) {processErrorCode(responseCode);return -2; //-2 represent access is error}String sHeader;for (int i = 1; ; i++) {//DataInputStream in = new DataInputStream(httpConnection.getInputStream ());//Utility.log(in.readLine());sHeader = httpConnection.getHeaderFieldKey(i);if (sHeader != null) {if (sHeader.equals("Content-Length")) {nFileLength = Integer.parseInt(httpConnection.getHeaderField(sHeader));break;}}else {break;}}}catch (Exception e) {e.printStackTrace();}Utility.log(nFileLength);return nFileLength;}// 保存下载信息(文件指针位置)private void write_nPos() {try {output = new DataOutputStream(new FileOutputStream(tmpFile));output.writeInt(nStartPos.length);for (int i = 0; i < nStartPos.length; i++) {// output.writeLong(nPos[i]);output.writeLong(fileSplitterFetch[i].nStartPos);output.writeLong(fileSplitterFetch[i].nEndPos);}output.close();}catch (Exception e) {e.printStackTrace();}}// 读取保存的下载信息(文件指针位置)private void read_nPos() {try {DataInputStream input = new DataInputStream(new FileInputStream(tmpFile));int nCount = input.readInt();nStartPos = new long[nCount];nEndPos = new long[nCount];for (int i = 0; i < nStartPos.length; i++) {nStartPos[i] = input.readLong();nEndPos[i] = input.readLong();}input.close();}catch (Exception e) {e.printStackTrace();}}private void processErrorCode(int nErrorCode) {System.err.println("Error Code : " + nErrorCode);}// 停止文件下载public void siteStop() {bStop = true;for (int i = 0; i < nStartPos.length; i++) {fileSplitterFetch[i].splitterStop();}}
}

  • 内部多线程下载类
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;public class FileSplitterFetch extends Thread {String sURL; //File URLlong nStartPos; //File Snippet Start Positionlong nEndPos; //File Snippet End Positionint nThreadID; //Thread's IDboolean bDownOver = false; //Downing is overboolean bStop = false; //Stop identicalFileAccessI fileAccessI = null; //File Access interfacepublic FileSplitterFetch(String sURL, String sName, long nStart, long nEnd, int id) throws IOException {this.sURL = sURL;this.nStartPos = nStart;this.nEndPos = nEnd;nThreadID = id;fileAccessI = new FileAccessI(sName, nStartPos);}@Overridepublic void run() {while (nStartPos < nEndPos && !bStop) {try {URL url = new URL(sURL);HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();httpConnection.setRequestProperty("User-Agent", "NetFox");String sProperty = "bytes=" + nStartPos + "-";httpConnection.setRequestProperty("RANGE", sProperty);Utility.log(sProperty);InputStream input = httpConnection.getInputStream();//logResponseHead(httpConnection);byte[] b = new byte[1024];int nRead;while ((nRead = input.read(b, 0, 1024)) > 0 && nStartPos < nEndPos && !bStop) {nStartPos += fileAccessI.write(b, 0, nRead);//if(nThreadID == 1)// Utility.log("nStartPos = " + nStartPos + ", nEndPos = " + nEndPos);}Utility.log("Thread " + nThreadID + " is over!");bDownOver = true;//nPos = fileAccessI.write (b,0,nRead);}catch (Exception e) {e.printStackTrace();}finally {try {fileAccessI.close();}catch (IOException e) {e.printStackTrace();}}}}// 打印回应的头信息public void logResponseHead(HttpURLConnection con) {for (int i = 1; ; i++) {String header = con.getHeaderFieldKey(i);if (header != null) {//responseHeaders.put(header,httpConnection.getHeaderField(header));Utility.log(header + " : " + con.getHeaderField(header));}else {break;}}}public void splitterStop() {bStop = true;}
}

  • 实体类
public class SiteInfoBean {private String sSiteURL; //Site's URLprivate String sFilePath; //Saved File's Pathprivate String sFileName; //Saved File's Nameprivate int nSplitter; //Count of Splited Downloading Filepublic SiteInfoBean(){//default value of nSplitter is 5this("","","",5);}public SiteInfoBean(String sURL,String sPath,String sName,int nSpiltter){sSiteURL= sURL;sFilePath = sPath;sFileName = sName;this.nSplitter = nSpiltter;}public String getSSiteURL(){return sSiteURL;}public void setSSiteURL(String value){sSiteURL = value;}public String getSFilePath(){return sFilePath;}public void setSFilePath(String value){sFilePath = value;}public String getSFileName(){return sFileName;}public void setSFileName(String value){sFileName = value;}public int getNSplitter(){return nSplitter;}public void setNSplitter(int nCount){nSplitter = nCount;}
}
import java.io.*;class FileAccessI  implements Serializable{RandomAccessFile oSavedFile;long nPos;public FileAccessI() throws IOException {this("",0);}public FileAccessI(String sName,long nPos) throws IOException {oSavedFile = new RandomAccessFile(sName,"rw");this.nPos = nPos;oSavedFile.seek(nPos);}public synchronized int write(byte[] b,int nStart,int nLen){int n = -1;try{oSavedFile.write(b,nStart,nLen);n = nLen;}catch(IOException e){e.printStackTrace ();}return n;}void close() throws IOException {oSavedFile.close();}
}

  • 日志类
public class Utility {public Utility() {}public static void sleep(int nSecond) {try {Thread.sleep(nSecond);}catch (Exception e) {e.printStackTrace();}}public static void log(String sMsg) {System.err.println(sMsg);}public static void log(int sMsg) {System.err.println(sMsg);}
}

相关文章:

下载远程服务器文件

业务需求:下载某云盘的视频文件存储到本地 测试代码 RequestMapping("testVideo")public String test() {try {SimpleDateFormat DATE_FORMAT new SimpleDateFormat("yyyy/MM/dd/");//组装本地保存地址StringBuilder filePath new StringBuilder(StoreP…...

[SQL挖掘机] - 索引

介绍: 当你在数据库中进行查询时&#xff0c;索引是一种用于提高查询性能的重要工具。索引是对表中的一列或多列进行排序的数据结构&#xff0c;它可以快速定位到满足特定条件的记录&#xff0c;从而减少了查询所需的时间和资源。 在数据库中使用索引的主要好处包括&#xff…...

C++STL库中的list

文章目录 list的介绍及使用 list的常用接口 list的模拟实现 list与vector的对比 一、list的介绍及使用 1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器&#xff0c;并且该容器可以前后双向迭代。 2. list的底层是双向带头循环链表结构&#xff0c;双向带头循…...

【LeetCode 75】第十七题(1493)删掉一个元素以后全为1的最长子数组

目录 题目&#xff1a; 示例&#xff1a; 分析&#xff1a; 代码运行结果&#xff1a; 题目&#xff1a; 示例&#xff1a; 分析&#xff1a; 给一个数组&#xff0c;求删除一个元素以后能得到的连续的最长的全是1的子数组。 我们可以先单独统计出连续为1的子数组分别长度…...

配置IPv6 over IPv4 GRE隧道示例

组网需求 如图1&#xff0c;两个IPv6网络分别通过SwitchA和SwitchC与IPv4公网中的SwitchB连接&#xff0c;客户希望两个IPv6网络中的PC1和PC2实现互通。 其中PC1和PC2上分别指定SwitchA和SwitchC为自己的缺省网关。 图1 配置IPv6 over IPv4 GRE隧道组网图 配置思路 要实现I…...

Google Earth Engine谷歌地球引擎提取多波段长期反射率数据后绘制折线图并导出为Excel

本文介绍在谷歌地球引擎GEE中&#xff0c;提取多年遥感影像多个不同波段的反射率数据&#xff0c;在GEE内绘制各波段的长时间序列走势曲线图&#xff0c;并将各波段的反射率数据与其对应的成像日期一起导出为.csv文件的方法。 本文是谷歌地球引擎&#xff08;Google Earth Engi…...

第三大的数

414、第三大的数 class Solution {public int thirdMax(int[] nums) {Arrays.sort(nums);int tempnums[0];int ansnums[0];int count 0;// if(nums.length<3){// return nums[nums.length-1];// }// else {for(int inums.length-1;i>0;i--){if (nums[i]>nums[i…...

正则表达式中的方括号[]有什么用?

在正则表达式中&#xff0c;方括号 [] 是用于定义字符集合的元字符。它在正则表达式中有以下作用&#xff1a; 匹配字符集合中的任意一个字符&#xff1a;方括号中列出的字符&#xff0c;表示在这个位置可以匹配这些字符中的任意一个。例如&#xff0c;[abc] 将匹配任意一个字符…...

SQL编写规范

文章目录 1.命名规范&#xff1a;2.库表设计&#xff1a;3.查询数据&#xff1a;4.修改数据&#xff1a;5.索引创建&#xff1a; 1.命名规范&#xff1a; 1.库名、表名、字段名&#xff0c;必须使用小写字母或数字&#xff0c;不得超过30个字符。 2.库名、表名、字段名&#…...

Azure pipeline自动化打包发布

pipeline自动化&#xff0c;提交代码后&#xff0c;就自动打包&#xff0c;打包成功后自动发布 第一步 pipeline提交代码后&#xff0c;自动打包。 1 在Repos,分支里选择要触发的分支&#xff0c;这里选择cn_china,对该分支设置分支策略 2 在生产验证中增加新的策略 3 在分支安…...

【算法提高:动态规划】1.4 状态机模型 TODO

文章目录 例题列表1049. 大盗阿福&#xff08;其实就是打家劫舍&#xff09;1057. 股票买卖 IV&#xff08;k笔交易&#xff09;1058. 股票买卖 V&#xff08;冷冻期&#xff09;1052. 设计密码⭐⭐⭐&#x1f6b9;&#x1f6b9;&#x1f6b9;&#xff08;TODO&#xff09;1053…...

ip link add 命令

ip link add veth0 type veth peer name veth1 这条命令主要用于在 Linux 操作系统中创建一个新的 veth(虚拟以太网) 对&#xff0c;这是一种虚拟网络设备&#xff0c;用于在 Linux 命名空间&#xff08;namespaces&#xff09;之间创建网络连接。此命令将创建两个设备&#xf…...

unity事件处理

方法调用 //发送事件 【发送事件码&#xff0c;发送消息内容】 EventCenterUtil.Broadcast(EventCenterUtil.EventType.Joystick, ui);//监听无参事件 EventCenterUtil.AddListener(EventCenterUtil.EventType.Joystick, show); public void show(){}//发送事件 有参事件 Eve…...

《ChatGPT原理最佳解释,从根上理解ChatGPT》

【热点】 2022年11月30日&#xff0c;OpenAI发布ChatGPT&#xff08;全名&#xff1a;Chat Generative Pre-trained Transformer&#xff09;&#xff0c; 即聊天机器人程序 &#xff0c;开启AIGC的研究热潮。 ChatGPT是人工智能技术驱动的自然语言处理工具&#xff0c;它能够…...

大数据Flink(五十):流式计算简介

文章目录 流式计算简介 一、数据的时效性 二、流式计算和批量计算...

13-4_Qt 5.9 C++开发指南_基于QWaitCondition 的线程同步_Wait

在多线程的程序中&#xff0c;多个线程之间的同步实际上就是它们之间的协调问题。例如上一小节讲到的3个线程的例子中&#xff0c;假设 threadDAQ 写满一个缓冲区之后&#xff0c;threadShow 和 threadSaveFile 才能对缓冲区进行读操作。前面采用的互斥量和基于 OReadWriteLock…...

STM32(HAL)多串口进行重定向(printf函数发送数据)

目录 1、简介 2.1 基础配置 2.1.1 SYS配置 2.1.2 RCC配置 2.2 串口外设配置 2.3 项目生成 3、KEIL端程序整合 4、效果测试 1、简介 在HAL库中&#xff0c;常用的printf函数是无法使用的。本文通过重映射实现在HAL库多个串口可进行类似printf函数的操作。 2.1 基础配置 2.…...

29_互联网(The Internet)(IP数据包;UDP;TCP;DNS;OSI)

上篇介绍了计算机网络的基础知识&#xff0c;也提到互联网&#xff08;The Internet&#xff09;&#xff0c;本篇将会详细介绍互联网&#xff08;The Internet&#xff09;。 文章目录 1. 互联网&#xff08;The Internet&#xff09;组成及数据包传输过程2. IP 数据包的不足3…...

xShell常用命令

xShell常用命令 一、文件夹目录1、cd-更改目录2、mkdir-建立目录3、rm-删除目录4、pwd-查看当前路径5、rmdir-删除空目录 二、文件操作1、cat-显示文件内容2、diff-比较文件内容3、查看文件的名字和后缀4、ls-列出文件5、cp-复制文件6、mv-移动和重命名文件找不同&#xff1a;选…...

React性能优化之Memo、useMemo

文章目录 React.memo两种方式参数应用场景 拓展useMemouseMemo(calculateValue, dependencies) 参考资料 React.memo React 的渲染机制&#xff0c;组件内部的 state 或者 props 一旦发生修改&#xff0c;整个组件树都会被重新渲染一次&#xff0c;即时子组件的参数没有被修改&…...

云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?

大家好&#xff0c;欢迎来到《云原生核心技术》系列的第七篇&#xff01; 在上一篇&#xff0c;我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在&#xff0c;我们就像一个拥有了一块崭新数字土地的农场主&#xff0c;是时…...

关于iview组件中使用 table , 绑定序号分页后序号从1开始的解决方案

问题描述&#xff1a;iview使用table 中type: "index",分页之后 &#xff0c;索引还是从1开始&#xff0c;试过绑定后台返回数据的id, 这种方法可行&#xff0c;就是后台返回数据的每个页面id都不完全是按照从1开始的升序&#xff0c;因此百度了下&#xff0c;找到了…...

Qwen3-Embedding-0.6B深度解析:多语言语义检索的轻量级利器

第一章 引言&#xff1a;语义表示的新时代挑战与Qwen3的破局之路 1.1 文本嵌入的核心价值与技术演进 在人工智能领域&#xff0c;文本嵌入技术如同连接自然语言与机器理解的“神经突触”——它将人类语言转化为计算机可计算的语义向量&#xff0c;支撑着搜索引擎、推荐系统、…...

基于数字孪生的水厂可视化平台建设:架构与实践

分享大纲&#xff1a; 1、数字孪生水厂可视化平台建设背景 2、数字孪生水厂可视化平台建设架构 3、数字孪生水厂可视化平台建设成效 近几年&#xff0c;数字孪生水厂的建设开展的如火如荼。作为提升水厂管理效率、优化资源的调度手段&#xff0c;基于数字孪生的水厂可视化平台的…...

多模态大语言模型arxiv论文略读(108)

CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文标题&#xff1a;CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文作者&#xff1a;Sayna Ebrahimi, Sercan O. Arik, Tejas Nama, Tomas Pfister ➡️ 研究机构: Google Cloud AI Re…...

让回归模型不再被异常值“带跑偏“,MSE和Cauchy损失函数在噪声数据环境下的实战对比

在机器学习的回归分析中&#xff0c;损失函数的选择对模型性能具有决定性影响。均方误差&#xff08;MSE&#xff09;作为经典的损失函数&#xff0c;在处理干净数据时表现优异&#xff0c;但在面对包含异常值的噪声数据时&#xff0c;其对大误差的二次惩罚机制往往导致模型参数…...

浪潮交换机配置track检测实现高速公路收费网络主备切换NQA

浪潮交换机track配置 项目背景高速网络拓扑网络情况分析通信线路收费网络路由 收费汇聚交换机相应配置收费汇聚track配置 项目背景 在实施省内一条高速公路时遇到的需求&#xff0c;本次涉及的主要是收费汇聚交换机的配置&#xff0c;浪潮网络设备在高速项目很少&#xff0c;通…...

SQL慢可能是触发了ring buffer

简介 最近在进行 postgresql 性能排查的时候,发现 PG 在某一个时间并行执行的 SQL 变得特别慢。最后通过监控监观察到并行发起得时间 buffers_alloc 就急速上升,且低水位伴随在整个慢 SQL,一直是 buferIO 的等待事件,此时也没有其他会话的争抢。SQL 虽然不是高效 SQL ,但…...

Java数值运算常见陷阱与规避方法

整数除法中的舍入问题 问题现象 当开发者预期进行浮点除法却误用整数除法时,会出现小数部分被截断的情况。典型错误模式如下: void process(int value) {double half = value / 2; // 整数除法导致截断// 使用half变量 }此时...

MySQL 8.0 事务全面讲解

以下是一个结合两次回答的 MySQL 8.0 事务全面讲解&#xff0c;涵盖了事务的核心概念、操作示例、失败回滚、隔离级别、事务性 DDL 和 XA 事务等内容&#xff0c;并修正了查看隔离级别的命令。 MySQL 8.0 事务全面讲解 一、事务的核心概念&#xff08;ACID&#xff09; 事务是…...