- front page: Introducing JS and CSS
<script type= "Text/javascript" src= "${static_file_path}/js/jquery/jquery-1.10.js${static_file_version}" > </script> <link type= "text/css" rel= "stylesheet" href= "${static_file_path}/js/jplug/uploadify/uploadify.css${ Static_file_version} "></link>
<script type= "Text/javascript" src= "${static_file_path}/js/jplug/uploadify/jquery.uploadify.min.js${ Static_file_version} "></SCRIPT>:
2: script content:
If you do not understand the following parameters, please refer to the official documentation, address: http://www.uploadify.com/documentation/
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ (function () {
$ (' #file_upload '). Uploadify ({
' swf ': ' ${static_file_path}/js/jplug/uploadify/uploadify.swf ',
' Uploader ': ' ${context_path}/shop/upload.html ',
' Height ': 25,
' whith ': 120,
' Auto ': false,
' Filedataname ': ' File ',
' ButtonText ': ' Select Picture ... ',
' filetypeexts ': ' *.gif; *.jpg; *.png ',
' Multi ': false,
' method ': ' Post ',
' Debug ': true,
' Onuploadsuccess ': function (file, data, response) {
Alert (' The file ' + File.name + ' is successfully uploaded with a response of ' + response + ': ' + data ');
},
' Onuploaderror ': function (file, ErrorCode, ErrorMsg, errorstring) {
Alert (' The file ' + File.name + ' could not being uploaded: ' + errorstring);
}
});
});
});
</script>
2:<body> Content:
<body >
<input id= "file_upload" type= "file" name= "file"/>
<a href= "javascript:$ (' #file_upload '). Uploadify (' upload ', ' * ') ' > upload file </a> | <a href= "javascript:$ (' #file_upload '). Uploadify (' Stop ') > Stop uploading!</a>
</body>
3: Backend Controller:
@RequestMapping (value = "/shop/upload", Method=requestmethod.post)
@ResponseBody
Public Object uploadhandlerforuploadify (String pichref,httpservletrequest request)
Throws Exception {
Integer UserID = 0;
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Multipartfile multipartfile = Multipartrequest.getfile ("Filedata");
/** write a file before you read the original high-width **/
byte[] bytes = Multipartfile.getbytes ();
InputStream is = new Bytearrayinputstream (bytes);
int width = 0; Original picture width
int height = 0; Original picture High
try {
BufferedImage bufimg = Imageio.read (IS);
Only pictures get high width
if (bufimg! = null) {
width = Bufimg.getwidth ();
Height = bufimg.getheight ();
}
Is.close ();
} catch (Exception e) {
E.printstacktrace ();
Loger.error (E.getmessage ());
Is.close ();
throw new Exception ("an exception occurred when the Uploadify upload image reads high width!");
}
/** a complete file save path Plus file **/
String originalfilename = Multipartfile.getoriginalfilename (); Full file name
String suffix = stringutil.substringafter (OriginalFilename, "."); Suffix
FileName transcoding
filename = base64.stringtobase64 (filename);
filename = stringutil.join (filename, suffix);
UPLOADFILEPATHVO UploadFile = Uploadfilepathutil.initfileupload (UserID, "test", suffix, width, height);
File File = new file (Uploadfile.getrealpath ());
Multipartfile.transferto (file);
return uploadfile;
}
4: Custom upload Path tool class:
/**
* Get image upload path (processing aspect)
*
* @return
*/
public static UPLOADFILEPATHVO Initfileupload (Integer UserID, string imageType, string suffix, int width, int height) {
String Randomkey = randomutil.getrandomstring (6);
Date date = new Date ();
String datestr = new SimpleDateFormat ("YyyyMMdd"). Format (date);
String timestr = new SimpleDateFormat ("Hhmmsssss"). Format (date);
int hashcode = Math.Abs (Userid.hashcode ()% 256);
String RelativePath = Stringutil.join (ImageType, "/", Hashcode, "/", UserID, "/", Datestr, "/");
String Realpath = Stringutil.join (Constants.upload_realpath, "/", relativepath);
File Logosavefile = new file (Realpath);
if (!logosavefile.exists ()) {
Logosavefile.mkdirs ();
}
Picture file name: Timestamp + random string + high width
String fileName = Stringutil.join (Timestr, Randomkey, ' _ ', height, ' _ ', Width, '. ', suffix);
UPLOADFILEPATHVO uploadfile = new UPLOADFILEPATHVO ();
Uploadfile.setrelativepath (Stringutil.join (RelativePath, fileName));
Uploadfile.setrealpath (Stringutil.join (Realpath, fileName));
return uploadfile;
}
5:UPLOADFILEPATHVO class:
Public String Realpath;
Public String RelativePath;
private int imgheight; Upload pictures of high
private int imgwidth; Wide
springmvc+uploadify3.2.1 Complete code example, java,jsp