Nonsense not much said, directly to everyone to paste code, the specific code as follows:
The HTML code is as follows:
<body>
<input id= "Fileid" type= "file" accept= "Video/*;capture=camera" onchange= "Onfile"
(This) >
<input id= "btn" type= "button" value= "Submit" >
<script type= "Text/javascript" >
var xhr;
function Onfile (file) {
var fd = new FormData ();
Fd.append ("Filetoupload", document.getElementById (' Fileid '). Files[0]);
XHR = new XMLHttpRequest ();
Xhr.open ("POST", "Http://localhost:8889/TestUp/upservlet");
Xhr.setrequestheader ("Content-type", "Charset=utf-8");
Xhr.send (FD);
Xhr.onreadystatechange = ProcessResponse;
}
function ProcessResponse () {
if (xhr.readystate = = 4) {
alert ("End of upload end data stream");
if (Xhr.status = =) {
var infor = xhr.responsetext;
Alert ("server-side response =" +infor);
}
}
</script>
</body>
The Java code looks like this:
Package com.yjm.up;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.util.List;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.commons.fileupload.FileItem;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
public class Upservlet extends HttpServlet {public void DoPost (HttpServletRequest request, httpservletresponse response) Throws Servletexception, IOException {//Get uploaded file save directory, will upload the file stored in the Web-inf directory, do not allow direct access to the outside world, to ensure the security of the upload file String Savepath =
This.getservletcontext (). Getrealpath ("/web-inf/upload");
File File = new file (Savepath); SYSTEM.OUT.PRINTLN ("Test .....")
"); Determine if the saved directory for the uploaded file exists if (!file.exists () &&!file.isdirectory ()) {System.out.println (Savepath + "directory does not exist, need to be created")
Build directory File.mkdir (); }//eliminationHint String message = "";
try {///Use Apache File Upload component to process file upload steps://1, create a diskfileitemfactory factory diskfileitemfactory factory = new Diskfileitemfactory ();
2, create a file upload parser servletfileupload upload = new Servletfileupload (factory);
Solve the upload file name of Chinese garbled upload.setheaderencoding ("UTF-8"); 3, to determine whether the data submitted is uploaded form data if (! Servletfileupload.ismultipartcontent (Request)) {//Get data in traditional way return;}//4, use Servletfileupload parser to parse upload data, The parse result returns a list<fileitem> set, each fileitem corresponds to an entry for form form list<fileitem> List = upload.parserequest (Request
); for (Fileitem item:list) {//If the data in Fileitem is encapsulated in the normal input item if (Item.isformfield ()) {String name = Item.getfieldname ();
The Chinese garbled problem of the data of the ordinary input item String value = item.getstring ("UTF-8");
Value = new String (value.getbytes ("iso8859-1"), "UTF-8");
SYSTEM.OUT.PRINTLN (name + "=" + value);
else {//if Fileitem is encapsulated in upload file///Get uploaded file name, String filename = Item.getname ();
SYSTEM.OUT.PRINTLN (filename);
if (filename = = NULL | | Filename.trim (). Equals ("")) {continue} Note: The file name submitted by different browsers is notThe same, some browsers submit the file name is a path, such as://C:\a\b\1.txt, and some just simple file name, such as: 1.txt//processing to obtain the upload file filename of the path section, only keep the filename part filename = filename.
SUBSTRING (filename.lastindexof ("\") + 1);
Gets the input stream inputstream in = Item.getinputstream () of the uploaded file in item;
Create a file output stream FileOutputStream out = new FileOutputStream (Savepath + "\" + filename);
Create a buffer byte buffer[] = new byte[1024 * 1024];
Determine if the data in the input stream has been read out of the identity int len = 0; The loop reads the input stream into the buffer, (len=in.read (buffer)) >0 indicates that there is also data in inside while (len = in.read (buffer) > 0) {//
Writes the data of the buffer to the specified directory (Savepath + "\"//+ filename) using the FileOutputStream output stream out.write (buffer, 0, Len);
} out.flush ();
Turn off the input stream in.close ();
Turn off the output stream out.close ();
Deletes the temporary files generated when processing file uploads item.delete (); message = "File Upload success!" ";
}
}
} catch (Exception e) {message = File upload failed!
";
E.printstacktrace ();
} request.setattribute ("message", message);
Request.getrequestdispatcher ("/message.jsp"). Forward (request, response); }
}
The Java package used is over 1G upload
Commons-fileupload-1.2.1.jar
Commons-io-1.3.2.jar
Xhr.setrequestheader ("Content-type", "Application/octet-stream;charset=utf-8");
This cannot be added