Search in sources :

Example 11 with BizException

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);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) BizException(com.github.lybgeek.common.exception.BizException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 12 with BizException

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);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BizException(com.github.lybgeek.common.exception.BizException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 13 with BizException

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);
}
Also used : FileUploadDTO(com.github.lybgeek.upload.dto.FileUploadDTO) BizException(com.github.lybgeek.common.exception.BizException) StopWatch(org.springframework.util.StopWatch) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 14 with BizException

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);
}
Also used : HashMap(java.util.HashMap) BizException(com.github.lybgeek.common.exception.BizException) FileConvertResultDTO(com.github.lybgeek.file.dto.FileConvertResultDTO) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

BizException (com.github.lybgeek.common.exception.BizException)14 Date (java.util.Date)4 Transactional (org.springframework.transaction.annotation.Transactional)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 ExcelImportResult (cn.afterturn.easypoi.excel.entity.result.ExcelImportResult)2 Result (com.github.lybgeek.common.model.Result)2 FileUploadDTO (com.github.lybgeek.upload.dto.FileUploadDTO)2 IOException (java.io.IOException)2 RandomAccessFile (java.io.RandomAccessFile)2 WriteSheet (com.alibaba.excel.write.metadata.WriteSheet)1 FillConfig (com.alibaba.excel.write.metadata.fill.FillConfig)1 BookDTO (com.github.lybgeek.dynamic.dto.BookDTO)1 Book (com.github.lybgeek.dynamic.model.Book)1 FileConvertResultDTO (com.github.lybgeek.file.dto.FileConvertResultDTO)1 CustomerserviceWagesDTO (com.github.lybgeek.modules.customerservice.dto.CustomerserviceWagesDTO)1 CustomerserviceWagesVO (com.github.lybgeek.modules.customerservice.vo.CustomerserviceWagesVO)1 ImageDTO (com.github.lybgeek.modules.image.dto.ImageDTO)1