标签:
实现mvc模式基于jfinal框架
首先是上传接口:
前端代码基于bootstrap和插件ajaxfileupload.js的异步上传:test.html
<script src="${base}/resources/js/ajaxfileupload.js" type="text/javascript"></script>
<input type="file" id="map" name="map">
<button type="button" onclick="ajaxFileUpload()" class="btn btn-info" id="upload">上传图片</button>
<img id="img1" alt="上传的图片显示位置" src="" style="max-width:400px;max-length:400px;"/>
<script>
function ajaxFileUpload() {
if(!checkImageExt("map")){
return;
}
var map = $("#map").val();
if(!validate_required(map)){
alert("请选择要上传的图片!");
return;
}
$.ajaxFileUpload
(
{
url: ‘${base}/img/uploadMap‘, //用于文件上传的服务器端请求地址
type: ‘post‘,
data: { mapType: ‘2‘}, //此参数非常严谨,写错一个引号都不行
secureuri: false, //一般设置为false
fileElementId: ‘map‘, //文件上传空间的id属性 <input type="file" id="file" name="file" />
dataType: ‘json‘, //返回值类型 一般设置为json
success: function (data, status) //服务器成功响应处理函数
{
mapCode = data.imgName;
$("#img1").attr("src", "${base}/img/iconMap?imgName="+data.imgName);
if (typeof (data.error) != ‘undefined‘) {
if (data.error != ‘‘) {
alert("上传失败!");
} else {
alert(data.message);
}
}
},
error: function (data, status, e)//服务器响应失败处理函数
{
alert(e);
}
}
);
}
//检测文件格式
function checkImageExt(eleId) {
var _file = document.getElementById(eleId);
var i = _file.value.lastIndexOf(‘.‘);
var len = _file.value.length;
var extEndName = _file.value.substring(i + 1, len);
var extName = "GIF,BMP,JPG,JPEG,PNG";//首先对格式进行验证
if (extName.indexOf(extEndName.toUpperCase()) == -1) {
alert("*您只能输入" + extName + "格式的文件");
return false;
} else {
return true;
}
}
</script>
---------------------------------------------------------------------------------------------
控制器:FileCtrl.class代码如下:
/**
* 根据图片id获取图片
* 备注:调用此方法URL返回在html的直接是一个图片
* */
public void iconMap() throws IOException{
String imageCode = this.getPara("imgName");
EbMapFile image = null;
MapService imgSer = new MapService();
image = imgSer.findMapFile(imageCode);
if(image != null){
byte[] bs = image.getBytes("map_conten");
this.getResponse().setContentLength(bs.length);
this.getResponse().setContentType("image/*;charset=UTF-8");
this.getResponse().setHeader("Content-Disposition", "filename="+ URLEncoder.encode(image.get("map_code")+"", "utf-8"));
this.getResponse().getOutputStream().write(bs);
}else{
}
this.renderNull();
}
/**
* 带参数传递测试上传图片
* @throws IOException
*/
public void uploadMap() throws IOException{
UploadFile uploadFile = getFile();
String mapType = this.getPara("mapType");
String mapCode= "";//初始化图片id
//取到文件保存
if(uploadFile!=null){
byte[] bs = FileHelper.toBytes(uploadFile.getFile());
String nameImg = uploadFile.getFileName();
String[] strs=nameImg.split("\\.");
mapCode = strs[0];
//检测数据库里图片的id是否已经存在
MapService imgSer = new MapService();
EbMapFile img = imgSer.findMapFile(mapCode);
if(img == null){
img = new EbMapFile();
//建立文件路径
File dirFile = new File(MCubeAppConfig.getInstance().getImagUrl());
if(!dirFile.exists()){
dirFile.mkdirs();
}
File desFile = new File(dirFile, mapCode);//定义文件名
boolean success = false;
try {
success = uploadFile.getFile().renameTo(desFile);//jfinal批量上传文件时重命名
} catch (Exception e) {
e.printStackTrace();
}
setAttr("error", success ? 0 : 1);
if(success){
img.set("map_id", "EB_MAP_seq.nextval");
img.set("map_code", mapCode);
// img.set("map_type",1);
img.set("map_type", new BigDecimal(mapType));//图片类型,1为中心地图
img.set("map_CONTEN",bs);
String mapUrl = MCubeAppConfig.getInstance().baseUrl +"/img/iconMap?imgName="+mapCode;
img.setMapUrl(mapUrl);
if(img.save()){
setAttr("message", "上传成功");
}
}else{
setAttr("message", "上传失败");
}
}
}
setAttr("imgName",mapCode);
render(new JsonRender().forIE());
}
--------------------------------------------------------------------------
图片的model类对象:
package cn.com.minstone.eBusiness.model;
import com.jfinal.plugin.activerecord.Model;
public class EbMapFile extends Model<EbMapFile>{
/**
* 展示地图文件图片表
*/
private static final long serialVersionUID = 1L;
public static final EbMapFile dao = new EbMapFile();
/**
* 数据库列名称:MAP_CODE
* */
private java.lang.String mapCode;
public java.lang.String getMapCode() {
this.mapCode = this.get("MAP_CODE");
return this.mapCode;
}
public void setMapCode(java.lang.String mapCode) {
this.mapCode = mapCode;
this.set("MAP_CODE", this.mapCode);
}
/**
* 数据库列名称:MAP_URL
* */
private java.lang.String mapUrl;
public java.lang.String getMapUrl() {
this.mapUrl = this.get("MAP_URL");
return mapUrl;
}
public void setMapUrl(java.lang.String mapUrl) {
this.mapUrl = mapUrl;
this.set("MAP_URL", this.mapUrl);
}
/**
* 数据库列名称:MAP_ID
* */
private java.math.BigDecimal mapId;
public java.math.BigDecimal getMapId() {
this.mapId = this.get("MAP_ID");
return mapId;
}
public void setMapId(java.math.BigDecimal mapId) {
this.mapId = mapId;
this.set("MAP_ID", this.mapId);
}
/**
* 数据库列名称:MAP_CONTEN
* */
private oracle.jdbc.OracleBlob mapConten;
public oracle.jdbc.OracleBlob getMapConten() {
this.mapConten = this.get("MAP_CONTEN");
return mapConten;
}
public void setMapConten(oracle.jdbc.OracleBlob mapConten) {
this.mapConten = mapConten;
this.set("MAP_CONTEN", this.mapConten);
}
/**
* 数据库列名称:MAP_TYPE
* */
private java.math.BigDecimal mapType;
public java.math.BigDecimal getMapType() {
this.mapType = this.get("MAP_TYPE");
return this.mapType;
}
public void setMapType(java.math.BigDecimal mapType) {
this.mapType = mapType;
this.set("MAP_TYPE", this.mapType);
}
}
标签:
原文地址:http://www.cnblogs.com/FWebApp/p/5563960.html