Java Code Examples for org.apache.tomcat.util.http.fileupload.IOUtils#closeQuietly()
The following examples show how to use
org.apache.tomcat.util.http.fileupload.IOUtils#closeQuietly() .
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: AbstractSysAttachUploadOperationService.java From mPaaS with Apache License 2.0 | 6 votes |
private SysAttachMediaSizeVO getImageSize(InputStream inputStream) { try { BufferedImage image = ImageIO.read(inputStream); if (image == null) { return null; } SysAttachMediaSizeVO mediaSizeVO = new SysAttachMediaSizeVO(); mediaSizeVO.setWidth(image.getWidth()); mediaSizeVO.setHeight(image.getHeight()); return mediaSizeVO; } catch (IOException e) { return null; } finally { IOUtils.closeQuietly(inputStream); } }
Example 2
Source File: SysAttachTempUploadOperationService.java From mPaaS with Apache License 2.0 | 6 votes |
/** * 上传临时附件,并保存临时附件记录 * @param uploadFunction 需要具体实现的文件上传逻辑 * @return 生成的临时附件ID */ private String recordTempUpload(SysAttachUploadBaseVO uploadBaseVO, InputStream inputStream, Function<InputStream, SysAttachUploadResultVO> uploadFunction) { try { // 执行文件上传 SysAttachUploadResultVO uploadResultVO = uploadFunction.apply(inputStream); if (uploadResultVO == null) { return null; } // 保存attachFileTemp SysAttachFile attachFile = this.saveAttachFile(uploadBaseVO, uploadResultVO); // 保存attachTemp SysAttachMain attachTemp = this.saveAttachTemp(uploadBaseVO, attachFile); return attachTemp.getFdId(); } catch (Exception e) { throw new KmssServiceException("sys-attach:sys.attach.msg.error.SysAttachWriteFailed", e); } finally { IOUtils.closeQuietly(inputStream); } }
Example 3
Source File: SensitiveWordUtils.java From xechat with MIT License | 6 votes |
/** * 读取敏感词 */ private static void readSensitiveWords() { keyWords = new HashSet<>(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(SensitiveWordUtils.class .getResourceAsStream("/sensitive-word.txt"), "utf-8")); String line; while ((line = reader.readLine()) != null) { keyWords.add(line.trim()); } } catch (Exception e) { log.error("读取敏感词库出现异常! error -> {}", e); } finally { IOUtils.closeQuietly(reader); } }
Example 4
Source File: TomcatConfig.java From onetwo with Apache License 2.0 | 6 votes |
public static Properties load(String srcpath){ Properties config = new Properties(); try { config = loadProperties(srcpath); } catch (Exception e) { InputStream in = null; try { in = TomcatConfig.class.getResourceAsStream(srcpath); if(in==null) throw new RuntimeException("can load resource as stream with : " +srcpath ); config.load(in); } catch (Exception e1) { throw new RuntimeException("load config error: " + srcpath, e); } finally{ IOUtils.closeQuietly(in); } } return config; }
Example 5
Source File: Streams.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * Copies the contents of the given {@link InputStream} * to the given {@link OutputStream}. * * @param inputStream The input stream, which is being read. * It is guaranteed, that {@link InputStream#close()} is called * on the stream. * @param outputStream The output stream, to which data should * be written. May be null, in which case the input streams * contents are simply discarded. * @param closeOutputStream True guarantees, that {@link OutputStream#close()} * is called on the stream. False indicates, that only * {@link OutputStream#flush()} should be called finally. * @param buffer Temporary buffer, which is to be used for * copying data. * @return Number of bytes, which have been copied. * @throws IOException An I/O error occurred. */ public static long copy(InputStream inputStream, OutputStream outputStream, boolean closeOutputStream, byte[] buffer) throws IOException { OutputStream out = outputStream; InputStream in = inputStream; try { long total = 0; for (;;) { int res = in.read(buffer); if (res == -1) { break; } if (res > 0) { total += res; if (out != null) { out.write(buffer, 0, res); } } } if (out != null) { if (closeOutputStream) { out.close(); } else { out.flush(); } out = null; } in.close(); in = null; return total; } finally { IOUtils.closeQuietly(in); if (closeOutputStream) { IOUtils.closeQuietly(out); } } }
Example 6
Source File: SysAttachServerProxy.java From mPaaS with Apache License 2.0 | 5 votes |
@Override public void writeFile(InputStream inputSream, String filePath, Map<String, String> header) { // 当服务器上面不存在附件文件时候,分别构建输入,输出流 File file = new File(filePath); File pfile = file.getParentFile(); if (!pfile.exists()) { pfile.mkdirs(); } if (file.exists()) { file.delete(); } FileOutputStream fileOutputStream = null; InputStream encryptionInputStream = null; try { file.createNewFile(); fileOutputStream = new FileOutputStream(file); // 进行附件文件写操作 encryptionInputStream = getEncryService().initEncryInputStream(inputSream); IOUtils.copy(encryptionInputStream, fileOutputStream); } catch (IOException e) { e.printStackTrace(); } finally { // 附件写操作完成后,依次关闭输出流和输入流,释放输出,输入流所在内存空间 IOUtils.closeQuietly(fileOutputStream); IOUtils.closeQuietly(encryptionInputStream); IOUtils.closeQuietly(inputSream); } }
Example 7
Source File: SysAttachServerProxy.java From mPaaS with Apache License 2.0 | 5 votes |
@Override public void appendFile(InputStream inputSream, String filePath, long position, Map<String, String> header) { if (position == 0) { this.writeFile(inputSream, filePath, header); return; } // 当服务器上面不存在附件文件时候,抛异常 File file = new File(filePath); if (!file.exists()) { throw new KmssRuntimeException("sys-attach:sys.attach.msg.error.SysAttachFileNotFound"); } RandomAccessFile randomFile = null; InputStream encryptionInputStream = null; try { randomFile = new RandomAccessFile(file, "rw"); randomFile.seek(position); // 进行附件文件写操作 encryptionInputStream = getEncryService().initEncryInputStream(inputSream); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(encryptionInputStream, baos); randomFile.write(baos.toByteArray()); } catch (IOException e) { throw new KmssRuntimeException("sys-attach:sys.attach.msg.error.SysAttachFileAppendFailed", e); } finally { // 附件写操作完成后,依次关闭输出流和输入流,释放输出,输入流所在内存空间 IOUtils.closeQuietly(randomFile); IOUtils.closeQuietly(encryptionInputStream); IOUtils.closeQuietly(inputSream); } }
Example 8
Source File: HomePageUI.java From dubbox with Apache License 2.0 | 5 votes |
private FileResource getFileResource(String path,String type){ InputStream inputStream = null; try { ClassPathResource resource = new ClassPathResource(path); inputStream = resource.getInputStream(); File tempFile = File.createTempFile("ds_" + System.currentTimeMillis(), type); FileUtils.copyInputStreamToFile(inputStream, tempFile); return new FileResource(tempFile); } catch (Exception e) { e.printStackTrace(); return null; } finally { IOUtils.closeQuietly(inputStream); } }
Example 9
Source File: Streams.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * Copies the contents of the given {@link InputStream} * to the given {@link OutputStream}. * * @param inputStream The input stream, which is being read. * It is guaranteed, that {@link InputStream#close()} is called * on the stream. * @param outputStream The output stream, to which data should * be written. May be null, in which case the input streams * contents are simply discarded. * @param closeOutputStream True guarantees, that {@link OutputStream#close()} * is called on the stream. False indicates, that only * {@link OutputStream#flush()} should be called finally. * @param buffer Temporary buffer, which is to be used for * copying data. * @return Number of bytes, which have been copied. * @throws IOException An I/O error occurred. */ public static long copy(InputStream inputStream, OutputStream outputStream, boolean closeOutputStream, byte[] buffer) throws IOException { OutputStream out = outputStream; InputStream in = inputStream; try { long total = 0; for (;;) { int res = in.read(buffer); if (res == -1) { break; } if (res > 0) { total += res; if (out != null) { out.write(buffer, 0, res); } } } if (out != null) { if (closeOutputStream) { out.close(); } else { out.flush(); } out = null; } in.close(); in = null; return total; } finally { IOUtils.closeQuietly(in); if (closeOutputStream) { IOUtils.closeQuietly(out); } } }
Example 10
Source File: HomePageUI.java From dubbo-switch with Apache License 2.0 | 5 votes |
private FileResource getFileResource(String path,String type){ InputStream inputStream = null; try { ClassPathResource resource = new ClassPathResource(path); inputStream = resource.getInputStream(); File tempFile = File.createTempFile("ds_" + System.currentTimeMillis(), type); FileUtils.copyInputStreamToFile(inputStream, tempFile); return new FileResource(tempFile); } catch (Exception e) { e.printStackTrace(); return null; } finally { IOUtils.closeQuietly(inputStream); } }
Example 11
Source File: SysAttachMainUploadOperationService.java From mPaaS with Apache License 2.0 | 4 votes |
/** * API上传附件,并保存附件记录 * @param uploadFunction 需要具体实现的文件上传逻辑 * @return 生成的附件ID */ private String recordBytesUpload(SysAttachUploadBaseVO uploadBaseVO, InputStream inputStream, String entityId, String entityKey, Function<InputStream, SysAttachUploadResultVO> uploadFunction) { try { // 从已有文件中查找相同文件 SysAttachFile sameFile = sysAttachFileService.findExistedSameFile(uploadBaseVO.getFdMd5(), uploadBaseVO.getFdFileSize(), uploadBaseVO.getFdFileName(), uploadBaseVO.getFdFileExtName()); SysAttachFile attachFile; if (sameFile != null) { // 若有相同文件则使用相同文件 attachFile = sameFile; } else { // 没有则上传新文件 SysAttachUploadResultVO uploadResultVO = uploadFunction.apply(inputStream); if (uploadResultVO == null) { return null; } // 保存attachFile attachFile = new SysAttachFile(); attachFile.setFdId(uploadResultVO.getFileId()); attachFile.setFdFileName(uploadBaseVO.getFdFileName()); attachFile.setFdFileExtName(uploadBaseVO.getFdFileExtName()); attachFile.setFdFileSize(uploadBaseVO.getFdFileSize()); attachFile.setFdMd5(uploadBaseVO.getFdMd5()); attachFile.setFdFilePath(uploadResultVO.getFilePath()); attachFile.setFdTenantId(TenantUtil.getTenantId()); attachFile.setFdCreatorId(uploadBaseVO.getFdCreatorId()); attachFile.setFdStatus(AttachStatusEnum.VALID); attachFile.setFdEncryptMethod(sysAttachConfig.getCurrentEncryMethod()); attachFile.setFdLocation(sysAttachStoreService.getDefaultStoreLocation()); attachFile.setFdSysAttachCatalog(uploadResultVO.getSysAttachCatalog()); attachFile.setFdSysAttachModuleLocation(uploadResultVO.getSysAttachModuleLocation()); // TODO 如果是媒体,获取媒体宽高帧率 SysAttachMediaSizeVO mediaSizeVO = this.getMediaSize(uploadBaseVO.getFdFileExtName(), uploadResultVO.getFullPath()); if (mediaSizeVO != null) { attachFile.setFdWidth(mediaSizeVO.getWidth()); attachFile.setFdHeight(mediaSizeVO.getHeight()); } sysAttachFileService.add(attachFile); } // 获取临时文件过期时间配置 SysAttachConfigVO configVO = applicationConfig.get(SysAttachConfigVO.class); Integer overdueSeconds = configVO != null ? configVO.getTempOverdueSeconds() : SysAttachConstant.ATTACH_TEMP_OVERDUE_SECONDS_DEFAULT; long overdueMillis = overdueSeconds * 1000L; // 生成attachMain String attachId = IDGenerator.generateID(); SysAttachMain attachMainTemp = new SysAttachMain(); // 关联已有文件或新创建的文件 attachMainTemp.setFdId(attachId); attachMainTemp.setFdSysAttachFile(attachFile); attachMainTemp.setFdFileName(attachFile.getFdFileName()); attachMainTemp.setFdFileExtName(attachFile.getFdFileExtName()); attachMainTemp.setFdFileSize(attachFile.getFdFileSize()); attachMainTemp.setFdCreatorId(attachFile.getFdCreatorId()); attachMainTemp.setFdLastModifier(attachFile.getFdLastModifier()); attachMainTemp.setFdEntityName(uploadBaseVO.getFdEntityName()); attachMainTemp.setFdTenantId(TenantUtil.getTenantId()); attachMainTemp.setFdMimeType(MimeTypeUtil.getMimeType(attachFile.getFdFileName() + NamingConstant.DOT + attachFile.getFdFileExtName())); attachMainTemp.setFdStatus(AttachStatusEnum.VALID); attachMainTemp.setFdEffectType(AttachEffectTypeEnum.TRANSIENT); attachMainTemp.setFdExpirationTime(new Timestamp(System.currentTimeMillis() + overdueMillis)); attachMainTemp.setFdExtendInfo(uploadBaseVO.getFdExtendInfo()); attachMainTemp.setFdWidth(attachFile.getFdWidth()); attachMainTemp.setFdHeight(attachFile.getFdHeight()); attachMainTemp.setFdAnonymous(uploadBaseVO.getFdAnonymous()); sysAttachService.add(attachMainTemp); return attachId; } catch (Exception e) { throw new KmssServiceException("sys-attach:sys.attach.msg.error.SysAttachWriteFailed", e); } finally { IOUtils.closeQuietly(inputStream); } }