标签:original multipart index 静态文件 static random ret Servle http
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; }
标签:original multipart index 静态文件 static random ret Servle http
原文地址:https://www.cnblogs.com/yifanSJ/p/13305150.html