@Override
public RespEntity uploadFileByMappedByteBuffer(MultipartFile file, int chunkNumber, int currentChunkSize,
int totalChunks, int chunkSize, CarCenter carCenter, String type, String carNum, Integer uploads,
String uploadFilePathSub, String serverUrl, String uploadsType) throws IOException {
// TODO Auto-generated method stub
RespEntity respEntity = new RespEntity();
String url = null;
String fileName = file.getOriginalFilename();
String tempDirPath = "D:/home/upload/carImg";
// String tempDirPath = uploadFilePathSub + "carImg";
//总页数大于1, 证明是已分片
if(totalChunks > 1) {
File newFile = new File(tempDirPath + fileName);
if (newFile.exists())
newFile.delete();
String tempFileName = fileName + "_tmp";
File tmpDir = new File(tempDirPath);
File tmpFile = new File(tempDirPath, tempFileName);
if (!tmpDir.exists())
tmpDir.mkdirs();
RandomAccessFile accessTmpFile = new RandomAccessFile(tmpFile, "rw");
long offset = chunkSize * (chunkNumber - 1);
accessTmpFile.seek(offset);
accessTmpFile.write(file.getBytes());
accessTmpFile.close();
boolean isOk = checkAndSetUploadProgress(file, tempDirPath, chunkNumber, currentChunkSize, totalChunks);
System.out.println("上传=" + isOk);
if (isOk) {
boolean flag = renameFile(tmpFile, fileName);
System.out.println("upload complete !!" + flag + " name=" + fileName);
}
if(chunkNumber == totalChunks) {
respEntity = readCarInfo(new File(tempDirPath, fileName), carCenter, type, carNum, uploads, tempDirPath, serverUrl, uploadsType);
//删除临时文件
tmpDir.delete();
tmpFile.delete();
}
}else {
//获取UUID
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
String newFileName = "";
if(fileName.indexOf(".") != -1) {
newFileName = fileName.substring(0, fileName.lastIndexOf("."));
String extendsFileName = fileName.substring(fileName.lastIndexOf("."), fileName.length());
newFileName = newFileName + uuid + extendsFileName;
}else {
newFileName += "微信图片_" + fileName + uuid + ".jpg";
}
//新增文件file
File newFile = new File(tempDirPath, newFileName);
//生成图片文件
FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
respEntity = readCarInfo(newFile, carCenter, type, carNum, uploads, tempDirPath, serverUrl, uploadsType);
}
return respEntity;
}
private boolean checkAndSetUploadProgress(MultipartFile file, String tempDirPath, int chunkNumber, int currentChunkSize, int totalChunks) {
try {
String fileName = file.getOriginalFilename();
File confFile = new File(tempDirPath, fileName + ".conf");
RandomAccessFile accessConfFile = new RandomAccessFile(confFile, "rw");
System.out.println("set part " + chunkNumber + " complete");
accessConfFile.setLength(totalChunks);
System.out.println("总分片数="+ totalChunks);
accessConfFile.seek((chunkNumber - 1));
System.out.println("ChunkNumber()=" + chunkNumber);
accessConfFile.write(127);
byte[] completeList = FileUtils.readFileToByteArray(confFile);
System.out.println("数组长="+ completeList.length)
大文件分片上传java后台合并文件并进行ocr识别
最新推荐文章于 2024-04-22 20:58:09 发布