Search in sources :

Example 1 with MimeType

use of org.compiere.util.MimeType in project adempiere by adempiere.

the class MobileUtil method streamFile.

//	streamAttachment
/**
	 * 	Stream File
	 *	@param response response
	 *	@param file file to stream
	 *	@return error message or null
	 */
public static String streamFile(HttpServletResponse response, File file) {
    if (file == null)
        return "No File";
    if (!file.exists())
        return "File not found: " + file.getAbsolutePath();
    MimeType mimeType = MimeType.get(file.getAbsolutePath());
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    FileInputStream in = null;
    ServletOutputStream out = null;
    //	Stream File
    try {
        //	8k Buffer
        int bufferSize = 8 * 1024;
        int fileLength = (int) file.length();
        //
        response.setContentType(mimeType.getMimeType());
        response.setBufferSize(bufferSize);
        // response.setContentLength(fileLength);
        // response.setHeader("Content-disposition", "attachment; filename=" + "Example.pdf" );
        // response.setHeader("Accept-Ranges", "bytes");
        // response.setHeader("Cache-Control", "max-age=0");
        //
        log.fine(file.toString());
        //	timer start
        long time = System.currentTimeMillis();
        //	Get Data
        in = new FileInputStream(file);
        bis = new BufferedInputStream(in);
        out = response.getOutputStream();
        bos = new BufferedOutputStream(out);
        byte[] bytes = new byte[bufferSize];
        int c;
        while ((c = bis.read(bytes, 0, bytes.length)) != -1) {
            bos.write(bytes, 0, c);
        }
        //
        out.flush();
        //
        time = System.currentTimeMillis() - time;
        double speed = (fileLength / 1024) / ((double) time / 1000);
        log.info("Length=" + fileLength + " - " + time + " ms - " + speed + " kB/sec - " + mimeType);
    } catch (Exception ex) {
        log.log(Level.SEVERE, ex.toString());
        return "Streaming error - " + ex;
    } finally {
        try {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
            if (out != null)
                out.close();
            if (in != null)
                in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) MimeType(org.compiere.util.MimeType) FileInputStream(java.io.FileInputStream) ServletException(javax.servlet.ServletException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AddressException (javax.mail.internet.AddressException)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 MimeType (org.compiere.util.MimeType)1