List of usage examples for javax.servlet ServletContext getMimeType
public String getMimeType(String file);
null
if the MIME type is not known. From source file:org.projectforge.web.core.ResponseUtils.java
/** * Prepares download of a file. The content type will be detected automatically by the file name. * * @param filename Virtual file name. Any path infos will be truncated. * @param response//from w w w .java 2 s . c o m * @param ctx der Servletcontext * @param attach Download as Attachment */ public static void prepareDownload(String filename, HttpServletResponse response, ServletContext ctx, boolean attach) { String mimeType = null; try { mimeType = ctx.getMimeType(filename); } catch (Exception ex) { log.info("Exception while getting mime-type (using application/binary): " + ex); } if (mimeType == null) { response.setContentType("application/binary"); } else { response.setContentType(mimeType); } log.debug("Using content-type " + mimeType); final String filenameWithoutPath = FilenameUtils.getName(filename); if (attach == true) { response.setHeader("Content-disposition", "attachment; filename=\"" + filenameWithoutPath + "\""); } else { response.setHeader("Content-disposition", "inline; filename=\"" + filenameWithoutPath + "\""); } }
From source file:cn.vlabs.duckling.vwb.ui.accclb.AttSaver.java
private static String getMimeType(HttpServletRequest req, String fileName) { String mimetype = null;/*from w w w.jav a 2 s . com*/ if (req != null) { ServletContext s = req.getSession().getServletContext(); if (s != null) { mimetype = s.getMimeType(fileName.toLowerCase()); } } if (mimetype == null) { mimetype = "application/binary"; } return mimetype; }
From source file:com.ecyrd.jspwiki.attachment.AttachmentServlet.java
/** * Returns the mime type for this particular file. Case does not matter. * * @param ctx WikiContext; required to access the ServletContext of the request. * @param fileName The name to check for. * @return A valid mime type, or application/binary, if not recognized *///from ww w . j a v a 2s . c o m private static String getMimeType(WikiContext ctx, String fileName) { String mimetype = null; HttpServletRequest req = ctx.getHttpRequest(); if (req != null) { ServletContext s = req.getSession().getServletContext(); if (s != null) { mimetype = s.getMimeType(fileName.toLowerCase()); } } if (mimetype == null) { mimetype = "application/binary"; } return mimetype; }
From source file:edu.isi.wings.portal.classes.StorageHandler.java
public static Response streamFile(String location, ServletContext context) { final File f = new File(location); if (!f.exists()) return Response.status(Status.NOT_FOUND).build(); if (!f.canRead()) return Response.status(Status.FORBIDDEN).build(); StreamingOutput stream = new StreamingOutput() { @Override//from ww w . j a v a2 s . co m public void write(OutputStream os) throws IOException { try { if (f.isDirectory()) StorageHandler.streamDirectory(f, os); else StorageHandler.streamFile(f, os); } catch (Exception e) { e.printStackTrace(); } } }; String filename = f.getName(); String mime = context.getMimeType(f.getAbsolutePath()); if (f.isDirectory()) { filename += ".zip"; mime = "application/zip"; } return Response.ok(stream, mime).header("content-disposition", "attachment; filename = " + filename) .build(); }
From source file:app.controller.ResourceController.java
@GetMapping(value = "/download/{resourceType:.*}") public void download(HttpServletRequest request, HttpServletResponse response, @RequestParam String filename, @PathVariable int resourceType) throws IOException { logger.info("---------------download resource-----------------"); FileResource resource = new FileResource(); int result = resourceService.download(filename, resourceType, resource); if (result != BaseResponse.COMMON_SUCCESS) { throw new ResourceNotFoundException(); }// w w w . j a v a 2 s .c om File file = resource.file; // get MIME type of the file ServletContext context = request.getServletContext(); String mimeType = context.getMimeType(filename); if (mimeType == null) { mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE; } response.setContentType(mimeType); response.setContentLength((int) file.length()); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); FileCopyUtils.copy(new BufferedInputStream(new FileInputStream(file)), out); }
From source file:org.pentaho.test.platform.web.GetImageIT.java
@Before public void setUp() throws PlatformInitializationException, ServletException { request = mock(HttpServletRequest.class); when(request.getMethod()).thenReturn("GET"); response = mock(HttpServletResponse.class); servlet = spy(new GetImage()); final ServletConfig servletConfig = mock(ServletConfig.class); final ServletContext servletContext = mock(ServletContext.class); when(servletContext.getMimeType(anyString())).thenReturn(TEST_MIME_TYPE); when(servletConfig.getServletContext()).thenReturn(servletContext); servlet.init(servletConfig);/*from www. j av a 2 s . c o m*/ mp.start(); }
From source file:fr.insalyon.creatis.vip.datamanager.server.rpc.FileDownloadServiceImpl.java
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User user = (User) req.getSession().getAttribute(CoreConstants.SESSION_USER); String operationId = req.getParameter("operationid"); if (user != null && operationId != null && !operationId.isEmpty()) { try {//from w w w . j a v a2 s . c o m GRIDAPoolClient client = CoreUtil.getGRIDAPoolClient(); Operation operation = client.getOperationById(operationId); File file = new File(operation.getDest()); if (file.isDirectory()) { file = new File(operation.getDest() + "/" + FilenameUtils.getName(operation.getSource())); } int length = 0; ServletOutputStream op = resp.getOutputStream(); ServletContext context = getServletConfig().getServletContext(); String mimetype = context.getMimeType(file.getName()); logger.info("(" + user.getEmail() + ") Downloading '" + file.getAbsolutePath() + "'."); resp.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); resp.setContentLength((int) file.length()); resp.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); byte[] bbuf = new byte[4096]; DataInputStream in = new DataInputStream(new FileInputStream(file)); while ((in != null) && ((length = in.read(bbuf)) != -1)) { op.write(bbuf, 0, length); } in.close(); op.flush(); op.close(); } catch (GRIDAClientException ex) { logger.error(ex); } } }
From source file:org.exoplatform.application.gadget.GadgetRegister.java
private String getMimeType(ServletContext context, String fileName) { return (context.getMimeType(fileName) != null) ? context.getMimeType(fileName) : "text/plain"; }
From source file:gov.nih.nci.firebird.web.common.Struts2UploadedFileInfoTest.java
@Test public void testResolveContentTypeWithContext() throws IOException { FileUtils.writeByteArrayToFile(file, new byte[10]); ServletContext ctx = mock(ServletContext.class); String type = "my special type"; when(ctx.getMimeType(anyString())).thenReturn(type); info.setDataFileName("foo"); info.resolveContentType(ctx);/*ww w.j a v a 2s. c om*/ assertEquals(type, info.getDataContentType()); }
From source file:fr.insalyon.creatis.vip.core.server.rpc.GetFileServiceImpl.java
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try {/*from w w w . j a va2 s . c o m*/ User user = CoreDAOFactory.getDAOFactory().getUserDAO() .getUserBySession(req.getParameter(CoreConstants.COOKIES_SESSION)); String filepath = req.getParameter("filepath"); if (filepath != null && !filepath.isEmpty()) { File file = new File(Server.getInstance().getWorkflowsPath() + filepath); boolean isDir = false; if (file.isDirectory()) { String zipName = file.getAbsolutePath() + ".zip"; FolderZipper.zipFolder(file.getAbsolutePath(), zipName); filepath = zipName; file = new File(zipName); isDir = true; } logger.info("(" + user.getEmail() + ") Downloading file '" + filepath + "'."); int length = 0; ServletOutputStream op = resp.getOutputStream(); ServletContext context = getServletConfig().getServletContext(); String mimetype = context.getMimeType(file.getName()); resp.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); resp.setContentLength((int) file.length()); resp.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); byte[] bbuf = new byte[4096]; DataInputStream in = new DataInputStream(new FileInputStream(file)); while ((in != null) && ((length = in.read(bbuf)) != -1)) { op.write(bbuf, 0, length); } in.close(); op.flush(); op.close(); if (isDir) { FileUtils.deleteQuietly(file); } } } catch (DAOException ex) { throw new ServletException(ex); } }