引用
1.gsp 上传页面代码
<g:form controller="document" action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="myFile" /> <g:select name="purCategory.id" from="${purcatelist}" optionKey="id" value="${documentInstance?.purCategory?.id}" />
<input type="submit" value="上 传" />
</g:form>
注:enctype="multipart/form-data" 必须要写
引用
2.controller 里的代码
def upload = {
def documentInstance = new Document(params)
def url = params.myFile
// println url
def f = request.getFile('myFile')
if(!f.empty) {
def webRootDir = servletContext.getRealPath("/")
println webRootDir
def userDir = new File(webRootDir, "/payload/")
userDir.mkdirs()
f.transferTo( new File( userDir, f.originalFilename))
documentInstance.fileName = f.originalFilename
}
documentInstance.save()
documentInstance.errors.each{
println it
}
if(!documentInstance.hasErrors() && documentInstance.save()) {
flash.message = "上传成功"
redirect(action:show,id:documentInstance.id)
}
else {
render(view:'create',model:[documentInstance:documentInstance])
}
}
引用
3.下载1
可以直接在gsp页面下载 gsp上的代码
<g:each in="${documentInstanceList}" status="j" var="document">
<tr>
<td width="640" nowrap="nowrap" style="padding: 0 15px;border-bottom:1px #CCC dashed;">${fieldValue(bean:document, field:'title')}</td>
td width="64" height="40" class="table_link" style="border-bottom:1px #CCC dashed;">
<a href="${request.getContextPath()}/payload/${document.fileName}" target="_blank">下载</a></td>
</tr>
</g:each>
下载2
在controller里写下载action
def download = {
def documentInstance = Document.get(params.id)
def filename = documentInstance.fileName
// println filename
response.setHeader("Content-disposition", "attachment; filename=" + filename)
response.contentType = ""
def webRootDir = servletContext.getRealPath("/")
def filepath = new File(webRootDir, "/upload/${filename}")
//def filepath = "E:/founderweb/web-app/upload/${filename}"
// println filepath
def out = response.outputStream
def inputStream = new FileInputStream(filepath)
byte[] buffer = new byte[1024]
int i = -1
while ((i = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, i)
}
out.flush()
out.close()
inputStream.close()
}
页面gsp上代码
<g:link controller="document" action="download" id="${document.id}" target="_blank">下载</g:link>
引用
4.判断文件后缀名
int i = filename.LastIndexOf("."); //取得文件名中最后一个"."的索引
string newext = filename.Substring(i); //获取文件扩展名
if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
{
Response.Write("文件格式不正确,请上传gif、jpg、jpeg、bmp及png的格式图片文件!");
Response.End();
}
引用
5.判断文件上传大小
if(f.size>10240){
}
else{
}
引用
6.把上传文件名称以时间命名
def sdf = new SimpleDateFormat("yyyyMMddHHmmssS")//格式化时间输出
def rname = sdf.format(new Date())//取得当前时间,Date()是java.util包里的,这作为真实名称
// println rname
def name = f.originalFilename//得到上传文件的原名称
//println name
int i = name.lastIndexOf(".")//原名称里倒数第一个"."在哪里
def ext = name.substring(i+1)//取得后缀,及"."后面的字符
name = rname+"."+ext//拼凑而成
分享到:
相关推荐
小姐姐炫酷唯美引导页.zip
scratch少儿编程逻辑思维游戏源码-让它们跳起来.zip
少儿编程scratch项目源代码文件案例素材-战斗塔防.zip
前端开发_Vue全家桶_vw适配_vux组件库_mescroll滚动_fastclick优化_mock模拟_less预处理器_移动端脚手架_基于Vue2和Webpack3构建的移动
数据可视化分析_微信小程序开发_JavaScript_Canvas图表渲染_数据助手工具_仿微信小程序数据助手_支持线性图圆环图柱状图条形图_动态效果适配_商业数据分析_移动端数据
少儿编程scratch项目源代码文件案例素材-章鱼怪.zip
音乐流媒体_API开发_网易云音乐接口封装与数据抓取_提供完整的网易云音乐API调用解决方案_包含歌曲搜索_MP3资源获取_歌词解析_播放列表管理_歌手专辑查询_专辑详情展示_MV
物联网与健康监测_基于RISC-V架构ESP32-C3芯片与MAX30102MAX30205传感器_LVGL图形界面与WiFiBLE双模通信的智能手表系统_实时监测心率血氧体温
wangtengfei-hn_EmployeesExample_23540_1745868671962
Android开发_自定义View绘制动画效果_模仿小米手环App首页计步数据展示_实现可自定义目标步数当前步数颜色字体大小圆点尺寸的动态步数统计图表组件_用于健康运动类App展示
少儿编程scratch项目源代码文件案例素材-钻机机器人.zip
scratch少儿编程逻辑思维游戏源码-狂暴坦克.zip
scratch少儿编程逻辑思维游戏源码-拳击.zip
scratch少儿编程逻辑思维游戏源码-魔方冲刺.zip
少儿编程scratch项目源代码文件案例素材-重返危机.zip
杰奇CMS小说网站系统_php53-73_mysql5-MariaDB10_utf8mb4编码_GBK自动转码_INNODB存储引擎_多模块支持_前后台分离_电脑手机双版适配_
linyuan620_StockAnalysisAssistant_17128_1745866117970
本系统采用的数据库是Mysql,使用SpringBoot框架开发,运行环境使用Tomcat服务器,ECLIPSE 是本系统的开发平台。在设计过程中,充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面简洁等特点。
少儿编程scratch项目源代码文件案例素材-足球踩踏者.zip
scratch少儿编程逻辑思维游戏源码-能力实验室争斗.zip