use of com.github.lybgeek.common.exception.BizException in project springboot-learning by lyb-geek.
the class FileUtil method copyFile.
public static void copyFile(String oldPath, String newPath) {
try {
File oldFile = new File(oldPath);
File file = new File(newPath);
FileInputStream in = new FileInputStream(oldFile);
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[2097152];
while ((in.read(buffer)) != -1) {
out.write(buffer);
}
} catch (IOException e) {
throw new BizException("复制文件错误", e);
}
}
use of com.github.lybgeek.common.exception.BizException in project springboot-learning by lyb-geek.
the class FileUtil method downloadFile.
public static void downloadFile(String name, String path, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException {
File downloadFile = new File(path);
String fileName = name;
if (StringUtils.isBlank(fileName)) {
fileName = downloadFile.getName();
}
String headerValue = String.format("attachment; filename=\"%s\"", fileName);
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, headerValue);
response.addHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
// 获取文件大小
long downloadSize = downloadFile.length();
long fromPos = 0, toPos = 0;
if (request.getHeader("Range") == null) {
response.addHeader(HttpHeaders.CONTENT_LENGTH, downloadSize + "");
} else {
log.info("range:{}", response.getHeader("Range"));
// 如果为持续下载
response.setStatus(HttpStatus.PARTIAL_CONTENT.value());
String range = request.getHeader("Range");
String bytes = range.replaceAll("bytes=", "");
String[] ary = bytes.split("-");
fromPos = Long.parseLong(ary[0]);
log.info("fronPos:{}", fromPos);
if (ary.length == 2) {
toPos = Long.parseLong(ary[1]);
}
int size;
if (toPos > fromPos) {
size = (int) (toPos - fromPos);
} else {
size = (int) (downloadSize - fromPos);
}
response.addHeader(HttpHeaders.CONTENT_LENGTH, size + "");
downloadSize = size;
}
try (RandomAccessFile in = new RandomAccessFile(downloadFile, "rw");
OutputStream out = response.getOutputStream()) {
if (fromPos > 0) {
in.seek(fromPos);
}
int bufLen = (int) (downloadSize < 2048 ? downloadSize : 2048);
byte[] buffer = new byte[bufLen];
int num;
// 当前写入客户端大小
int count = 0;
while ((num = in.read(buffer)) != -1) {
out.write(buffer, 0, num);
count += num;
if (downloadSize - count < bufLen) {
bufLen = (int) (downloadSize - count);
if (bufLen == 0) {
break;
}
buffer = new byte[bufLen];
}
}
response.flushBuffer();
} catch (IOException e) {
log.error("download error:" + e.getMessage(), e);
throw new BizException("文件下载失败", 406);
}
}
use of com.github.lybgeek.common.exception.BizException in project springboot-learning by lyb-geek.
the class FileController method upload.
@PostMapping(value = "/upload")
@ResponseBody
public Result<FileUploadDTO> upload(FileUploadRequestDTO fileUploadRequestDTO) throws IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileUploadDTO fileUploadDTO = null;
if (isMultipart) {
StopWatch stopWatch = new StopWatch();
stopWatch.start("upload");
if (fileUploadRequestDTO.getChunk() != null && fileUploadRequestDTO.getChunks() > 0) {
fileUploadDTO = fileService.sliceUpload(fileUploadRequestDTO);
} else {
fileUploadDTO = fileService.upload(fileUploadRequestDTO);
}
stopWatch.stop();
log.info("{}", stopWatch.prettyPrint());
return new Result<FileUploadDTO>().setData(fileUploadDTO);
}
throw new BizException("上传失败", 406);
}
use of com.github.lybgeek.common.exception.BizException in project springboot-learning by lyb-geek.
the class FileController method upload.
@PostMapping(value = "/upload")
@ResponseBody
public Result<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
Map<String, Object> params = new HashMap<>();
params.put(FileConstant.FILE_PARAM_KEY, file.getBytes());
String response = HttpClientUtil.INSTANCE.post(FileConstant.PREVIEW_REMOTE_URL, params, file.getOriginalFilename());
FileConvertResultDTO fileConvertResultDTO = JSON.parseObject(response, FileConvertResultDTO.class);
if (ObjectUtils.isNotEmpty(fileConvertResultDTO) && FileConstant.SUCCESS.equals(fileConvertResultDTO.getStatus())) {
return new Result<String>().setData(fileConvertResultDTO.getTargetFileName());
}
throw new BizException("文件上传解析预览失败", 406);
}
Aggregations