上传大文件完成 速度有点慢
删除完成
This commit is contained in:
parent
7ead5d4f1a
commit
b167e904c8
@ -1,5 +1,8 @@
|
||||
package com.zhu.onemanager.controller.onedrive;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.stream.CollectorUtil;
|
||||
import com.zhu.onemanager.constant.FileConstant;
|
||||
import com.zhu.onemanager.logic.impl.OneDriveItemUrlImpl;
|
||||
import com.zhu.onemanager.pojo.DriveParams;
|
||||
@ -12,6 +15,8 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ggBall
|
||||
@ -84,7 +89,7 @@ public class OnedriveController {
|
||||
* @return com.zhu.onemanager.result.R
|
||||
**/
|
||||
@PostMapping("/upload")
|
||||
public R upload(@RequestParam("file") MultipartFile file,String itemId) throws IOException {
|
||||
public R uploadItem(@RequestParam("file") MultipartFile file,String itemId) throws IOException, InterruptedException {
|
||||
if (file.isEmpty()) {
|
||||
return R.error("上传失败,请选择文件");
|
||||
}
|
||||
@ -102,8 +107,15 @@ public class OnedriveController {
|
||||
} else {
|
||||
return onedriveService.uploadMIniFile(uploadItem);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/delete")
|
||||
public R deleteItem(@RequestParam("itemIds") List<String> itemIds) {
|
||||
if (CollUtil.isEmpty(itemIds)) {
|
||||
return R.error("未选择删除文件");
|
||||
}
|
||||
|
||||
return onedriveService.deleteItem(itemIds);
|
||||
}
|
||||
|
||||
|
||||
|
@ -80,4 +80,29 @@ public class OneDriveItemUrlImpl {
|
||||
return rootUrl + Strings.solve(url,uploadItem.getItemId(),uploadItem.getFileName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @Author ggball
|
||||
* @Description 获取删除url
|
||||
* @Date 2022/1/11
|
||||
* @Param driveId
|
||||
* @Param itemId
|
||||
* @return java.lang.String
|
||||
**/
|
||||
public String getDeleteUrl(String driveId,String itemId) {
|
||||
String path = Strings.solve("/drives/{drive-id}/items/{item-id}", driveId, itemId);
|
||||
return rootUrl + path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Author ggball
|
||||
* @Description 获取删除url
|
||||
* @Date 2022/1/11
|
||||
* @Param itemId
|
||||
* @return java.lang.String
|
||||
**/
|
||||
public String getCurDeleteUrl(String itemId) {
|
||||
String path = Strings.solve("/me/drive/items/{item-id}", itemId);
|
||||
return rootUrl + path;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import com.zhu.onemanager.result.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public interface OnedriveService {
|
||||
|
||||
@ -55,4 +56,13 @@ public interface OnedriveService {
|
||||
* @return com.zhu.onemanager.result.R
|
||||
**/
|
||||
R multiUpload(OneDriveUploadItem uploadItem) throws IOException;
|
||||
|
||||
/**
|
||||
* @Author ggball
|
||||
* @Description 删除itemId集合
|
||||
* @Date 2022/1/11
|
||||
* @Param [itemIds]
|
||||
* @return com.zhu.onemanager.result.R
|
||||
**/
|
||||
R deleteItem(List<String> itemIds);
|
||||
}
|
||||
|
@ -19,11 +19,14 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
/**
|
||||
* @author ggBall
|
||||
@ -109,8 +112,6 @@ public class OnedriveServiceImpl implements OnedriveService {
|
||||
}
|
||||
|
||||
public R multiUpload(OneDriveUploadItem uploadItem) throws IOException {
|
||||
// 初始化线程池
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||
|
||||
MultipartFile file = uploadItem.getFile();
|
||||
byte[] bytes = file.getBytes();
|
||||
@ -125,7 +126,9 @@ public class OnedriveServiceImpl implements OnedriveService {
|
||||
// 文件总长度
|
||||
long totalLength = file.getSize();
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
for (Map.Entry<int[], byte[]> entry : byteMap.entrySet()) {
|
||||
|
||||
// 数据范围 [0] 起始位置 [1] 结束位置
|
||||
int[] range = entry.getKey();
|
||||
// 数据
|
||||
@ -133,14 +136,39 @@ public class OnedriveServiceImpl implements OnedriveService {
|
||||
// 此次传输的文件长度
|
||||
int contentLength = values.length;
|
||||
|
||||
System.out.println("range[0] = " + range[0] + "range[1] =" + range[1]);
|
||||
// 调用上传接口
|
||||
Dict res = GHttpUtil.put(curUploadSession, fileBytes, contentType, contentLength, totalLength, range);
|
||||
}
|
||||
|
||||
return R.ok(null);
|
||||
// 调用上传接口
|
||||
Dict res = GHttpUtil.put(curUploadSession, values, contentType, contentLength, totalLength, range);
|
||||
System.out.println("range[0] = " + range[0] + "range[1] =" + range[1] + " res: " + res);
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("耗时{}秒",(end-start)/1000.0);
|
||||
|
||||
return R.ok("上传成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param itemIds
|
||||
* @return com.zhu.onemanager.result.R
|
||||
* @Author ggball
|
||||
* @Description 删除itemId集合
|
||||
* @Date 2022/1/11
|
||||
* @Param [itemIds]
|
||||
*/
|
||||
@Override
|
||||
public R deleteItem(List<String> itemIds) {
|
||||
List<String> urls = new LinkedList<>();
|
||||
|
||||
for (String itemId : itemIds) {
|
||||
urls.add(oneDriveItemUrl.getCurDeleteUrl(itemId));
|
||||
}
|
||||
|
||||
urls.parallelStream().forEach(url -> {
|
||||
GHttpUtil.delete(url);
|
||||
});
|
||||
|
||||
return R.ok("删除成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -27,7 +27,7 @@ public class FileUtils {
|
||||
/**
|
||||
* Microsoft Graph indicates all chunks MUST be in a multiple of 320Kib (327,680 bytes).
|
||||
*/
|
||||
private static final Integer FRAG_SIZE = 320 * 10 * FileConstant.STANDARD_KB;
|
||||
private static final Integer FRAG_SIZE = 192 * 320 * FileConstant.STANDARD_KB;
|
||||
|
||||
/**
|
||||
* @Author ggball
|
||||
@ -46,6 +46,13 @@ public class FileUtils {
|
||||
FileUtil.writeUtf8Map(map,file, FileConstant.KV_SEPARATOR,false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Author ggball
|
||||
* @Description 读取文件 返回map
|
||||
* @Date 2022/1/11
|
||||
* @Param [path, separator]
|
||||
* @return java.util.Map
|
||||
**/
|
||||
public static Map readMap(String path,String separator) throws IOException {
|
||||
File file = FileUtil.file(PathConstant.getFilePath(path));
|
||||
|
||||
@ -94,7 +101,7 @@ public class FileUtils {
|
||||
// 添加字节
|
||||
newBytes[i % FRAG_SIZE] = values[i];
|
||||
|
||||
// 下标是4mb的倍数
|
||||
// 下标是FRAG_SIZE的倍数
|
||||
if ((i+1) / FRAG_SIZE > 0 && (i+1) % FRAG_SIZE == 0) {
|
||||
// 添加字节数组
|
||||
bytes.put(new int[]{preIndex,i},newBytes.clone());
|
||||
|
@ -53,17 +53,25 @@ public class GHttpUtil extends HttpUtil {
|
||||
public static Dict put(String url,byte[] values,String contentType,int contentLength,long totalLength,int[] range) {
|
||||
HttpRequest request = HttpRequest
|
||||
.put(url)
|
||||
.contentType("application/octet-stream")
|
||||
.contentLength(contentLength)
|
||||
.contentType(contentType)
|
||||
.header("Content-Range","bytes " + range[0] + "-" + range[1] + "/" + totalLength) // "bytes 0-99/100"
|
||||
// .contentLength(contentLength)
|
||||
.body(values);
|
||||
|
||||
// 添加 传输指定字节的范围
|
||||
request.header("Content-Range","bytes " + range[0] + "-" + range[1] + "/" + totalLength); // "bytes 0-99/100"
|
||||
String url1 = request.getUrl();
|
||||
String res = request.execute().body();
|
||||
System.out.println("res = " + res);
|
||||
return Dict.parse(JSONObject.parseObject(res, Map.class));
|
||||
|
||||
}
|
||||
|
||||
/*删除请求*/
|
||||
public static Dict delete(String url) {
|
||||
HttpRequest request = HttpRequest
|
||||
.delete(url);
|
||||
HttpResponse res = request.execute();
|
||||
Dict dict = new Dict();
|
||||
dict.put("status",res.getStatus());
|
||||
return dict;
|
||||
}
|
||||
|
||||
}
|
||||
|
20350
src/main/resources/webapp/package-lock.json
generated
20350
src/main/resources/webapp/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -20,6 +20,7 @@
|
||||
"css-loader": "^0.28.11",
|
||||
"element-ui": "^2.0.0-rc.1",
|
||||
"file-loader": "^1.1.11",
|
||||
"qs": "^6.10.3",
|
||||
"sass-loader": "^7.1.0",
|
||||
"vue": "^2.5.2",
|
||||
"vue-pdf": "^4.3.0",
|
||||
|
@ -2,7 +2,7 @@
|
||||
<div id="table">
|
||||
<el-row :gutter="24">
|
||||
<el-col :span="1">
|
||||
<el-button size="small" type="danger">删除</el-button>
|
||||
<el-button size="small" type="danger" @click="deleteCheckedItems">删除</el-button>
|
||||
</el-col>
|
||||
<el-col :span="1">
|
||||
<el-upload
|
||||
@ -11,8 +11,7 @@
|
||||
:data="uploadExtraData"
|
||||
:show-file-list="false"
|
||||
:on-success="uploadSuccess"
|
||||
:on-error="uploadError"
|
||||
:on-exceed="handleExceed">
|
||||
:on-error="uploadError">
|
||||
<el-button size="small" type="primary">上传</el-button>
|
||||
<!-- <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>-->
|
||||
</el-upload>
|
||||
@ -46,7 +45,14 @@
|
||||
style="width: 100%"
|
||||
v-loading="loading"
|
||||
:data="fileList"
|
||||
ref="multipleTable"
|
||||
@selection-change="handleSelectionChange"
|
||||
>
|
||||
<el-table-column
|
||||
type="selection"
|
||||
width="55">
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column
|
||||
prop="id"
|
||||
label="id"
|
||||
@ -120,7 +126,7 @@
|
||||
|
||||
<script>
|
||||
import axios from 'axios'
|
||||
|
||||
import qs from 'qs'
|
||||
|
||||
var env = process.env
|
||||
const baseUrl = env.BASE_URL === 'undefined' ? 'http://localhost:8081/' : env.BASE_URL
|
||||
@ -145,7 +151,9 @@ export default {
|
||||
// 上传路径
|
||||
uploadUrl: baseUrl+"oneDrive/upload",
|
||||
// 上传额外信息
|
||||
uploadExtraData: {}
|
||||
uploadExtraData: {},
|
||||
// 选中itemId集合
|
||||
checkedItem:{}
|
||||
}
|
||||
|
||||
},
|
||||
@ -226,18 +234,6 @@ export default {
|
||||
},
|
||||
|
||||
// 上传相关
|
||||
handleRemove(file, fileList) {
|
||||
console.log(file, fileList);
|
||||
},
|
||||
handlePreview(file) {
|
||||
console.log(file);
|
||||
},
|
||||
handleExceed(files, fileList) {
|
||||
this.$message.warning(`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
|
||||
},
|
||||
beforeRemove(file, fileList) {
|
||||
return this.$confirm(`确定移除 ${ file.name }?`);
|
||||
},
|
||||
uploadSuccess(response, file, fileList) {
|
||||
this.$message({
|
||||
message: '上传成功',
|
||||
@ -245,6 +241,7 @@ export default {
|
||||
});
|
||||
this.index()
|
||||
},
|
||||
|
||||
uploadError(err, file, fileList) {
|
||||
this.$message({
|
||||
message: '上传失败',
|
||||
@ -257,6 +254,42 @@ export default {
|
||||
this.uploadExtraData = {"itemId":this.curItemId}
|
||||
},
|
||||
|
||||
// 删除
|
||||
deleteCheckedItems() {
|
||||
// 获取项目ids
|
||||
let itemIds = []
|
||||
for (let checkedItem of this.checkedItem) {
|
||||
itemIds.push(checkedItem.id)
|
||||
}
|
||||
return axios
|
||||
.get(baseUrl + 'oneDrive/delete',{
|
||||
params: {
|
||||
itemIds: itemIds
|
||||
},
|
||||
paramsSerializer: params => {
|
||||
return qs.stringify(params, { indices: false })
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
console.log(response.data.data)
|
||||
this.loading = false;
|
||||
this.index()
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(error)
|
||||
this.index()
|
||||
})
|
||||
|
||||
|
||||
},
|
||||
|
||||
/*checkBox*/
|
||||
|
||||
// checkbox变化时触发
|
||||
handleSelectionChange(val) {
|
||||
this.checkedItem = val;
|
||||
},
|
||||
|
||||
// 容量转换
|
||||
bytesToSize (item) {
|
||||
var bytes = item.size
|
||||
|
Loading…
Reference in New Issue
Block a user