List of usage examples for org.apache.commons.io FileUtils readFileToByteArray
public static byte[] readFileToByteArray(File file) throws IOException
From source file:com.github.jengelman.gradle.plugins.integration.TestFile.java
private byte[] getHash(String algorithm) { try {/*from w w w. java2 s. c om*/ MessageDigest messageDigest = MessageDigest.getInstance(algorithm); messageDigest.update(FileUtils.readFileToByteArray(this)); return messageDigest.digest(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:io.appium.java_client.ComparesImages.java
/** * Performs images matching to calculate the similarity score between them. * The flow there is similar to the one used in * {@link #findImageOccurrence(byte[], byte[], OccurrenceMatchingOptions)}, * but it is mandatory that both images are of equal size. * * @param image1 The location of the full image * @param image2 The location of the partial image * @param options comparison options/*from ww w.j a v a 2 s. c om*/ * @return Matching result. The configuration of fields in the result depends on comparison options. */ default SimilarityMatchingResult getImagesSimilarity(File image1, File image2, @Nullable SimilarityMatchingOptions options) throws IOException { return getImagesSimilarity(Base64.encodeBase64(FileUtils.readFileToByteArray(image1)), Base64.encodeBase64(FileUtils.readFileToByteArray(image2)), options); }
From source file:com.huawei.ais.demo.TokenDemo.java
/** * Base64???Token???/* w w w . j a v a2 s. c om*/ * @param token token? * @param formFile * @throws IOException */ public static void requestOcrGeneralTableBase64(String token, String formFile) { // 1.??? String url = "https://ais.cn-north-1.myhuaweicloud.com/v1.0/ocr/general-table"; Header[] headers = new Header[] { new BasicHeader("X-Auth-Token", token), new BasicHeader("Content-Type", ContentType.APPLICATION_JSON.toString()) }; try { byte[] fileData = FileUtils.readFileToByteArray(new File(formFile)); String fileBase64Str = Base64.encodeBase64String(fileData); JSONObject json = new JSONObject(); json.put("image", fileBase64Str); StringEntity stringEntity = new StringEntity(json.toJSONString(), "utf-8"); // 2.??, POST?? HttpResponse response = HttpClientUtils.post(url, headers, stringEntity); System.out.println(response); String content = IOUtils.toString(response.getEntity().getContent()); System.out.println(content); } catch (Exception e) { e.printStackTrace(); } }
From source file:de.unisaarland.swan.rest.services.v1.ProjectFacadeREST.java
/** * This method returns a zip archive containing all Users annotations project * related. For each document and user pair will be a single .xmi file * created in the UIMA format.//from www.j a va 2 s. c o m * * @param projId * @return */ @GET @Path("/exportXmi/{projId}") @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response exportProjectByProjIdAsXmiZip(@PathParam("projId") Long projId) { try { LoginUtil.check(usersDAO.checkLogin(getSessionID(), Users.RoleType.projectmanager)); Project proj = (Project) projectDAO.find(projId, false); ExportUtil exportUtil = new ExportUtil(annotationDAO, linkDAO); File file = exportUtil.getExportDataInXMI(proj); return Response.ok(FileUtils.readFileToByteArray(file)) .header("Content-Disposition", "attachment; filename=\"exportXmi_" + proj.getName() + ".zip\"") .build(); } catch (SecurityException e) { return Response.status(Response.Status.FORBIDDEN).build(); } catch (IOException ex) { Logger.getLogger(ProjectFacadeREST.class.getName()).log(Level.SEVERE, null, ex); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (NoResultException e) { return Response.status(Response.Status.BAD_REQUEST).build(); } }
From source file:de.unisaarland.swan.export.ExportUtil.java
private void marshalScheme(Project proj, ZipOutputStream zos) throws IOException { final Scheme schemeOrig = proj.getScheme(); final de.unisaarland.swan.export.model.xml.scheme.Scheme schemeExport = (de.unisaarland.swan.export.model.xml.scheme.Scheme) mapperFacade .map(schemeOrig, SCHEME_EXPORT_CLASS); String fileName = schemeExport.getName() + ".xml"; File schemefile = new File(fileName); marshalXMLToSingleFile(schemeExport, schemefile); zos.putNextEntry(new ZipEntry(fileName)); zos.write(FileUtils.readFileToByteArray(schemefile)); zos.closeEntry();//w ww. jav a2 s. co m }
From source file:be.fedict.eid.idp.admin.webapp.bean.RPBean.java
@Override @Begin(join = true)//from w w w.j a v a2 s . co m public void uploadListenerLogo(UploadEvent event) throws IOException { UploadItem item = event.getUploadItem(); this.log.debug(item.getContentType()); this.log.debug(item.getFileSize()); this.log.debug(item.getFileName()); byte[] logoBytes; if (null == item.getData()) { // meaning createTempFiles is set to true in the SeamFilter logoBytes = FileUtils.readFileToByteArray(item.getFile()); } else { logoBytes = item.getData(); } this.selectedRP.setLogo(logoBytes); }
From source file:com.t3.model.AssetManager.java
/** * Create an asset from a file./*from w w w . j ava 2 s .c o m*/ * * @param file * File to use for asset * @return Asset associated with the file * @throws IOException */ public static Asset createAsset(File file) throws IOException { return new Asset(FileUtil.getNameWithoutExtension(file), FileUtils.readFileToByteArray(file)); }
From source file:de.mpg.imeji.logic.storage.util.ImageUtils.java
/** * Transform a image {@link File} to a jpeg file * /* w ww .j a v a2 s. com*/ * @param f - the {@link File} where the image is * @param dec - The {@link ImageDecoder} needed to decode the passed image * @return the image as {@link Byte} array */ private static byte[] image2Jpeg(File f, ImageDecoder dec) { try { RenderedImage ri = dec.decodeAsRenderedImage(0); File jpgFile = File.createTempFile("imeji_upload", "jpg.tmp"); FileOutputStream fos = new FileOutputStream(jpgFile); JPEGEncodeParam jParam = new JPEGEncodeParam(); jParam.setQuality(1.0f); ImageEncoder imageEncoder = ImageCodec.createImageEncoder("JPEG", fos, jParam); imageEncoder.encode(ri); fos.flush(); return FileUtils.readFileToByteArray(jpgFile); } catch (Exception e) { throw new RuntimeException("Error transforming image file to jpeg", e); } }
From source file:eu.planets_project.tb.impl.services.mockups.workflow.WorkflowDroidXCDLExtractorComparator.java
/** * Runs the XCDL extractor on a given file and for a given xcel descriptor * Returns the XCDL description (UTF-8 encoded) for the provided file * @param f1/* w ww . j ava 2 s. c om*/ * @param xcel * @return * @throws Exception */ private String runXCDLExtractor(File f1 /*, String xcel*/) throws Exception { URL url = null; try { url = new URL(URL_XCDLEXTRACTOR); Service service = Service.create(url, new QName(PlanetsServices.NS, Migrate.NAME)); Migrate extractor = service.getPort(Migrate.class); //the service's input byte[] array = FileUtils.readFileToByteArray(f1); //the service call and it's result DigitalObject digitalObject = extractor .migrate(new DigitalObject.Builder(Content.byValue(array)).build(), null, null, null) .getDigitalObject(); byte[] results = IOUtils.toByteArray(digitalObject.getContent().getInputStream()); String xcdl = new String(results, "UTF-8"); if (xcdl == null) { throw new Exception("XCDL extraction failed - please check service logs for details"); } return xcdl; } catch (MalformedURLException e) { e.printStackTrace(); throw e; } catch (UnsupportedEncodingException e) { //xcel file was not UTF-8 encodable e.printStackTrace(); throw e; } catch (PlanetsException e) { //error calling the web-service e.printStackTrace(); throw e; } }
From source file:com.tohours.imo.module.AttractModule.java
private int addFile(String path, String name, String contentType) throws IOException { Date now = new Date(); Files files = new Files(); files.setName(name);/*w ww . ja v a2 s. c o m*/ files.setPath(path); files.setContentType(contentType); files.setCreateTime(now); files.setUpdateTime(now); File file = new File(this.getRealPath(path)); files.setData(FileUtils.readFileToByteArray(file)); files = dao.insert(files); return files.getId(); }