Implementing the MVC pattern based on the jfinal framework
The first is the upload interface:
Front-end code is based on asynchronous uploads of bootstrap and plug-ins 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" > Upload image </button>
<script>
function Ajaxfileupload () {
if (!checkimageext ("map")) {
Return
}
var map = $ ("#map"). Val ();
if (!validate_required (map)) {
Alert ("Please select a picture to upload!");
Return
}
$.ajaxfileupload
(
{
URL: ' ${base}/img/uploadmap ',//server-side request address for file upload
Type: ' Post ',
Data: {maptype: ' 2 '},//This parameter is very rigorous, a wrong quote is not
Secureuri:false,//general set to False
Fileelementid: ' map ',//File upload Space id attribute <input type= "file" id= "file" name= "file"/>
DataType: ' json ',//return value type is generally set to JSON
Success:function (data, status)//Server Success Response handler function
{
Mapcode = Data.imgname;
$ ("#img1"). attr ("src", "${base}/img/iconmap?imgname=" +data.imgname);
if (typeof (Data.error)! = ' undefined ') {
if (data.error! = ") {
Alert ("Upload failed!");
} else {
alert (data.message);
}
}
},
Error:function (data, status, E)//server response failure handler function
{
Alert (e);
}
}
);
}
Detecting file formats
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";//Verify the format first
if (Extname.indexof (extendname.touppercase ()) = =-1) {
Alert ("* You can only enter files in the" + extname + "format);
return false;
} else {
return true;
}
}
</script>
---------------------------------------------------------------------------------------------
Controller: Filectrl.class code is as follows:
/**
* Get pictures based on image ID
* Note: Calling this method URL returns the HTML directly as a picture
* */
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 ();
}
/**
* Upload picture with parameter passing test
* @throws IOException
*/
public void Uploadmap () throws ioexception{
UploadFile UploadFile = GetFile ();
String Maptype = This.getpara ("Maptype");
String mapcode= "";//Initialize picture ID
Take to File Save
if (uploadfile!=null) {
byte[] bs = filehelper.tobytes (Uploadfile.getfile ());
String nameimg = Uploadfile.getfilename ();
String[] Strs=nameimg.split ("\ \");
Mapcode = Strs[0];
Detects if the ID of the image in the database already exists
Mapservice imgser = new Mapservice ();
Ebmapfile img = imgser.findmapfile (mapcode);
if (img = = null) {
img = new Ebmapfile ();
Establish file path
File Dirfile = new file (Mcubeappconfig.getinstance (). Getimagurl ());
if (!dirfile.exists ()) {
Dirfile.mkdirs ();
}
File Desfile = new file (Dirfile, mapcode);//define file name
Boolean success = false;
try {
Success = Uploadfile.getfile (). Renameto (desfile)//jfinal rename when uploading files in bulk
} 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));//Picture type, 1 for center map
Img.set ("Map_conten", BS);
String MapURL = Mcubeappconfig.getinstance (). BaseUrl + "/img/iconmap?imgname=" +mapcode;
Img.setmapurl (MapURL);
if (Img.save ()) {
SetAttr ("message", "upload success");
}
}else{
SetAttr ("message", "Upload failed");
}
}
}
SetAttr ("Imgname", Mapcode);
Render (New Jsonrender (). Forie ());
}
--------------------------------------------------------------------------
The model class object for the picture:
Package Cn.com.minstone.eBusiness.model;
Import Com.jfinal.plugin.activerecord.Model;
public class Ebmapfile extends model<ebmapfile>{
/**
* Display map file picture table
*/
Private static final long serialversionuid = 1L;
public static final Ebmapfile DAO = new Ebmapfile ();
/**
* Database column Name: 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);
}
/**
* Database column Name: 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);
}
/**
* Database column Name: 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);
}
/**
* Database column Name: 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);
}
/**
* Database column Name: 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);
}
}
General form of file upload based on jifnal framework (IMG)