springmvc+uploadify3.2.1 Complete code example, java,jsp

Source: Internet
Author: User

    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.