码迷,mamicode.com
首页 > 其他好文 > 详细

1. 存储

时间:2020-07-15 15:30:50      阅读:54      评论:0      收藏:0      [点我收藏+]

标签: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;
}

 

1. 存储

标签:original   multipart   index   静态文件   static   random   ret   Servle   http   

原文地址:https://www.cnblogs.com/yifanSJ/p/13305150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!