Java Code Examples for cn.hutool.core.io.FileUtil#readBytes()

The following examples show how to use cn.hutool.core.io.FileUtil#readBytes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UploadFile.java    From yue-library with Apache License 2.0 5 votes vote down vote up
/**
 * @return 获得文件字节流
 * @throws IOException IO异常
 */
public byte[] getFileContent() throws IOException {
	assertValid();
	
	if (data != null) {
		return data;
	}
	if (tempFile != null) {
		return FileUtil.readBytes(tempFile);
	}
	return null;
}
 
Example 2
Source File: NetlifyStaticDeployHandler.java    From halo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void deploy() {
    String domain = optionService.getByPropertyOfNonNull(NetlifyStaticDeployProperties.NETLIFY_DOMAIN).toString();
    String siteId = optionService.getByPropertyOfNonNull(NetlifyStaticDeployProperties.NETLIFY_SITE_ID).toString();
    String token = optionService.getByPropertyOfNonNull(NetlifyStaticDeployProperties.NETLIFY_TOKEN).toString();

    HttpHeaders headers = new HttpHeaders();

    headers.set("Content-Type", "application/zip");
    headers.set(HttpHeaders.AUTHORIZATION, "Bearer " + token);

    Path path = staticPageService.zipStaticPagesDirectory();

    byte[] bytes = FileUtil.readBytes(path.toFile());

    HttpEntity<byte[]> httpEntity = new HttpEntity<>(bytes, headers);

    ResponseEntity<Object> responseEntity = httpsRestTemplate.postForEntity(String.format(DEPLOY_API, siteId), httpEntity, Object.class);
}
 
Example 3
Source File: UeditorUtil.java    From Guns with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 读取文件
 *
 * @author fengshuonan
 * @Date 2019-08-27 13:02
 */
public static void readFile(String fileName, HttpServletResponse response, FileType fileType, String orginalName) {

    if (ToolUtil.isEmpty(fileName)) {
        throw new ServiceException(UE_FILE_NULL_ERROR);
    }

    //获取文件路径
    String path = ConstantsContext.getFileUploadPath() + fileName;
    File file = new File(path);

    //文件不存在或者不可读
    if (!file.exists() || !file.canRead()) {
        throw new ServiceException(UE_FILE_NULL_ERROR);
    }

    //读取文件
    byte[] bytes = null;

    //设置响应的类型
    if (fileType.equals(FileType.IMG)) {

        response.setContentType("image/png");
        bytes = FileUtil.readBytes(file);

    } else if (fileType.equals(FileType.FILE)) {

        response.setContentType("multipart/form-data;charset=utf-8");

        //判断文件是否已经被重命名
        String newFilePath = ConstantsContext.getFileUploadPath() + orginalName;
        File newFile = new File(newFilePath);
        if (!newFile.exists()) {
            newFile = UeditorUtil.reName(file, orginalName);
        }
        bytes = FileUtil.readBytes(newFile);

    } else {

        response.setContentType("video/x-sgi-movie");
        bytes = FileUtil.readBytes(file);

    }

    try {
        OutputStream stream = response.getOutputStream();
        stream.write(bytes);
    } catch (IOException e) {
        log.error("读取文件错误!", e);
        throw new ServiceException(UE_FILE_READ_ERROR);
    }
}