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;
}
Aggregations