包装成formdata('file',fileEntry)
controller
package com.umh.doctorreferral.api.controller
import com.umh.doctorreferral.api.model.*
import com.umh.doctorreferral.api.service.ApiDoctorReferralCaseService
import com.umh.doctorreferral.core.model.doctorreferralcase.ReferralCasesSearchParam
import io.swagger.annotations.ApiResponse
import io.swagger.annotations.ApiResponses
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.util.*
/**
* @Description
* @Author hunagzp
* @Date 2021/7/22 16:01
*
*/
@RestController
@Tag(name = "医生转介API", description = "医生转介API")
class DoctorReferralCaseController {
@Autowired
private lateinit var doctorReferralCaseService : ApiDoctorReferralCaseService
@Operation(summary = "删除转介单附件", description = "删除附件")
@DeleteMapping("/referral-cases/{referralCaseId}/application-files/{applicationFileId}")
fun remove(@PathVariable referralCaseId : UUID, @PathVariable applicationFileId : UUID) {
doctorReferralCaseService.removeFile(referralCaseId, applicationFileId)
}
}
service
package com.umh.doctorreferral.api.service
import com.umh.doctorreferral.api.model.*
import com.umh.doctorreferral.core.error.ErrorCode
import com.umh.doctorreferral.core.error.ErrorCode.ERR_CONFLICTING_ID
import com.umh.doctorreferral.core.error.ErrorCode.ERR_ILLEGAL_ARGUMENT
import com.umh.doctorreferral.core.error.ErrorCode.ERR_ILLEGAL_OPERATION
import com.umh.doctorreferral.core.error.ErrorCode.ERR_ILLEGAL_REFERRAL_CASE_STATUS
import com.umh.doctorreferral.core.error.ErrorCode.ERR_NOT_EXIST_RECORD
import com.umh.doctorreferral.core.exception.ApplicationException
import com.umh.doctorreferral.core.model.doctorreferralcase.DoctorReferralCase
import com.umh.doctorreferral.core.model.doctorreferralcase.DoctorReferralCaseRepository
import com.umh.doctorreferral.core.model.doctorreferralcase.ReferralCasesSearchParam
import com.umh.doctorreferral.core.model.file.ApplicationFileRepository
import com.umh.doctorreferral.core.service.ApplicationFileService
import io.swagger.v3.oas.annotations.Operation
import org.slf4j.LoggerFactory
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*
import javax.persistence.EntityManager
import javax.persistence.PersistenceContext
import kotlin.collections.HashMap
/**
* @Description
* @Author hunagzp
* @Date 2021/7/22 16:04
*
*/
@Service
class ApiDoctorReferralCaseService {
private val logger = LoggerFactory.getLogger(this.javaClass)
@Autowired
private lateinit var applicationFileService: ApplicationFileService
@Autowired
private lateinit var doctorReferralCaseRepository:DoctorReferralCaseRepository
@Autowired
private lateinit var applicationFileRepository : ApplicationFileRepository
@Autowired
private lateinit var referralCasesWrapper: ReferralCasesWrapper
@Autowired
private lateinit var memberService: ApiMemberService
@PersistenceContext
private lateinit var em: EntityManager
@Transactional(rollbackFor = [Exception::class])
fun removeFile(referralCaseId : UUID, applicationFileId : UUID) {
val referralCaseOptional = doctorReferralCaseRepository.findById(referralCaseId)
val referralCase = referralCaseOptional.orElseThrow { throw ApplicationException(ERR_NOT_EXIST_RECORD) }
val documents = referralCase.documents
val iterator = documents?.iterator() ?: throw ApplicationException(ERR_ILLEGAL_OPERATION)
while (iterator.hasNext()) {
val document = iterator.next()
if (document.applicationFileId == applicationFileId) {
referralCase.documents!!.remove(document)
doctorReferralCaseRepository.saveAndFlush(referralCase)
applicationFileService.removeDeadFile(document.applicationFileId!!)
break
}
}
}
}
ApplicationFileService
package com.umh.doctorreferral.core.service
import com.amazonaws.services.s3.model.ObjectMetadata
import com.umh.doctorreferral.core.model.file.ApplicationFile
import com.umh.doctorreferral.core.model.file.ApplicationFileRepository
import com.umh.doctorreferral.core.properties.ApplicationProperties
import net.coobird.thumbnailator.Thumbnails
import org.apache.commons.io.IOUtils
import org.apache.commons.io.input.AutoCloseInputStream
import org.apache.tika.Tika
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.io.Resource
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.time.LocalDateTime
import java.util.*
@Service
class ApplicationFileService {
@Autowired
private lateinit var applicationProperties: ApplicationProperties
@Autowired
private lateinit var s3Service: S3Service
@Autowired
private lateinit var applicationFileRepository: ApplicationFileRepository
@Transactional
fun removeDeadFile(applicationFileId: UUID) {
val amazonS3 = s3Service.getAmazonS3()
applicationFileRepository.deleteById(applicationFileId)
amazonS3.deleteObject(s3Service.bucketName, S3Service.AppBucketFolder.THUMBNAIL.folderName + "/" + applicationFileId.toString())
amazonS3.deleteObject(s3Service.bucketName, S3Service.AppBucketFolder.FILE.folderName + "/" + applicationFileId.toString())
}
}
S3Service
package com.umh.doctorreferral.core.service
import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.client.builder.AwsClientBuilder
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import com.umh.doctorreferral.core.properties.ApplicationProperties
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service
class S3Service {
@Autowired
private lateinit var applicationProperties: ApplicationProperties
enum class AppBucketFolder(var folderName: String) {
FILE("file"),
THUMBNAIL("thumbnail"),
}
val bucketName: String?
get() {
return applicationProperties.s3.bucket
}
}
上传文件
package com.umh.doctorreferral.api.controller
import com.umh.doctorreferral.core.exception.ApplicationException
import com.umh.doctorreferral.core.model.file.ApplicationFile
import com.umh.doctorreferral.core.model.user.User
import com.umh.doctorreferral.core.service.ApplicationFileService
import org.apache.tika.Tika
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.io.InputStreamResource
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import java.net.http.HttpResponse
import java.util.*
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
@RestController
class ApplicationFileController {
private val logger = LoggerFactory.getLogger(this.javaClass)
@Autowired
private lateinit var applicationFileService: ApplicationFileService
@RequestMapping(value = ["/application-files"], method = [RequestMethod.POST])
fun upload(@RequestParam("file") uploadedFile: MultipartFile, httpRequest: HttpServletRequest): ApplicationFile {
var user: User? = null
if (SecurityContextHolder.getContext().authentication.principal is User) {
user = SecurityContextHolder.getContext().authentication.principal as User?
}
return applicationFileService.upload(uploadedFile)
}
}
package com.umh.doctorreferral.core.service
import com.amazonaws.services.s3.model.ObjectMetadata
import com.umh.doctorreferral.core.model.file.ApplicationFile
import com.umh.doctorreferral.core.model.file.ApplicationFileRepository
import com.umh.doctorreferral.core.properties.ApplicationProperties
import net.coobird.thumbnailator.Thumbnails
import org.apache.commons.io.IOUtils
import org.apache.commons.io.input.AutoCloseInputStream
import org.apache.tika.Tika
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.io.Resource
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.time.LocalDateTime
import java.util.*
@Service
class ApplicationFileService {
@Autowired
private lateinit var applicationProperties: ApplicationProperties
@Autowired
private lateinit var s3Service: S3Service
@Autowired
private lateinit var applicationFileRepository: ApplicationFileRepository
@Transactional
fun upload(file: MultipartFile): ApplicationFile {
return upload(file.resource, file.originalFilename)
}
@Transactional
fun upload(file: Resource, fileName: String? = null): ApplicationFile {
val fileId = UUID.randomUUID()
val mimeType = Tika().detect(file.inputStream)
var thumbnail: ByteArray? = null
if (mimeType.startsWith("image")) {
val out = ByteArrayOutputStream()
Thumbnails.of(file.inputStream)
.size(200, 200)
.outputFormat("png")
.toOutputStream(out)
thumbnail = out.toByteArray()
storeFile(fileId, ByteArrayInputStream(thumbnail), S3Service.AppBucketFolder.THUMBNAIL)
}
storeFile(fileId, file.inputStream, S3Service.AppBucketFolder.FILE)
val appFile = ApplicationFile().apply {
this.applicationFileId = fileId
this.fileName = fileName ?: ""
this.fileSize = file.contentLength()
this.permanent = false
this.hasThumbnail = thumbnail != null
this.thumbnail = thumbnail
}
applicationFileRepository.saveAndFlush(appFile)
return appFile
}
}