版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaopingga/article/details/79496760
Jfinal附件、照片上传及下载
1、附件前台
<div class="form-group">
<label class="col-sm-4 control-label no-padding-right" for="form-field-1">附件</label>
<div class="col-sm-6">
<% if(!isEmpty(param.fujian)) {%>
<a type="button" href="${cxt!}/test/param/downloadFujian/${escapeXml(param.ids!)}" >下载附件</a>
<% } else { %>
<span> 无附件</span>
<% }%>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label no-padding-right" for="form-field-1">附件</label>
<div class="col-sm-6">
<input type="file" name="param.fujian" class="input-xlarge">
</div>
</div>
2、附件后台
// 保存
public void save() {
List<UploadFile> filelist = getFiles();
Param param = getModel(Param.class);
for(UploadFile file : filelist) {
if(file.getParameterName().contains("fujian")) {
param.setFujian(file.getFileName());
} else {
param.setZhaopian(file.getFileName());
}
}
param.save();
redirect("/test/param");
}
public void downloadFujian() { // 下载
Param param = Param.dao.findById(getPara());
if(StringUtils.isNotBlank(param.getFujian())) {
renderFile(param.getFujian());
} else {
renderNull();
}
}
3、照片前台
var file;
$(function () {
$("#uploadfile").change(function(){
$("#lookFile").hide();
$("#checkFile").show();
file = this.files[0];
});
});
/* 查看照片(保存后) */
function showUri() {
var i = Math.round(Math.random() * 10000);
var html = "<image src='${cxt!}/test/param/viewZhaopian/"+$("#uri").val()+"-"+ i +"' style='width:320px;height:240px'></image>"
var d = dialog({
title: '操作提示',
content: html,
okValue: '确定',
ok: function () {}
});
d.show();
out.clear();
out = pageContext.pushBody();
}
/* 照片预览(保存前) */
function previewFile() {
var url = null ;
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
var html = "<image src='"+url+"' style='width:320px;height:240px'></image>"
var d = dialog({
title: '操作提示',
content: html,
okValue: '确定',
ok: function () {}
});
d.show();
}
<div class="form-group">
<label class="col-sm-4 control-label no-padding-right" for="form-field-1">照片</label>
<div class="col-sm-6">
<input id="uri" value="${escapeXml(param.ids!)}" type="hidden"/>
<input type="file" id="uploadfile" name="param.zhaopian" class="input-xlarge" accept="image/*"/>
<a type="button" id="lookFile" ${isEmpty(param.zhaopian)?'hidden':'' } onclick="showUri()">查看照片</a>
<a type="button" id="checkFile" onclick="previewFile()" hidden>预览照片</a>
</div>
</div>
4、照片后台(使用文件流实现),一定记得在结尾加上renderNull()
public void viewZhaopian() {
Param param = Param.dao.findById(getPara(0));
byte[] buffer = new byte[1024*8];
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
try {
String realPath = PathKit.getWebRootPath() + "/files/";
String fileName = param.getZhaopian();
if (getRequest().getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
fileName = URLEncoder.encode(fileName, "UTF-8");
} else {
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
}
File file = new File(realPath, fileName);
getResponse().setCharacterEncoding("UTF-8"); // 设置编码格式
getResponse().setContentType("application/force-download");// 设置强制下载不打开
getResponse().addHeader("Content-Disposition","attachment;fileName=" + fileName);// 设置文件名
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
os = getResponse().getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
bis.close();
fis.close();
os.flush();
os.close();
} catch (Exception e) {
log.error("图片查看失败,原因:",e);
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
renderNull();
}