Plugins: Ajaxfileupload.js
Jsp:
1, the introduction of the JS
2, write an input. For example <input type= "file" id= "Id_file" name= "File" >
3, write the upload JS
<script type= "Text/javascript" >
function Ajaxfileupload () {
var obj_file = document.getElementById ("Id_file");
var filesize = 0;
FileSize = obj_file.files[0].size;
if (filesize>512000) {
Alert ("Picture cannot exceed 500kb!");
$ ("#id_file"). val= "";
}else{
$.ajaxfileupload ({
URL: ' <%=basepath%>sjtp/tempimg.do ',
Server-side request address for file uploads
Secureuri:false,//general set to False
Fileelementid: ' Id_file ',//File upload Space id attribute <input type= "file" id= "Id_file" name= "file"/>
Type: ' Post ',
DataType: ' HTML ',//return value type is generally set to JSON
Success:function (data, status)/Server Success Response handler function
{
Everyone here does not have the same business logic, do not do the example.
},
Error:function (data, status, E)//server response failure Handler
{
Alert (e);
}
})
return false;
}
}
</script>
Page Upload JS complete, now is the background
Next to my background part of the business, every day uploaded pictures are present in the folder of the day. For example, today, April 23, 2016, the folder of the picture is 20160423.
Background code:
Package Com.oceansoft.swl.controller;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.multipart.MultipartFile;
Import Org.springframework.web.multipart.MultipartHttpServletRequest;
/**
* Picture upload
* @author LL
*
* 2015-7-24 3:09:27
*/
@Controller
@RequestMapping ("SJSPTP")
public class Sjtpsccontroller {
@RequestMapping ("/sptp.do")
public void Addcategory (HttpServletRequest request, String path1,
String path2, HttpServletResponse actioncontext) {
Get Current Month Day
Date d = new Date ();
SimpleDateFormat SDF = new SimpleDateFormat ("YyyyMMdd");
String datenowstr = Sdf.format (d);
System.out.println ("Add Picture");
String Re=null;
String Sub=null;
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Multipartfile imgFile1 = Multipartrequest.getfile ("FILESPTP");//Get last filename
Multipartfile imgFile2 = multipartrequest.getfile ("file");
SYSTEM.OUT.PRINTLN ("----->" + imgfile1.getoriginalfilename ());
list<string> filetypes = new arraylist<string> ();
Filetypes.add ("JPG");
Filetypes.add ("JPEG");
Filetypes.add ("BMP");
Filetypes.add ("GIF");
Filetypes.add ("PNG");
Filetypes.add ("GIF");
String sssssstring = Imgfile1.getoriginalfilename ();
System.out.println (sssssstring);
if (null!= imgFile1) {
if (!) ( Imgfile1.getoriginalfilename () = = NULL | | "". Equals (ImgFile1
. Getoriginalfilename ())) {
String UploadFile = Request.getservletcontext (). Getrealpath ("Upload");
String UploadFile = Request.getsession (). Getservletcontext (). Getrealpath ("/")
+ "upload" + "\";
SYSTEM.OUT.PRINTLN ("--->" + path1 + "--->" + path2);
System.out.println ("Upload path---->" + uploadfile);
File file1 = This.getfile (ImgFile1, path2, path1, filetypes);
String Path = UploadFile + "/" +DATENOWSTR;
File TargetFile = new file (path);
if (!targetfile.exists ()) {
Targetfile.mkdirs ();
}
File file1 = this.getfile (imgFile1, Path, filetypes);
System.out.println ("Complete upload------>" + file1);
Re=file1.tostring ();
int I=re.lastindexof ("\");
Sub=re.substring (Re.lastindexof ("\") +1);
SYSTEM.OUT.PRINTLN ("Interception--->" +sub);
String TPBT = path + sub;
Determine if a file has a caption picture
File Targetfiletwo = new file (TPBT);
if (!targetfiletwo.exists ()) {
Targetfile.mkdirs ();
}
HDZX.SETTPBT (TPBT);
Request.getsession (). setattribute ("SSS", DATENOWSTR);
Request.getsession (). setattribute ("FileName", imgFile1);
}
Actioncontext.setcontenttype ("text/html");
PrintWriter out;
try {
out = Actioncontext.getwriter ();
Out.println ("upload/" +datenowstr+ "/" + sub);
Out.flush ();
Out.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
Private File GetFile (Multipartfile imgfile, String typeName,
Private File GetFile (Multipartfile imgfile, String brandname, List filetypes) {
String fileName = Imgfile.getoriginalfilename ();
Gets the extension of the uploaded file type, first gets the location of the., then intercepts the next location from. To the end of the file, and finally to the extension
String ext = filename.substring (Filename.lastindexof (".") + 1,
Filename.length ());
SYSTEM.OUT.PRINTLN ("Get extension---->" + ext);
lowercase conversion of an extension
ext = Ext.tolowercase ();
File file = null;
if (filetypes.contains (EXT)) {//If the extension belongs to a type that is allowed to be uploaded, create the file
File = This.creatfolder (TypeName, brandname, fileName);
File = This.creatfolder (brandname, fileName);
SYSTEM.OUT.PRINTLN ("Full Path--->" + file);
try {
Imgfile.transferto (file); Save uploaded files
catch (IllegalStateException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}
return file;
}
Private File Creatfolder (string typeName, String brandname, String
FileName) {
Private File Creatfolder (String brandname, String fileName) {
File file = null;
TypeName = Typename.replaceall ("/", ""); Remove the "/"
TypeName = Typename.replaceall ("", ""); Replace a half corner space
TypeName = Typename.replaceall ("", ""); Replace a full-width space
brandname = Brandname.replaceall ("/", ""); Remove the "/"
brandname = Brandname.replaceall ("", ""); Replace a half corner space
brandname = Brandname.replaceall ("", ""); Replace a full-width space
File Firstfolder = new file ("c:/" + typeName); First level folder
File Firstfolder = new file (brandname);
System.out.println ("File path--->" + firstfolder);
String suffix = filename.substring (filename.lastindexof ('. '));
System.out.println ("Intercepting file suffix--->" + suffix);
String prefix = system.currenttimemillis () + "";
String newfilename = prefix + suffix;
if (firstfolder.exists ()) {//If a level folder exists, detect level two folders
File Secondfolder = new file (Firstfolder, brandname);
if (secondfolder.exists ()) {//If the Level two folder also exists, create the file
File = new file (Secondfolder, fileName);
else {//if level Two folder does not exist, create level Two folder
Secondfolder.mkdir ();
File = new file (Secondfolder, fileName); After creating the level two folder, you can then build the file again
File = new file (fileName);
SYSTEM.OUT.PRINTLN ("Now path---->" + brandname);
File = new File (brandname + "\" + NewFileName);
// }
else {//If the level does not exist, create a level folder
Firstfolder.mkdirs ();
File Secondfolder = new file (brandname);
if (secondfolder.exists ()) {//If the Level two folder also exists, create the file
File = new file (Secondfolder, fileName);
else {//if level Two folder does not exist, create level Two folder
Secondfolder.mkdir ();
File = new file (Secondfolder, fileName);
SYSTEM.OUT.PRINTLN ("Now path---->" + brandname);
File = new File (brandname + "\" + NewFileName);
// }
}
return file;
}
}
Ok. End
http://download.csdn.net/detail/leilei19920808/9500342 plugin download address.