1. 存储
Posted yifansj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1. 存储相关的知识,希望对你有一定的参考价值。
1. 存储
/** * @param file 文件 * @param fileUploadPath 存储根路径 * @param staticFileServer 访问根域名 */ public static DataGrid<StaticFileObj> uploadFile(MultipartFile file, HttpServletRequest request, String fileUploadPath, String staticFileServer, OrganizationServiceI organizationService) { String host = WebUtils.getDomain(request); Organization organization = organizationService.findByDomainName(host); String companyFileName = UUID.randomUUID().toString().replace("-", ""); if (organization != null) { companyFileName = organization.getDominName().substring(organization.getDominName().indexOf(".") + 1, organization.getDominName().lastIndexOf(".")); } //改为上传到本地静态文件服务器 DataGrid dataGrid = new DataGrid<>(); try { String dateTime = new SimpleDateFormat("yyyy/MM/dd").format(new Date()); String saveDir = "importExcel/" + dateTime + "/" + companyFileName + "/"; String rootDir = fileUploadPath + saveDir; File root = new File(rootDir); if (!root.exists()) { root.mkdirs(); } String realFileName = file.getOriginalFilename(); int index = realFileName.lastIndexOf("."); String fileName = System.currentTimeMillis() + realFileName.substring(index); String filePath = rootDir + fileName; String savePath = saveDir + fileName; File newFile = new File(filePath); file.transferTo(newFile); dataGrid.setFlag(true); StaticFileObj staticFileObj = new StaticFileObj(); staticFileObj.setDownloadPath(staticFileServer + savePath); //访问路径 staticFileObj.setFileName(file.getName()); //文件名 staticFileObj.setSavePath(savePath); //存储路径 dataGrid.setObj(staticFileObj); } catch (Exception e) { logger.error(e.getMessage(), e); throw new EshipException(I18nUtil.get("上传文件失败")); } return dataGrid; }
以上是关于1. 存储的主要内容,如果未能解决你的问题,请参考以下文章