|
|
@@ -0,0 +1,192 @@
|
|
|
+package com.jld.vod.utils;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @program: mqttService
|
|
|
+ * @description: 文件分片发送
|
|
|
+ * @author: MoJiaJian
|
|
|
+ * @create: 2021-09-17 14:49
|
|
|
+ **/
|
|
|
+public class FileUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件分块工具
|
|
|
+ *
|
|
|
+ * @param offset 起始偏移位置
|
|
|
+ * @param file 文件
|
|
|
+ * @param blockSize 分块大小
|
|
|
+ * @return 分块数据
|
|
|
+ */
|
|
|
+
|
|
|
+ public static byte[] getBlock(long offset, File file, int blockSize) {
|
|
|
+
|
|
|
+ byte[] result = new byte[blockSize];
|
|
|
+
|
|
|
+ RandomAccessFile accessFile = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ accessFile = new RandomAccessFile(file, "r");
|
|
|
+
|
|
|
+ accessFile.seek(offset);
|
|
|
+
|
|
|
+ int readSize = accessFile.read(result);
|
|
|
+
|
|
|
+ if (readSize == -1) {
|
|
|
+
|
|
|
+ return null;
|
|
|
+
|
|
|
+ } else if (readSize == blockSize) {
|
|
|
+
|
|
|
+ return result;
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ byte[] tmpByte = new byte[readSize];
|
|
|
+
|
|
|
+ System.arraycopy(result, 0, tmpByte, 0, readSize);
|
|
|
+
|
|
|
+ return tmpByte;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+
|
|
|
+ e.printStackTrace();
|
|
|
+
|
|
|
+ } finally {
|
|
|
+
|
|
|
+ if (accessFile != null) {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ accessFile.close();
|
|
|
+
|
|
|
+ } catch (IOException e1) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+//
|
|
|
+// public static String UploadFileByHttpClient(File file, String url, Map<String, Object> params) {
|
|
|
+// StringBuilder result = new StringBuilder();
|
|
|
+// CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+// CloseableHttpResponse response = null;
|
|
|
+// InputStream content = null;
|
|
|
+// BufferedReader in = null;
|
|
|
+// try {
|
|
|
+// HttpPost httpPost = new HttpPost(url);
|
|
|
+// //HttpMultipartMode.RFC6532参数的设定是为避免文件名为中文时乱码
|
|
|
+// MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
|
|
|
+// //httpPost.addHeader("header1", "111");//头部放文件上传的head可自定义
|
|
|
+// builder.addBinaryBody("file", file, ContentType.MULTIPART_FORM_DATA, file.getName());
|
|
|
+// params.forEach((k, v) -> builder.addTextBody(k, v.toString()));//解析参数
|
|
|
+// HttpEntity entity = builder.build();
|
|
|
+// httpPost.setEntity(entity);
|
|
|
+// response = httpClient.execute(httpPost);// 执行提交
|
|
|
+// HttpEntity responseEntity = response.getEntity();//接收调用外部接口返回的内容
|
|
|
+// if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
+// // 返回的内容都在content中
|
|
|
+// content = responseEntity.getContent();
|
|
|
+// // 定义BufferedReader输入流来读取URL的响应
|
|
|
+// in = new BufferedReader(new InputStreamReader(content));
|
|
|
+// String line;
|
|
|
+// while ((line = in.readLine()) != null) {
|
|
|
+// result.append(line);
|
|
|
+// }
|
|
|
+// if (!result.toString().equals("")) {
|
|
|
+// System.out.println("上传文件" + file.getName() + "返回参数==>" + result);
|
|
|
+// } else {
|
|
|
+// System.out.println("上传文件失败:返回result为null");
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// System.out.println("上传文件失败:" + e);
|
|
|
+// } finally {//处理结束后关闭httpclient的链接
|
|
|
+// try {
|
|
|
+// httpClient.close();
|
|
|
+// if (content != null) {
|
|
|
+// content.close();
|
|
|
+// }
|
|
|
+// if (in != null) {
|
|
|
+// in.close();
|
|
|
+// }
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return result.toString();
|
|
|
+// }
|
|
|
+
|
|
|
+// public static void main(String[] args) throws IOException {
|
|
|
+// String url = "http://127.0.0.1:8080/uploadFile";
|
|
|
+//
|
|
|
+// File file = new File("E:\\DY\\asd.mp4");
|
|
|
+// String s1 = file.getName();
|
|
|
+// System.out.println(s1.split("\\.")[0]);
|
|
|
+//
|
|
|
+// //计算文件总分片大小
|
|
|
+// long length = file.length();//当前文件大小
|
|
|
+// int total = 0;//总分片大小
|
|
|
+//
|
|
|
+// int CHUNK_SIZE = 10*1024*1024; //分片大小 最好是long型
|
|
|
+//
|
|
|
+// if (length % CHUNK_SIZE ==0){
|
|
|
+// total = (int) (length / CHUNK_SIZE);
|
|
|
+// }else {
|
|
|
+// total = (int) (length / CHUNK_SIZE) + 1 ;
|
|
|
+// }
|
|
|
+//
|
|
|
+// boolean tmp = true; //读取进度结束符
|
|
|
+// int index = 0;
|
|
|
+// //一次1M进行读取
|
|
|
+// while (tmp) {
|
|
|
+// byte[] block = getBlock(CHUNK_SIZE * index, file, CHUNK_SIZE);
|
|
|
+// if (block != null) {
|
|
|
+// String filePath = "E:\\DY\\" + s1 + "_"+index+".tmp";//缓存目录 外部自定义
|
|
|
+// OutputStream out = new FileOutputStream(filePath);
|
|
|
+// InputStream is = new ByteArrayInputStream(block);
|
|
|
+// byte[] buff = new byte[1024];
|
|
|
+// int len = 0;
|
|
|
+// while ((len = is.read(buff)) != -1) {
|
|
|
+// out.write(buff, 0, len);
|
|
|
+// }
|
|
|
+// is.close();
|
|
|
+// out.close();
|
|
|
+// /*
|
|
|
+// * 模拟发送请求
|
|
|
+// */
|
|
|
+// //设置参数
|
|
|
+// int finalTotal = total;
|
|
|
+// int finalIndex = index;
|
|
|
+// Map<String, Object> map = new HashMap<String, Object>() {{
|
|
|
+// put("chunks", finalTotal);
|
|
|
+// put("chunk", finalIndex);
|
|
|
+// put("chunkSize",CHUNK_SIZE);//固定块大小
|
|
|
+// put("md5","000");
|
|
|
+// put("resId","000");
|
|
|
+// put("mid","000");
|
|
|
+// put("fileName",s1);
|
|
|
+// }};
|
|
|
+// String s = UploadFileByHttpClient(new File(filePath), url, map);
|
|
|
+// System.out.println(s);//输出返回内容
|
|
|
+// index++;
|
|
|
+// //todo 删除缓存文件
|
|
|
+// new File(filePath).delete();
|
|
|
+// } else {
|
|
|
+// tmp = false;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// System.out.println("读取了文件:" + index + "次");
|
|
|
+// }
|
|
|
+}
|