springboot上传文件至tomcat下的某个目录下-XSSFWorkbook生成Excel后保存到服务器中

上传文件至服务器的Tomcat的目录下

 @ResponseBody
    @PostMapping(value = ["/upload"])
    fun testUpload(@RequestParam("file") file: MultipartFile): SBaseResponse {
        try {

            var path:String
            val tomcat_path = System.getProperty("user.dir")
            val myFileName = file.originalFilename
            val fileName = UUID.randomUUID().toString() + "." + myFileName.substring(myFileName.lastIndexOf(".") + 1)
            //获取tomcat中项目同级路径
            val bin_path = tomcat_path.substring(tomcat_path.lastIndexOf("/") + 1, tomcat_path.length)
            if ("bin" == bin_path) {
                path = tomcat_path.substring(0, System.getProperty("user.dir").lastIndexOf("/")) + "/temp/" + "/upload/"
            } else {
                path = "$tomcat_path/temp/upload/"
            }
            logger.info("上传图片的路径:$path$fileName")
            val fileDir = File(path + fileName)
            //如果不存在 则创建
            if (!fileDir.exists()) {
                fileDir.mkdirs()
            }
            // 将内存中的数据写入磁盘
            file.transferTo(fileDir);
            return SBaseResponse(SResponseType.success, fileName)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,"error:" + e.message)
        }

    }

上传的Excel 保存到服务器tomcat

 fun importExcelPoint(file: MultipartFile): SBaseResponse {
        try {
            if (file.isEmpty) {
                throw Exception("未选择文件")
            }

            val projectId = SPageContext.getHeader("ProjectId")
            // 转为 XSSFWorkbook 对象
            val workbook = XSSFWorkbook(file.inputStream)
            // 读取第一个 sheet
            val sheet = workbook.getSheetAt(0)
            // 时间
            val date: String = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
            var path:String
            // Tomcat 路径
            val tomcat_path = System.getProperty("user.dir")
            // 上传的文件名
            val myFileName = file.originalFilename
            // 重组文件名 
            val fileName = projectId +  date + "." + myFileName.substring(myFileName.lastIndexOf(".") + 1)
            //获取tomcat中项目同级路径
            val bin_path = tomcat_path.substring(tomcat_path.lastIndexOf("/") + 1, tomcat_path.length)
            if ("bin" == bin_path) {
                path = tomcat_path.substring(0, System.getProperty("user.dir").lastIndexOf("/")) + "/temp/" + "/upload/"
            } else {
                path = "$tomcat_path/temp/upload/"
            }
            val fileDir = File(path)
            //如果不存在 则创建
            if (!fileDir.exists()) {
                fileDir.mkdirs()
            }
            // 文件
            val files = File(path,fileName)
            // 文件流
            val os: OutputStream = FileOutputStream(files)
            // 写入
            workbook.write(os)
            os.flush()
            os.close()

        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
        return SBaseResponse(SResponseType.success)
    } // Fun importExcel()

下载Tomcat 存储的文件

fun reportDownloadsPoint(response: HttpServletResponse,key:String) {
        var path:String

        val tomcat_path = System.getProperty("user.dir")

        //获取tomcat中项目同级路径
        val bin_path = tomcat_path.substring(tomcat_path.lastIndexOf("/") + 1, tomcat_path.length)
        if ("bin" == bin_path) {
            path = tomcat_path.substring(0, System.getProperty("user.dir").lastIndexOf("/")) + "/temp/" + "/upload/"
        } else {
            path = "$tomcat_path/temp/upload/"
        }
        logger.debug("dizhi=${path+key} ******************************")
        val file = File(path+key)
        val workbook = if (file.exists()) {
            XSSFWorkbook(file)
        } else {
            XSSFWorkbook(RelationTypeProjectService::class.java.classLoader.getResourceAsStream("excel/relation-template.xlsx"))
        }
        response.contentType = "application/octet-stream"
        response.setHeader("Content-disposition", "attachment;filename=${key}")
        response.flushBuffer()
        workbook.write(response.outputStream)
    } // Function templateDownloads()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值