Java Code Examples for cn.hutool.core.io.IoUtil#readBytes()
The following examples show how to use
cn.hutool.core.io.IoUtil#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: HdfsUtil.java From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
/** * 上传文件 * <p> * 如果 {@link File}存在,读取文件内容,并上传到 targetPath * * @param file 原文件 * @param targetPath 文件路径 * @throws IOException */ public void uploadFile(@NotNull File file, @NotBlank String targetPath) throws Exception { if (file == null || !file.exists()) { throw new IOException("file not exists"); } // 从文件中读取二进制数据 byte[] bytes = IoUtil.readBytes(new FileInputStream(file)); FileSystem fileSystem = null; FSDataOutputStream outputStream = null; try { fileSystem = this.hdfsPool.borrowObject(); outputStream = fileSystem.create(new Path(targetPath)); outputStream.write(bytes); outputStream.flush(); } finally { IoUtil.close(outputStream); if (fileSystem != null) { this.hdfsPool.returnObject(fileSystem); } } }
Example 2
Source File: HdfsUtil.java From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
/** * 上传文件 * * @param inputStream 输入流 * @param targetPath 文件路径 * @throws IOException */ public void uploadFile(@NotNull InputStream inputStream, @NotBlank String targetPath) throws Exception { // 从输入流中读取二进制数据 byte[] bytes = IoUtil.readBytes(inputStream); FileSystem fileSystem = null; FSDataOutputStream outputStream = null; try { fileSystem = this.hdfsPool.borrowObject(); outputStream = fileSystem.create(new Path(targetPath)); outputStream.write(bytes); outputStream.flush(); } finally { IoUtil.close(outputStream); if (fileSystem != null) { this.hdfsPool.returnObject(fileSystem); } } }
Example 3
Source File: RestFileInfoService.java From Guns with GNU Lesser General Public License v3.0 | 6 votes |
/** * 获取文件流 * * @author fengshuonan * @Date 2019-05-04 17:04 */ public byte[] getFileBytes(String fileId) { if (ToolUtil.isEmpty(fileId)) { throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND); } RestFileInfo fileInfo = this.getById(fileId); if (fileInfo == null) { throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND); } else { try { String filePath = fileInfo.getFilePath(); return IoUtil.readBytes(new FileInputStream(filePath)); } catch (FileNotFoundException e) { log.error("文件未找到,id为:" + fileId, e); throw new ServiceException(BizExceptionEnum.FILE_NOT_FOUND); } } }
Example 4
Source File: AuthConfig.java From v-mock with MIT License | 5 votes |
/** * 返回配置文件流 避免ehcache配置文件一直被占用,无法完全销毁项目重新部署 */ protected InputStream getCacheManagerConfigFileInputStream() { String configFile = "classpath:ehcache/ehcache-shiro.xml"; try { @Cleanup InputStream inputStream = ResourceUtils.getInputStreamForPath(configFile); byte[] readBytes = IoUtil.readBytes(inputStream); InputStream in = new ByteArrayInputStream(readBytes); return in; } catch (IOException e) { throw new ConfigurationException( "Unable to obtain input stream for cacheManagerConfigFile [" + configFile + "]", e); } }
Example 5
Source File: FileInfoService.java From Guns with GNU Lesser General Public License v3.0 | 5 votes |
/** * 预览当前用户头像 * * @author fengshuonan * @Date 2019-05-04 17:04 */ public byte[] previewAvatar() { LoginUser currentUser = LoginContextHolder.getContext().getUser(); if (currentUser == null) { throw new ServiceException(CoreExceptionEnum.NO_CURRENT_USER); } //获取当前用户的头像id User user = userService.getById(currentUser.getId()); String avatar = user.getAvatar(); //如果头像id为空就返回默认的 if (ToolUtil.isEmpty(avatar)) { return Base64.decode(DefaultAvatar.BASE_64_AVATAR); } else { //文件id不为空就查询文件记录 FileInfo fileInfo = this.getById(avatar); if (fileInfo == null) { return Base64.decode(DefaultAvatar.BASE_64_AVATAR); } else { try { String filePath = fileInfo.getFilePath(); return IoUtil.readBytes(new FileInputStream(filePath)); } catch (FileNotFoundException e) { log.error("头像未找到!", e); return Base64.decode(DefaultAvatar.BASE_64_AVATAR); } } } }
Example 6
Source File: Ip2Region.java From magic-starter with GNU Lesser General Public License v3.0 | 4 votes |
/** * 根据ip获取城市信息 */ public static String getAddress(String ip) { if (StrUtil.equals(ip, IpUtil.LOCAL_INNER_LOOP_IP)) { ip = IpUtil.LOCAL_IP; } String address = ""; boolean ipAddress = Util.isIpAddress(ip); if (!ipAddress) { return address; } try { DbConfig config = new DbConfig(); ToolProperties toolProperties = SpringUtil.getBean(ToolProperties.class); InputStream streamSafe = ResourceUtil.getStreamSafe(toolProperties.getIpRegion().getDbFile()); if (streamSafe == null) { log.error("【获取地理位置】未找到 IP 数据库文件,请前往 https://github.com/xkcoding/magic-starter/tree/master/magic-core-tool/src/main/resources/ip/ip2region.db 下载!"); return address; } DbSearcher searcher = new DbSearcher(config, IoUtil.readBytes(streamSafe)); DataBlock dataBlock = searcher.memorySearch(ip); // dataBlock格式:城市Id|国家|区域|省份|城市|ISP // region格式:国家|区域|省份|城市|ISP // region例如:中国|0|浙江省|杭州市|电信 String region = dataBlock.getRegion(); // 按 | 切分 List<String> regionList = Splitter.on("|").splitToList(region); // 过滤为 0 的数据 List<String> regionListFilter = regionList.stream().filter(s -> !StrUtil.equals(StrUtil.ZERO, s)).distinct().collect(Collectors.toList()); // 再用 | 拼接回来 address = Joiner.on("|").join(regionListFilter); } catch (IOException | DbMakerConfigException e) { log.error("【获取地理位置】发生异常: ", e); } return address; }
Example 7
Source File: ServletUtils.java From yue-library with Apache License 2.0 | 3 votes |
/** * 获取请求体byte[]<br> * 调用该方法后,getParam方法将失效 * * @param request {@link ServletRequest} * @return 获得请求体byte[] * @since 4.0.2 */ public static byte[] getBodyBytes(ServletRequest request) { try { return IoUtil.readBytes(request.getInputStream()); } catch (IOException e) { throw new IORuntimeException(e); } }