项目中对于文件的处理本地正常运行,放到服务器上报错

场景:

最近做的一个项目涉及到非常多的文件的处理

比如文件上传、下载、pdf文件下载下来后进行盖章再将文件夹打包成流返回给前端下载。好不容易将这些功能写完本地自测通过了,结果放到服务器上去,所有涉及文件处理的都失效了。

原因:

最主要的还是linux环境和windows环境下地址路径的不同导致的。

windows下文件采取的是"\\",而linux环境下,是"/"。因为路径的书写方式不同,本地正常跑的项目,放到服务器上就报错。下面给出解决办法。

路径获取:linux支持下列方式获取地址存储

String downLoadLocalFile = request.getSession().getServletContext().getRealPath("") +"/DownLoad/"+timeMillis;
  /**
     * 批量盖章下载逻辑:
     * 1、获取需要下载的数据集合
     * 2、进行数据清洗,根据filename、ossfilename、secondossfilename这三个文件名获取清洗后的盖章文件
     * 3、将清洗后的文件数据放进一个batchDownLoadFileBodyList集合,进行循环盖章,最后打包输出
     * Attention:服务器上的文件地址和windows地址读取方式不一样,放到服务器上的地址一定要用linux文件方式存取
     * @param response
     * @param batchDownLoadReqBodyList
     * @return
     */
    @SneakyThrows
    @ApiOperation(value = "图纸接收——图纸批量下载")
    @RequestMapping(value = "/batchdownload", method = RequestMethod.POST)
    public RtMsg batchdownload(HttpServletRequest request,HttpServletResponse response, @RequestBody List<BatchDownLoadReqBody> batchDownLoadReqBodyList) {
        // 第一步:获取打包文件集合,并进行数据清洗
        List<BatchDownLoadFileBody> batchDownLoadFileBodyList = new ArrayList<>();
        for (BatchDownLoadReqBody batchDownLoadReqBody :batchDownLoadReqBodyList) {
            // 第一种情况:自己上传的文件,ossfilename为空,只有filename
            // 第二种情况:ossfilename不为空,secondossfilename为空
            // 第三种情况:ossfilename不为空,secondossfilename不为空
            String filename = batchDownLoadReqBody.getFilename();
            String ossfilename = batchDownLoadReqBody.getOssfilename();
            String secondossfilename = batchDownLoadReqBody.getSecondossfilename();

            // 需要盖章打包的文件
            BatchDownLoadFileBody batchDownLoadFileBody = new BatchDownLoadFileBody();
            // 将除了下载文件名以外的数据赋值进来
            BeanUtils.copyProperties(batchDownLoadReqBody,batchDownLoadFileBody);
            if (null == ossfilename) {
                batchDownLoadFileBody.setDownloadfilename(batchDownLoadReqBody.getFilename());
            } else {
                if (null == secondossfilename) {
                    batchDownLoadFileBody.setDownloadfilename(batchDownLoadReqBody.getOssfilename());
                } else {
                    batchDownLoadFileBody.setDownloadfilename(batchDownLoadReqBody.getOssfilename());
                    batchDownLoadFileBody.setDownloadfilename(batchDownLoadReqBody.getSecondossfilename());
                }
            }
            // 将数据赋值到批量打包的集合中
            batchDownLoadFileBodyList.add(batchDownLoadFileBody);

            String id = batchDownLoadReqBody.getId();
            String createBy = batchDownLoadReqBody.getCreateBy();
            //如果ossfilename是空的,那么下载状态和下载次数均设置为0
            DmDrawingDownloadlogDO insertDO = DmDrawingDownloadlogDO.builder().createBy(createBy).createTime(new Date())
                    .approverId(id).downloadStatus(null == ossfilename ? "0":"1").downloadTimes(null == ossfilename?0:1).build();

            // 图纸下载,下载日志表更新数据
            dmDrawingDownloadlogMapper.insert(insertDO);
        }

        // 第二步:创建下载文件和盖章文件的存取路径
        // 核心代码,路径一定要适配linux的,正常的windows路径挂在到服务器文件路径不生效
        String timeMillis = System.currentTimeMillis() + "";
        // oss下载文档的文件夹
        String downLoadLocalFile = request.getSession().getServletContext().getRealPath("") +"/DownLoad/"+timeMillis;
        File file = new File(downLoadLocalFile);
        file.mkdirs();

        // 本地盖章后的文件夹
        String downStampLocalFile = request.getSession().getServletContext().getRealPath("") +"/DownStamp/"+timeMillis;
        File filestamp = new File(downStampLocalFile);
        filestamp.mkdirs();

        // 第三步:循环遍历需要盖章的集合,注意区分下载文件夹和盖章文件夹的地址
        for (BatchDownLoadFileBody batchDownLoadFileBody : batchDownLoadFileBodyList) {
            String downloadfilename = batchDownLoadFileBody.getDownloadfilename();
            String controltype = batchDownLoadFileBody.getControltype();
            String urgentlevel = batchDownLoadFileBody.getUrgentlevel();
            String implementdate = batchDownLoadFileBody.getImplementdate();

            // 1、文件从服务器下载下来的路径
            String downLoadFilePath = downLoadLocalFile+"/"+ downloadfilename;
            simpleDownload(downLoadFilePath, downloadfilename);
            // 2、盖好章文件保存的地址
            String outstamplocalPath = downStampLocalFile + "/" + downloadfilename;

            // 3、根据紧急与普通的程度来盖不同的章,0:普通,1:紧急,2:一次性
            // 紧急正式图、普通正式图、普通一次性图,默认普通正式图
            URL imgPath = this.getClass().getClassLoader().getResource("stamp/putongformal.png");
            if (StringUtils.equals("正式图", controltype) && StringUtils.equals("紧急", urgentlevel)) {
                imgPath = this.getClass().getClassLoader().getResource("stamp/mergentformal.png");
            } else if (StringUtils.equals("正式图", controltype)&&StringUtils.equals("普通", urgentlevel) ) {
                imgPath = this.getClass().getClassLoader().getResource("stamp/putongformal.png");
            }else if (StringUtils.equals("正式图", controltype) && (null == urgentlevel)) {
                imgPath = this.getClass().getClassLoader().getResource("stamp/putongformal.png");
            }else if (StringUtils.equals("一次性", controltype) && StringUtils.equals("普通", urgentlevel)) {
                imgPath = this.getClass().getClassLoader().getResource("stamp/putongonetime.png");
            }else if (StringUtils.equals("一次性", controltype) && (null == urgentlevel)) {
                imgPath = this.getClass().getClassLoader().getResource("stamp/putongonetime.png");
            }
            logger.info("imgPath:"+imgPath);
            // 4、图纸盖章
            waterMark(downLoadFilePath, outstamplocalPath, imgPath,implementdate);
        }

        // 第四步:浏览器打包盖章后的文件夹,前端以压缩包形式进行下载
        createZip(downStampLocalFile, downStampLocalFile, response);

        return null;
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值