List of usage examples for java.io BufferedOutputStream close
@Override public void close() throws IOException
From source file:jeffaschenk.tomcat.instance.generator.builders.TomcatInstanceBuilderHelper.java
/** * Extracts a zip entry (file entry)// w w w . j a v a 2 s. c o m * * @param zipIn * @param filePath * @throws IOException */ protected static void extractFile(GenerationLogger GENERATION_LOGGER, ZipInputStream zipIn, String filePath) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[4096]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); GENERATION_LOGGER.debug("Extracted zip Entry: " + filePath); }
From source file:bean.FileUploadBean.java
public String uploadResume() throws IOException, SQLException { UploadedFile uploadedPhoto = getResume(); //System.out.println("Name " + getName()); //System.out.println("tmp directory" System.getProperty("java.io.tmpdir")); //System.out.println("File Name " + uploadedPhoto.getFileName()); //System.out.println("Size " + uploadedPhoto.getSize()); this.filePath = "C:/Users/Usuario/Documents/fotos/"; objOutros.setUrl(this.filePath); byte[] bytes = null; if (null != uploadedPhoto) { bytes = uploadedPhoto.getContents(); String filename = FilenameUtils.getName(uploadedPhoto.getFileName()); BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(new File(this.filePath + filename))); stream.write(bytes);//from www . j a v a2 s. c om stream.close(); } return "success"; }
From source file:com.lugia.timetable.SubjectList.java
public boolean saveToFile(Context context) { try {/*w ww . j a va2 s . com*/ FileOutputStream out = context.openFileOutput(SAVEFILE, Context.MODE_PRIVATE); BufferedOutputStream stream = new BufferedOutputStream(out); stream.write(generateJSON().toString().getBytes()); stream.flush(); stream.close(); out.close(); } catch (Exception e) { // something went wrong Log.e(TAG, "Error on save!", e); return false; } return true; }
From source file:com.att.voice.TTS.java
public void say(String text, String file) { text = text.replace("\"", ""); try {/*from ww w . j a v a 2s . co m*/ HttpPost httpPost = new HttpPost("https://api.att.com/speech/v3/textToSpeech"); httpPost.setHeader("Authorization", "Bearer " + mAuthToken); httpPost.setHeader("Accept", "audio/x-wav"); httpPost.setHeader("Content-Type", "text/plain"); httpPost.setHeader("Tempo", "-16"); HttpEntity entity = new StringEntity(text, "UTF-8"); httpPost.setEntity(entity); HttpResponse response = httpclient.execute(httpPost); //String result = EntityUtils.toString(response.getEntity()); HttpEntity result = response.getEntity(); BufferedInputStream bis = new BufferedInputStream(result.getContent()); String filePath = System.getProperty("user.dir") + tempFile; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath))); int inByte; while ((inByte = bis.read()) != -1) { bos.write(inByte); } bis.close(); bos.close(); executeOnCommandLine("afplay " + System.getProperty("user.dir") + "/" + tempFile); } catch (Exception ex) { System.err.println(ex.getMessage()); } }
From source file:bean.FileUploadBean.java
public void uploadPhoto(FileUploadEvent e) throws IOException, SQLException { UploadedFile uploadedPhoto = e.getFile(); this.filePath = "C:/Users/Usuario/Documents/fotos/"; byte[] bytes = null; System.out.println("ID do usurio - " + loginBean.getId()); if (null != uploadedPhoto) { bytes = uploadedPhoto.getContents(); String filename = FilenameUtils.getName(uploadedPhoto.getFileName()); this.setName(filename); this.setUrl(filePath); BufferedOutputStream stream = new BufferedOutputStream( new FileOutputStream(new File(this.filePath + filename))); stream.write(bytes);// w w w . j av a2s . com stream.close(); documentosBD.inserirDocumentos(getName(), getUrl(), loginBean.getId()); pdfModify.modificaPDF(this.url, this.getName()); } FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage(FacesMessage.SEVERITY_INFO, uploadedPhoto.getFileName(), "")); }
From source file:com.aurel.track.util.PluginUtils.java
/** * Unzip this file into the given directory. If the zipFile is called "zipFile.zip", * the files will be placed into "targetDir/zipFile". * @param zipFile//from ww w . j av a 2s . c o m * @param targetDir */ public static void unzipFileIntoDirectory(File zipFile, File targetDir) { try { LOGGER.debug("Expanding Genji extension file " + zipFile.getName()); int BUFFER = 2048; File file = zipFile; ZipFile zip = new ZipFile(file); String newPath = targetDir + File.separator + zipFile.getName().substring(0, zipFile.getName().length() - 4); new File(newPath).mkdir(); Enumeration zipFileEntries = zip.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); File destFile = new File(newPath, currentEntry); //destFile = new File(newPath, destFile.getName()); File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; // establish buffer for writing file byte data[] = new byte[BUFFER]; // write the current file to disk LOGGER.debug("Unzipping " + destFile.getAbsolutePath()); FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } dest.flush(); dest.close(); is.close(); } } } catch (Exception e) { LOGGER.error(ExceptionUtils.getStackTrace(e)); } }
From source file:com.macdonst.ftpclient.FtpClient.java
/** * Downloads a file from a ftp server./*from w ww .j a v a 2 s. c o m*/ * @param filename the name to store the file locally * @param url the url of the server * @throws IOException */ private void get(String filename, URL url) throws IOException { FTPClient f = setup(url); BufferedOutputStream buffOut = null; buffOut = new BufferedOutputStream(new FileOutputStream(filename)); f.retrieveFile(extractFileName(url), buffOut); buffOut.flush(); buffOut.close(); teardown(f); }
From source file:com.sastix.cms.common.services.htmltopdf.PdfImpl.java
private File saveAs(String path, byte[] document) throws IOException { File file = new File(path); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); bufferedOutputStream.write(document); bufferedOutputStream.flush();//from w w w . j ava 2 s . c o m bufferedOutputStream.close(); return file; }
From source file:com.andrew.apollo.cache.ImageFetcher.java
/** * Download a {@link Bitmap} from a URL, write it to a disk and return the * File pointer. This implementation uses a simple disk cache. * * @param context The context to use/* w w w .j a v a 2 s. com*/ * @param urlString The URL to fetch * @return A {@link File} pointing to the fetched bitmap */ public static final File downloadBitmapToFile(final Context context, final String urlString, final String uniqueName) { final File cacheDir = ImageCache.getDiskCacheDir(context, uniqueName); if (!cacheDir.exists()) { cacheDir.mkdir(); } HttpURLConnection urlConnection = null; BufferedOutputStream out = null; try { final File tempFile = File.createTempFile("bitmap", null, cacheDir); //$NON-NLS-1$ final URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { return null; } final InputStream in = new BufferedInputStream(urlConnection.getInputStream(), IO_BUFFER_SIZE_BYTES); out = new BufferedOutputStream(new FileOutputStream(tempFile), IO_BUFFER_SIZE_BYTES); int oneByte; while ((oneByte = in.read()) != -1) { out.write(oneByte); } return tempFile; } catch (final IOException ignored) { ignored.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (out != null) { try { out.close(); } catch (final IOException ignored) { } } } return null; }
From source file:com.card.loop.xyz.controller.LearningElementController.java
@RequestMapping(value = "/upload", method = RequestMethod.POST) public ModelAndView upload(@RequestParam("title") String title, @RequestParam("author") String author, @RequestParam("subject") String subject, @RequestParam("description") String description, @RequestParam("file") MultipartFile file, @RequestParam("type") String type) { if (!file.isEmpty()) { try {// w w w .j av a 2s.c o m byte[] bytes = file.getBytes(); File fil = new File(AppConfig.USER_VARIABLE + AppConfig.LE_FILE_PATH + file.getOriginalFilename()); if (!fil.getParentFile().exists()) { fil.getParentFile().mkdirs(); } BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fil)); stream.write(bytes); stream.close(); LearningElement le = new LearningElement(); le.setTitle(title); le.setUploadedBy(author); le.setDescription(description); le.setDownloads(0); le.setStatus(1); le.setRating(1); le.setUploadDate(new Date()); le.setSubject(subject); le.setFilePath(AppConfig.LE_FILE_PATH); le.setFilename(file.getOriginalFilename()); le.setContentType(file.getOriginalFilename().split("\\.")[1]); dao.addLearningElement(le); System.out.println("UPLOAD LE FINISHED"); } catch (Exception e) { System.err.println(e.toString()); } } else { System.err.println("EMPTY FILE."); } return new ModelAndView("developer-le-loop-redirect"); }