JS file:
var uploaddropzone = new Dropzone ("#uploaddropzone", {
Url:ctx + "/slider/fileuploadcontinue",
Acceptedfiles: "",
Maxfiles:1,
Autodiscover:false,
Addremovelinks:true,
Dictremovefile: ' x ',
Dictdefaultmessage: ' Drop file here or click to upload ',
Dictinvalidfiletype: "You cannot upload the type file, the file type can only be",
THUMBNAILHEIGHT:100,
THUMBNAILWIDTH:256,
Customupload:function (FILES,XHR) {//rewrite Dropzone.js source code, the main function is to intercept when dropzone send file request, in another way.
var file=files[0];
Fileuploadcheckajax (FILE,XHR);//file need to upload files, xhr listen to Events
},
Init:function () {
This.on ("Addedfile", function (file) {
$ ("#uploadChoose"). attr ("Disabled", true);
});
This.on ("Success", function (file) {
$ ("#fileResource"). HTML (file.xhr.responseText);
$ ("#uploadChoose"). attr ("disabled", false);
});
This.on ("Complete", function (data) {
if (Data.status = = "Error") {
Promptpop ("Warning", "Upload failed");
}
});
}
});
function Fileuploadcheckajax (FILE,XHR) {
Xhr.onreadystatechange = function () {//Upload status monitor Xhr.responsetext return value, provided by background
Console.log (xhr.readystate + "-" + Xhr.status + "-" + xhr.responsetext);
if (xhr.readystate = = 4 && xhr.status = = 200) {
if (xhr.responsetext== "error") {
Alert (' failure ');
}else{
Alert (' success ');
}
}
};
var load=0;//upload Progress Percentage
var param = "Filename=" + file.name + "&filesize=" + file.size + "&filetype="
+ File.type + "&filelastmodified=" + file.lastmodified; File information
var xhrcheck = new XMLHttpRequest ();//Check request the second listener main listening file upload situation
Xhrcheck.onreadystatechange = function () {//Check status monitor, execute send upload request after success
if (Xhrcheck.status = = && Xhrcheck.readystate = = 4) {
Load = parseint (Xhrcheck.responsetext);
Xhr.open ("POST", ctx+ "/slider/fileuploadcontinue?" + param, true);//upload file Method implementation
Xhr.send (File.slice (Load, file.size, File.type));//Get uploaded data by File.slice method and upload (focus)
xhr.upload.uploadprogress = function (e) {//Upload progress monitoring
This.emit ("uploadprogress", File, File.upload.bytesSent)
};
}
};
Xhrcheck.open ("POST", ctx+ "/slider/fileuploadcheck?" + param, true);//Listener request address, this method mainly returns the size of the uploaded file
Xhrcheck.send ();//Send Check Request
}
Dropzone.js Modification:
Dropzone.prototype.uploadFiles method,
In return Xhr.send (FormData); Before adding the following code:
if (this.options.customupload!=null) {
return This.options.customUpload (FILES,XHR);
}//if the Customupload method is defined, the Customupload method is returned, otherwise it is implemented as the original method. This is why you should add Customupload to JS.
Java:
Fileuploadcheck method: To upload the file name, type, size and last modified time to determine whether the file is the same file, the path specified in the temporary storage is retrieved and returns the size of the file has been uploaded.
@ResponseBody
@RequestMapping (value= "/fileuploadcheck", method = Requestmethod.post)
Public Long Fileuploadcheck (Filevo Filevo) throws Exception {
String Syspath=this.getfileuploadpath ("upload.file.tmp");
The file uniqueness--md5 hash value is determined by the name, size, type, lastmodified four properties.
String FileID = EncoderByMd5 (Filevo.getfilename () +filevo.getfilesize () + filevo.getfiletype () + Filevo.getfilelastmodified ());
String fileName = dateutils.getcurrentdate ("YyyyMMDD") +fileid +filevo.getfilename (). substring (Filevo.getfilename () . LastIndexOf ("."));
File dir = new file (Syspath);
To determine whether a folder exists or not exists then create
if (!dir.exists ()) {
Dir.mkdir ();
}
File file =new file (Syspath+file.separator + fileName);
if (!file.exists ()) {
return 0l;
} else {
return File.length ();
}
}
EncoderByMd5 method: MD5 encoding of the specified string
public string EncoderByMd5 (String str) {
try {
Generate a MD5 cryptographic calculation summary
MessageDigest MD = messagedigest.getinstance ("MD5");
Calculate the MD5 function
Md.update (Str.getbytes ());
Digest () finally determines the return MD5 hash value with a return value of 8 as a string. Because the MD5 hash value is a 16-bit hex value, it's actually a 8-bit character.
The BigInteger function converts a 8-bit string to a 16-bit hex value, represented by a string, and a hash value in the form of a string.
return new BigInteger (1, Md.digest ()). toString (16);
} catch (Exception e) {
E.printstacktrace ();
return str;
}
}
Fileuploadcontinue: Method uploads a file to a temporary path in the form of a replication,
@ResponseBody
@RequestMapping (value= "/fileuploadcontinue", method = Requestmethod.post)
Public String fileuploadcontinue (modelmap map,httpservletrequest Request,filevo Filevo) throws Exception {
ServletInputStream is = Request.getinputstream ();//input stream, provided by the front-end,
String Syspath=this.getfileuploadpath ("upload.file.tmp");//File temporary storage path for system configuration
The file uniqueness--md5 hash value is determined by the name, size, type, lastmodified four properties.
String FileID = EncoderByMd5 (Filevo.getfilename () +filevo.getfilesize () + filevo.getfiletype () + Filevo.getfilelastmodified ());
String fileName = dateutils.getcurrentdate ("YyyyMMDD") + FileID +filevo.getfilename (). substring (Filevo.getfilename () . LastIndexOf ("."));
FileOutputStream OS = null;
File file = null;
try {
Bufferedinputstream bis = new Bufferedinputstream (IS);
Byte[] B = new byte[1024 * 1024];
File =new file (Syspath+file.separator + fileName);
if (!file.exists ()) {
File.createnewfile ();
}
OS = new FileOutputStream (file, true);//Append
int n = 0;
while ((n = bis.read (b)) > 0) {
Os.write (b, 0, N);
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Os.close ();
Is.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
InputStream newis=new FileInputStream (New File (Syspath+file.separator + fileName)); Newis a new file stream
}
Dropzone.js Rewrite breakpoint continuation function