Skip to content

Instantly share code, notes, and snippets.

@allwefantasy
Created May 6, 2016 02:36
Show Gist options
  • Save allwefantasy/45daa64bfab845d5cb829069b78c0283 to your computer and use it in GitHub Desktop.
Save allwefantasy/45daa64bfab845d5cb829069b78c0283 to your computer and use it in GitHub Desktop.
ServiceFramework 上传文件示例
@At(path = Array("/upload"), types = Array( RestRequest.Method.POST))
@BasicInfo(
desc = "可指定哪些服务器下载指定url地址的文件到指定目录",
state = State.alpha,
testParams = "",
testResult = "task submit",
author = "WilliamZhu",
email = "[email protected]"
)
def upload = {
auth
val fileName = param("fileName", "temp")
val env = new Environment(akkaMaster._conf)
val outputFile = new File(env.templateDirFile().getPath + "/assets/" + fileName)
logger.info(s"upload fileName ${fileName}, path ${outputFile.getAbsolutePath}")
val opf = new FileOutputStream(outputFile)
IOUtils.write(request.contentByteArray(),opf)
opf.close()
render("upload success")
}
@At(path = Array("/form/upload"), types = Array(RestRequest.Method.POST))
@BasicInfo(
desc = "可指定哪些服务器下载指定url地址的文件到指定目录",
state = State.alpha,
testParams = "",
testResult = "task submit",
author = "WilliamZhu",
email = "[email protected]"
)
def formUpload = {
auth
val items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request.httpServletRequest())
try {
items.filterNot(f => f.isFormField).map {
item =>
//val fieldName = item.getFieldName();
val fileName = FilenameUtils.getName(item.getName())
val fileContent = item.getInputStream()
val env = new Environment(akkaMaster._conf)
val targetPath = new File(env.templateDirFile().getPath + "/assets/" + fileName)
logger.info(s"upload to ${targetPath.getPath}")
FileUtils.copyInputStreamToFile(fileContent,targetPath)
}
} catch {
case e: Exception =>
logger.info("upload fail ", e)
render(500, s"upload fail,check master log ${e.getMessage}")
}
val fields = items.filter(f=>f.isFormField && f.getFieldName=="redirect")
val redirect = if(fields.size==0)"-" else fields.head.getString
if(redirect=="-") render("upload success") else redirectTo(redirect,WowCollections.map())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment