Directly send code. This is the upload code that I have been using.
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>
<% @ Page import = "Java. util. *, java. Io. *" %>
<% @ Page import = "Java. Text. simpledateformat" %>
<% @ Page import = "org. Apache. commons. fileupload. *" %>
<% @ Page import = "org. Apache. commons. fileupload. disk. *" %>
<% @ Page import = "org. Apache. commons. fileupload. servlet. *" %>
<% @ Page import = "org. JSON. Simple. *" %>
<%
// Obtain the action path and Image Storage path
// String actionurl = request. getparameter ("actionurl ");
String imageurl = request. getparameter ("Foldername ");
// System. Out. println ("the directory path of the file is" + imageurl );
String resultmsg = NULL;
// Path of the file storage directory
String savepath = pagecontext. getservletcontext (). getrealpath ("/") + imageurl + "/";
String url = This. getclass (). getclassloader (). getresource ("/")
. Tostring (). Replace ("WEB-INF/classes/", ""). Replace ("file :/","");
// System. Out. println ("the directory path of the file is" + savepath );
// File Save directory URL
String saveurl = request. getcontextpath () + "/" + imageurl + "/";
// File name
String newfilename = "";
// File Size
Long filesize = 0;
// Define the file extension that can be uploaded
String [] filetypes = new string [] {"GIF", "jpg", "Jpeg", "PNG", "BMP", "GIF", "jpg ", "Jpeg", "PNG", "BMP "};
// Maximum File Size
Long maxsize = 1000000;
Response. setcontenttype ("text/html; charset = UTF-8 ");
If (! Servletfileupload. ismultipartcontent (request )){
Resultmsg = "select a file! ";
} Else {
// Check the Directory
File uploaddir = new file (savepath );
If (! Uploaddir. isdirectory ()){
Out. println (savepath );
Resultmsg = "the upload directory does not exist! ";
Uploaddir. mkdir ();
} Else {
If (! Uploaddir. canwrite ()){
Resultmsg = "the upload directory has no read/write permission! ";
} Else {
// Create a folder
Simpledateformat SDF = new simpledateformat ("yyyymmdd ");
String ymd = SDF. Format (new date ());
Savepath + = ymd + "/";
Saveurl + = ymd + "/";
File dirfile = new file (savepath );
If (! Dirfile. exists ()){
Dirfile. mkdirs ();
}
Fileitemfactory factory = new diskfileitemfactory ();
Servletfileupload upload = new servletfileupload (factory );
Upload. setheaderencoding ("UTF-8 ");
List items = upload. parserequest (request );
Iterator itr = items. iterator ();
While (itr. hasnext ()){
Fileitem item = (fileitem) itr. Next ();
String filename = item. getname ();
Filesize = item. getsize ();
If (! Item. isformfield ()){
// Check the file size
If (item. getsize ()> maxsize ){
Resultmsg = "the size of the uploaded file exceeds the limit! ";
} Else {
// Check the extension
String fileext = filename. substring (filename. lastindexof (".") + 1). tolowercase ();
If (! Arrays. <string> aslist (filetypes). Contains (fileext )){
Resultmsg = "the File Upload extension is not allowed! ";
} Else {
Simpledateformat df = new simpledateformat ("yyyymmddhhmmss ");
Newfilename = DF. Format (new date () + "_" + new random (). nextint (1000) + "." + fileext;
Try {
File uploadedfile = new file (savepath, newfilename );
Item. Write (uploadedfile );
} Catch (exception e ){
Resultmsg = "An error occurred while uploading the file! ";
}
}
}
}
}
}
}
}
String uploadurl = saveurl + newfilename;
System. Out. Print (newfilename );
If (uploadurl. indexof (".") <0 ){
Uploadurl = "";
}
Try {
If (resultmsg! = NULL ){
Response. getwriter (). Print ("<SCRIPT> alert (\" "+ resultmsg +" \ "); </SCRIPT> ");
}
String resultstr = "{\" ID \ ": \" "+ 1000 +" \ ", \" FILENAME \ ": \" "+ newfilename + "\", \ "attachmentpath \": \ "" + uploadurl + "\", \ "attachmentsize \": \ "" + filesize + "\"}";
Response. getwriter (). Print (resultstr );
} Catch (ioexception e ){
E. printstacktrace ();
}
%>
Because the dwz framework is used in combination, the data format is JSON, so some prompts in it should be changed to this, but it should not affect the trial, as long as a slight adjustment is needed.