问题描述
出现java.io.IOException:java.io.FileNotFoundException:/home/admin/appName/.default/temp/tomcat.4504264197870423949.7001/work/Tomcat/Localhost/ROOT/upLoad_ff92855a_13c6_49d9_bbdf_1c062fb9bfd9_00000004.tmp(没有那个文件或目录)的错误。 参考博客
错误部分代码:
accessFile.write(bytes);
这里如果使用multipartFile.getBytes();则会出现FileNotFoundException异常。
全部代码
package com.operative.core.d3DicomFile.controller;
import com.aliyun.oss.model.PartETag;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.operative.base.annotation.OperationLogAnnotation;
import com.operative.base.annotation.UnLogin;
import com.operative.base.constants.SysLogConstants;
import com.operative.base.http.ResponseData;
import com.operative.base.http.ResponseStatus;
import com.operative.base.http.Result;
import com.operative.base.utils.OSSUtils;
import com.operative.core.d3DicomFile.entity.D3DicomFile;
import com.operative.core.d3DicomFile.service.D3DicomFileService;
import com.operative.core.sysLog.entity.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
@CrossOrigin
@RestController
@RequestMapping("/d3DicomFile")
public class D3DicomFileController {
@Autowired
private D3DicomFileService d3DicomFileService;
@Value("${path.static}")
private String staticPath;
private static final ConcurrentMap<String, byte[]> TAP_BYTE = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, File> MD5_CACHE = new ConcurrentHashMap<>();
private static final File tempFilePath = new File("");
static AtomicInteger c = new AtomicInteger(1);
static ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(2000);
static ThreadPoolExecutor treadPool = new ThreadPoolExecutor(
6,
6,
0,
TimeUnit.MILLISECONDS,
queue,
r -> new Thread(r,"myThread"+c.getAndIncrement())
);
@PostMapping
@UnLogin
@Async
public void chunkUpload1(String name,
String md5,
Long size,
Integer chunks,
Integer chunk,
String fileName,
@RequestParam("file") MultipartFile multipartFile) throws IOException {
final byte[] bytes = multipartFile.getBytes();
treadPool.submit(()->{
try {
chunkUpload(name, md5, size, chunks, chunk, fileName,bytes, multipartFile);
} catch (IOException e) {
e.printStackTrace();
}
});
}
@UnLogin
public void chunkUpload(String name,
String md5,
Long size,
Integer chunks,
Integer chunk,
String fileName,
byte[] bytes,
@RequestParam("file") MultipartFile multipartFile) throws IOException {
File targetFile = MD5_CACHE.get(md5);
if (targetFile == null) {
targetFile = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + staticPath + System.getProperty("file.separator"), name);
targetFile.getParentFile().mkdirs();
MD5_CACHE.put(md5, targetFile);
}
System.out.println("targetFile"+targetFile);
File newFile = new File(System.getProperty("user.dir") + System.getProperty("file.separator") + staticPath +System.getProperty("file.separator"), name);
System.out.println("newFile"+newFile);
RandomAccessFile accessFile = new RandomAccessFile(newFile, "rw");
boolean finished = (chunk.equals(chunks));
if (finished) {
accessFile.seek(size - multipartFile.getSize());
}else {
accessFile.seek((chunk - 1) * multipartFile.getSize());
}
accessFile.write(bytes);
accessFile.close();
if (finished) {
System.out.println("success.");
MD5_CACHE.remove(md5);
}
}
}
同一个文件上传时间对比
直接上传
分片上传
后端不做多线程处理也行
|