Example usage for java.io BufferedOutputStream close

List of usage examples for java.io BufferedOutputStream close

Introduction

In this page you can find the example usage for java.io BufferedOutputStream close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with the stream.

Usage

From source file:org.pdfgal.pdfgalweb.utils.impl.FileUtilsImpl.java

@Override
public String saveFile(final MultipartFile file) {

    String name = null;/* w w w  .ja  va  2 s.c  om*/

    if (!file.isEmpty()) {
        try {
            final String originalName = file.getOriginalFilename();
            name = this.getAutogeneratedName(originalName);
            final byte[] bytes = file.getBytes();
            final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
            stream.write(bytes);
            stream.close();
        } catch (final Exception e) {
            name = null;// TODO Incluir no log.
        }
    }

    return name;
}

From source file:julie.com.mikaelson.file.controller.FileInfoController.java

public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {// w ww. j ava2  s.  c  om
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File(name + "-uploaded")));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded " + name + " into " + name + "-uploaded !";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

From source file:com.aurel.track.admin.customize.category.report.ReportBL.java

/**
 * Save the new template at given location(repository, project)
 * @param repository//from  w  w  w .  ja  va  2 s. c o  m
 * @param personID
 * @param project
 * @param templateName
 * @param uploadZip
 * @return an error meassage if exist
 */
public static void saveTemplate(Integer id, ZipInputStream zis) throws IOException {
    // specify buffer size for extraction
    final int BUFFER = 2048;
    // Open Zip file for reading
    File unzipDestinationDirectory = getDirTemplate(id);
    BufferedOutputStream dest = null;
    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
        File destFile = new File(unzipDestinationDirectory, entry.getName());
        if (destFile.exists()) {
            destFile.delete();
        }
        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();
        // create the parent directory structure if needed
        destinationParent.mkdirs();
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
}

From source file:com.card.loop.xyz.pageControllers.LOIDEController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadLearningElement(@RequestParam("title") String title, @RequestParam("author") String authorID,
        @RequestParam("description") String description, @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {// www. j a v a2 s . c om
            byte[] bytes = file.getBytes();
            File fil = new File(AppConfig.UPLOAD_BASE_PATH + title);

            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(authorID);
            le.setDescription(description);
            le.setDownloads(0);
            le.setStatus(1);
            le.setRating(1);
            le.setUploadDate(new Date());
            le.setFilePath(AppConfig.DOWNLOAD_BASE_PATH);
            le.setFilename(file.getOriginalFilename());
            le.setContentType(file.getOriginalFilename().split("\\.")[1]);
            daoLE.addLearningElement(le);

            //   LearningElement lo = new LearningElement(title, authorID, description);

            //   lo.setType(file.getOriginalFilename().split("\\.")[1]);
            //   Database.get().save(lo);

            System.out.println("UPLOAD FINISHED");
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        System.err.println("EMPTY FILE.");
    }
}

From source file:asciidoc.maven.plugin.tools.ZipHelper.java

public void unzipEntry(ZipFile zipfile, ZipEntry zipEntry, File outputDir) throws IOException {
    if (zipEntry.isDirectory()) {
        createDir(new File(outputDir, zipEntry.getName()));
        return;// ww w  .j av a  2s.c om
    }
    File outputFile = new File(outputDir, zipEntry.getName());
    if (!outputFile.getParentFile().exists()) {
        createDir(outputFile.getParentFile());
    }

    if (this.log.isDebugEnabled())
        this.log.debug("Extracting " + zipEntry);
    BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(zipEntry));
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
    try {
        IOUtils.copy(inputStream, outputStream);
    } finally {
        outputStream.close();
        inputStream.close();
    }

    if ((outputFile != null)
            && (!outputFile.isDirectory() && FileHelper.getFileExtension(outputFile).equalsIgnoreCase("py"))) {
        outputFile.setExecutable(true);
    }
}

From source file:com.panet.imeta.core.xml.XMLHandler.java

/**
 * Build an XML string (including a carriage return) for a certain tag
 * binary (byte[]) value//from  w  w w  . j  av a  2s  .c o  m
 * 
 * @param tag
 *            The XML tag
 * @param val
 *            The binary value of the tag
 * @return The XML String for the tag.
 * @throws IOException
 *             in case there is an Base64 or GZip encoding problem
 */
public static final String addTagValue(String tag, byte[] val, boolean cr) throws IOException {
    String string;
    if (val == null) {
        string = null;
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzos = new GZIPOutputStream(baos);
        BufferedOutputStream bos = new BufferedOutputStream(gzos);
        bos.write(val);
        bos.flush();
        bos.close();

        string = new String(Base64.encodeBase64(baos.toByteArray()));
    }

    return addTagValue(tag, string, true);
}

From source file:com.xumpy.timesheets.controller.pages.TimesheetsCtrl.java

@RequestMapping(value = "timesheets/saveSQLite")
public String saveSQLiteDB(@RequestParam("file") MultipartFile file) throws IOException {
    if (!file.isEmpty()) {
        BufferedOutputStream stream = new BufferedOutputStream(
                new FileOutputStream(new File("/tmp/timeRecording.db")));
        stream.write(file.getBytes());/*from   ww  w. j  a va2s  .c  om*/
        stream.close();
    }

    return "redirect:/timesheets/importTimeRecordings";
}

From source file:hu.sztaki.lpds.pgportal.services.dspace.DSpaceUtil.java

/**
 * Extracts any file from <code>inputFile</code> that begins with
 * 'bitstream' to <code>outputFile</code>.
 * Adapted from examples on//w w  w  .ja  v a  2 s. c  o  m
 * http://java.sun.com/developer/technicalArticles/Programming/compression/
 *
 * @param inputFile  File to extract 'bitstream...' from
 * @param outputFile  File to extract 'bitstream...' to
 */
private static void unzipBitstream(InputStream is, File outputFile) throws IOException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry;
    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().matches("bitstream.*")) {
                int count;
                byte data[] = new byte[BUFFER];

                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(outputFile.getAbsoluteFile());
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                    //System.out.println("Writing: " + outputFile.getAbsoluteFile());
                }
                dest.flush();
                dest.close();
            }
        }
    } finally {
        zis.close();
        try {
            dest.close();
        } catch (Exception e) {
        }
    }
}

From source file:net.zcarioca.jmx.servlet.handlers.RestRequestHandler.java

public void respond(HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {//from   w  ww  . ja  va 2s.  c  om
        Object value = getResponse(request);

        if (value != null) {
            response.setContentType(MediaType.APPLICATION_JSON);
            response.setCharacterEncoding("UTF-8");

            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(out, value);

            out.flush();
            out.close();
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } catch (InvalidObjectException exc) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    } catch (IOException exc) {
        throw exc;
    } catch (Exception exc) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:com.gsr.myschool.server.reporting.convocation.ConvocationController.java

@RequestMapping(method = RequestMethod.GET, produces = "application/pdf")
@ResponseStatus(HttpStatus.OK)/*from   w  w w .j  a v a 2  s.  c o m*/
public void generateConvocation(@RequestParam String number, HttpServletResponse response) {
    ReportDTO dto = convocationService.generateConvocation(number);
    try {
        response.addHeader("Content-Disposition", "attachment; filename=" + dto.getFileName());

        BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        byte[] result = reportService.generatePdf(dto);

        outputStream.write(result, 0, result.length);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}