/** * Determines the {@link MediaType} for the given filename. * * <p>The default implementation will check the {@linkplain #setMediaTypes(Map) media types} * property first for a defined mapping. If not present, and if the Java Activation Framework can * be found on the classpath, it will call {@link FileTypeMap#getContentType(String)} * * <p>This method can be overridden to provide a different algorithm. * * @param filename the current request file name (i.e. {@code hotels.html}) * @return the media type, if any */ protected MediaType getMediaTypeFromFilename(String filename) { String extension = StringUtils.getFilenameExtension(filename); if (!StringUtils.hasText(extension)) { return null; } extension = extension.toLowerCase(Locale.ENGLISH); MediaType mediaType = this.mediaTypes.get(extension); if (mediaType == null) { String mimeType = getServletContext().getMimeType(filename); if (StringUtils.hasText(mimeType)) { mediaType = MediaType.parseMediaType(mimeType); } if (this.useJaf && (mediaType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mediaType))) { MediaType jafMediaType = ActivationMediaTypeFactory.getMediaType(filename); if (jafMediaType != null && !MediaType.APPLICATION_OCTET_STREAM.equals(jafMediaType)) { mediaType = jafMediaType; } } if (mediaType != null) { this.mediaTypes.putIfAbsent(extension, mediaType); } } return mediaType; }
public String getContent(String code) { Template tpl = loadByCode(code); if (tpl == null) { logger.warn("没有找到编码为'" + code + "'的模板!"); return null; } // 纯文本类型 if (tpl.isPureText()) return tpl.getContentEx(); // 附件的扩展名 String extension = StringUtils.getFilenameExtension(tpl.getPath()); if (tpl.getTemplateType().getCode().equals("word-docx") && extension.equals("docx")) return DocxUtils.loadText(tpl.getInputStream()); if (tpl.getTemplateType().getCode().equals("xls") && extension.equals("xls")) return XlsUtils.loadText(tpl.getInputStream()); if (tpl.getTemplateType().getCode().equals("xlsx") && extension.equals("xlsx")) return XlsxUtils.loadText(tpl.getInputStream()); if (tpl.getTemplateType().getCode().equals("html") && extension.equals("html")) return TemplateUtils.loadText(tpl.getInputStream()); logger.warn("文件后缀名:" + extension + ",未转换为字符串类型"); return null; }
private File loadTestFile(String name) throws IOException { InputStream in = getClass().getResourceAsStream("/" + name); File f = File.createTempFile(name, "." + StringUtils.getFilenameExtension(name)); FileCopyUtils.copy(in, new FileOutputStream(f)); return f; }
@Override protected String getMediaTypeKey(NativeWebRequest webRequest) { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); if (request == null) { logger.warn("An HttpServletRequest is required to determine the media type key"); return null; } String path = PATH_HELPER.getLookupPathForRequest(request); String filename = WebUtils.extractFullFilenameFromUrlPath(path); String extension = StringUtils.getFilenameExtension(filename); return (StringUtils.hasText(extension)) ? extension.toLowerCase(Locale.ENGLISH) : null; }
/** * A public method exposing the knowledge of the path extension strategy to resolve file * extensions to a MediaType in this case for a given {@link Resource}. The method first looks up * any explicitly registered file extensions first and then falls back on JAF if available. * * @param resource the resource to look up * @return the MediaType for the extension or {@code null}. * @since 4.3 */ public MediaType getMediaTypeForResource(Resource resource) { Assert.notNull(resource); MediaType mediaType = null; String filename = resource.getFilename(); String extension = StringUtils.getFilenameExtension(filename); if (extension != null) { mediaType = lookupMediaType(extension); } if (mediaType == null && JAF_PRESENT) { mediaType = JafMediaTypeFactory.getMediaType(filename); } if (MediaType.APPLICATION_OCTET_STREAM.equals(mediaType)) { mediaType = null; } return mediaType; }
@Deprecated public List<Resource> getAllServiceSrcDirs() { try { List<Resource> serviceSrcDirs = new ArrayList<Resource>(); List<Resource> serviceDirs = getFileSystem().listChildren(getProjectRoot().createRelative("services/")); for (Resource serviceDir : serviceDirs) { if (StringUtils.getFilenameExtension(serviceDir.getFilename()) == null) { Resource srcDir = serviceDir.createRelative("src/"); if (srcDir.exists()) { serviceSrcDirs.add(srcDir); } } } return serviceSrcDirs; } catch (IOException ex) { throw new WMRuntimeException(ex); } }
// 同步附件 private void syscAttach(Case4InfractBusiness cib, String procInstId, String taskId) { if (cib == null || procInstId == null || procInstId.length() == 0) return; List<Attach> attachs = attachService.findByPtype(Case4InfractBusiness.ATTACH_TYPE, cib.getUid()); if (attachs == null || attachs.size() == 0) return; for (Attach attach : attachs) { // 复制附件到流程附件位置中----开始--- // 扩展名 String extension = StringUtils.getFilenameExtension(attach.getPath()); // 文件存储的相对路径(年月),避免超出目录内文件数的限制 String subFolder = DateUtils.formatCalendar(Calendar.getInstance(), "yyyyMM"); // 上传文件存储的绝对路径 String appRealDir = Attach.DATA_REAL_PATH + File.separator + FlowAttach.DATA_SUB_PATH; // 所保存文件所在的目录的绝对路径名 String realFileDir = appRealDir + File.separator + subFolder; // 不含路径的文件名 String fileName = DateUtils.formatCalendar(Calendar.getInstance(), "yyyyMMddHHmmssSSSS") + "." + extension; // 所保存文件的绝对路径名 String realFilePath = realFileDir + File.separator + fileName; // 构建文件要保存到的目录 File _fileDir = new File(realFileDir); if (!_fileDir.exists()) { if (logger.isFatalEnabled()) logger.fatal("mkdir=" + realFileDir); _fileDir.mkdirs(); } // 直接复制附件 if (logger.isInfoEnabled()) logger.info("pure copy file"); // 附件路径 String path = Attach.DATA_REAL_PATH + File.separator + attach.getPath(); // 从附件目录下的指定文件复制到attachment目录下 try { FileCopyUtils.copy(new FileInputStream(new File(path)), new FileOutputStream(realFilePath)); } catch (Exception ex) { logger.error(ex.getMessage(), ex); } // 复制附件到流程附件位置中----结束--- // 插入流程附件记录信息 FlowAttach flowAttach = new FlowAttach(); flowAttach.setUid(idGeneratorService.next(FlowAttach.ATTACH_TYPE)); flowAttach.setType(FlowAttach.TYPE_ATTACHMENT); // 类型:1-附件,2-意见 flowAttach.setPid(procInstId); // 流程id flowAttach.setPath(subFolder + File.separator + fileName); // 附件路径,物理文件保存的相对路径 flowAttach.setExt(extension); // 扩展名 flowAttach.setSubject(attach.getSubject()); // 标题 flowAttach.setSize(attach.getSize()); flowAttach.setFormatted(false); // 附件是否需要格式化 if (taskId == null) { flowAttach.setCommon(true); // 公共附件 } else { flowAttach.setCommon(false); // 任务附件 flowAttach.setTid(taskId); } // 创建人,最后修改人信息 SystemContext context = SystemContextHolder.get(); flowAttach.setAuthor(context.getUserHistory()); flowAttach.setModifier(context.getUserHistory()); flowAttach.setFileDate(Calendar.getInstance()); flowAttach.setModifiedDate(Calendar.getInstance()); this.flowAttachService.save(flowAttach); } }
/** * 지정한 경로에서 파일의 확장자를 추출한다. 예) "mypath/myfile.txt" -> "txt". * * @param path 파일 경로(<tt>null</tt>이 될 수도 있음) * @return the extracted filename extension, or <tt>null</tt> if none */ public static String getFilenameExtension(String path) { return org.springframework.util.StringUtils.getFilenameExtension(path); }
private boolean isFile(Resource resource) { return resource != null && resource.exists() && StringUtils.hasText(StringUtils.getFilenameExtension(resource.getFilename())); }
public void formatTo(String code, Map<String, Object> args, OutputStream out) { Template tpl = loadByCode(code); if (tpl == null) logger.warn("没有找到编码为'" + code + "'的模板!"); if (tpl.isFormatted()) logger.warn("code=" + code + ",模板不可格式化。"); // 纯文本类型 if (tpl.isPureText()) { String source = this.getContent(code); String r = TemplateUtils.format(source, args); try { out.write(r.getBytes()); out.flush(); } catch (IOException e) { logger.warn("formatTo 写入数据到流错误:" + e.getMessage()); } finally { try { out.close(); } catch (IOException ex) { } } } else { InputStream is = tpl.getInputStream(); // 附件的扩展名 String extension = StringUtils.getFilenameExtension(tpl.getPath()); if ("word-docx".equals(tpl.getTemplateType().getCode()) && "docx".equals(extension)) { XWPFDocument docx = DocxUtils.format(is, args); try { docx.write(out); out.flush(); } catch (IOException e) { logger.warn("formatTo 写入数据到流错误:" + e.getMessage()); } finally { try { is.close(); out.close(); } catch (IOException ex) { } } } else if ("xls".equals(tpl.getTemplateType().getCode()) && "xls".equals(extension)) { HSSFWorkbook xls = XlsUtils.format(is, args); try { xls.write(out); out.flush(); } catch (IOException e) { e.printStackTrace(); logger.warn("formatTo 写入数据到流错误:" + e.getMessage()); } finally { try { is.close(); out.close(); } catch (IOException ex) { } } } else if ("xlsx".equals(tpl.getTemplateType().getCode()) && "xlsx".equals(extension)) { // Excel2007+ XSSFWorkbook xlsx = XlsxUtils.format(is, args); try { xlsx.write(out); out.flush(); } catch (IOException e) { e.printStackTrace(); logger.warn("formatTo 写入数据到流错误:" + e.getMessage()); } finally { try { is.close(); out.close(); } catch (IOException ex) { } } } else if ("html".equals(tpl.getTemplateType().getCode()) && "html".equals(extension)) { // html String source = FreeMarkerUtils.format(TemplateUtils.loadText(is), args); try { out.write(source.getBytes()); out.flush(); } catch (IOException e) { e.printStackTrace(); logger.warn("formatTo 写入数据到流错误:" + e.getMessage()); } finally { try { is.close(); out.close(); } catch (IOException ex) { } } } else { logger.warn("文件后缀名:" + extension + ",不能formatTo"); try { is.close(); out.close(); } catch (IOException ex) { } } } }