上传文件至服务器的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()