Recently, the project needs to be uploaded a file. After completing the work, share it and take notes as well.
The upload function is implemented in the background and Java, and the front-end is mainly implemented in JS's Ajax. The background also adds temporary files to be deleted regularly.
The effect is as shown in the picture
First of all, the main class of the upload function, the following is the code
package util.upload;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Iterator;import java.util.List;import java.util.UUID;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;public class UploadServlet extends HttpServlet { private static final long serialVersionUID = -3100028422371321159L; private boolean isAllowed; private String upFileName; //Define array with legal suffix name private String[] allowedExtName=new String[] {"zip","rar",//Compress file "txt","doc","wps","docx","java",//Text"xls","xlsx",//Table"ppt","pptx",//Slide"pdf",//Play"f"jpg","bmp","gif","png"//Picture}; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Set the encoding format to utf-8 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //Get session, save progress and upload results, upload starts as nok, when Ok means uploading is completed HttpSession session=request.getSession(); session.setAttribute("result", "nok"); session.setAttribute("error", ""); String error=""; upFileName=""; isAllowed=false; //Set a maximum value for the uploaded file, which must not exceed 100MB int maxSize=100*1024*1024; //Create factory objects and file upload objects DiskFileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload(factory); //Create upload listener and set listener UploadListener listener=new UploadListener(); session.setAttribute("LISTENER", listener); upload.setProgressListener(listener); //Upload path String path = request.getSession().getServletContext().getRealPath("/upload"); String requestPath = request.getSession().getServletContext().getContextPath()+"/upload"; File dirFile =new File(path); //System.out.println(request.getSession().getServletContext().getContextPath()); //If the folder does not exist, create if (!dirFile .exists() && !dirFile .isDirectory()) { dirFile .mkdir(); } //Create a folder based on the date and save it to the folder with the corresponding date Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); String subDirName=sdf.format(date); File subDirFile=new File(path+"/"+subDirName); if (!subDirFile .exists() && !subDirFile .isDirectory()) { subDirFile .mkdir(); } try { // parse upload request List<FileItem> items=upload.parseRequest(request); Iterator<FileItem> itr=items.iterator(); while(itr.hasNext()){ FileItem item=(FileItem)itr.next(); //Determine whether it is a file domain if(!item.isFormField()){ if(item.getName()!=null&&!item.getName().equals("")){ //Get the uploaded file size and file name long upFileSize=item.getSize(); String fileName=item.getName(); //Get the file suffix name String[] splitName=fileName.split("//."); String extName=splitName[splitName.length-1]; //Check the file suffix name for(String allowed: allowedExtName) { if(allowed.equalsIgnoreCase(extName)) { isAllowed=true; } } if(!isAllowed){ error="The upload file format is illegal! "; break; } if(upFileSize>maxSize){ error="The file you upload is too large, please select a file that does not exceed 100MB!"; break; } //At this time the file is temporarily stored in the server's memory, constructing a temporary object File tempFile=new File(makeFileName(fileName)); //Specify the directory and file name of the file upload server File file=new File(path+"/"+subDirName+"/",tempFile.getName()); item.write(file); //The first method of writing file is upFileName=requestPath+"/"+subDirName+"/"+tempFile.getName(); if(upFileName.equals("")){ error="No selection to upload file! "; } System.out.println(upFileName); /*//Construct the second method of writing file of input stream reading file InputStream is=item.getInputStream(); int length=0; byte[] by=new byte[1024]; FileOutputStream fos=new FileOutputStream(file); while((length=is.read(by))!=-1){ fos.write(by, 0, length); //Thread.sleep(10); } fos.close(); //Thread.sleep(1000); */ }else{ error="No selection to upload file! "; } } } } catch (Exception e) { e.printStackTrace(); error="Error occurred when uploading the file:"+e.getMessage(); } if(!error.equals("")){ System.out.println(error); session.setAttribute("error", error); }else{ session.setAttribute("result", "OK"); session.setAttribute("filename",upFileName); } } /** * To prevent file overwriting, a unique file name must be generated for uploading the file* @param filename Original file name* @return The generated unique file name*/ private String makeFileName(String filename){ return UUID.randomUUID().toString() + "_" + filename; } } It is necessary to introduce commons-fileupload-1.3.1.jar and commons-io-2.4.jar
During the upload process, we need to obtain information such as upload progress in real time. The introduced library added a ProgressListener interface for us. We will write another class to implement this interface, and add this interface to the above class.
//Create factory objects and file upload objects DiskFileItemFactory factory=new DiskFileItemFactory(); ServletFileUpload upload=new ServletFileUpload(factory); //Create upload listener and set listener UploadListener listener=new UploadListener(); session.setAttribute("LISTENER", listener); upload.setProgressListener(listener);Below is the specific implementation code of this listening class
package util.upload;import org.apache.commons.fileupload.ProgressListener;public class UploadListener implements ProgressListener{ private volatile long bytesRead = 0L,//number of uploaded bytes contentLength = 0L,//total number of bytes item = 0L; public UploadListener() { super(); } @Override public void update(long aBytesRead, long aContentLength, int anItem) { bytesRead = aBytesRead; contentLength = aContentLength; item = anItem; } public long getBytesRead() { return bytesRead; } public long getContentLength() { return contentLength; } public long getItem() { return item; }}Now you can get information such as upload progress, but you still need a servlet to return to the front end. The following implementation
package util.upload;import java.io.IOException;import java.io.PrintWriter;import java.util.HashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.commons.fileupload.ProgressListener;import com.google.gson.Gson;/** Get upload progress, upload path, error, upload result and other information*/public class GetProgressServlet extends HttpServlet{ private static final long serialVersionUID = -3596466520775012991L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); UploadListener listener= null; HttpSession session = request.getSession(); String error=(String) session.getAttribute("error"); String result= (String) session.getAttribute("result"); String fileName=(String) session.getAttribute("filename"); PrintWriter out = response.getWriter(); long bytesRead = 0,contentLength = 0; if (session != null) { listener = (UploadListener)session.getAttribute("LISTENER"); if (listener == null) { return; } else { bytesRead = listener.getBytesRead();//The number of uploaded bytes contentLength = listener.getContentLength();//Total bytes} //The return format defined by yourself is String rp=bytesRead+"," +contentLength+"," +error+"," +result+"," +fileName; //System.out.println(rp); out.print(rp); /* //Return json format data Map<String,Object> map=new HashMap<String,Object>(); map.put("bytesRead", bytesRead); map.put("contentLength", contentLength); map.put("error", error); map.put("result", result); map.put("fileName", fileName); Gson gson=new Gson(); String json=gson.toJson(map); out.print(json);*/ out.flush(); out.close(); } }}The function code uploaded in the background has been written. The following is the front-end to implement the upload, first of all, html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script type="text/javascript" src="js/upfile.js" charset="utf-8"></script> <link rel="stylesheet" type="text/css" href="css/upfile.css"> </head> <body > <a href="javascript:addOne()">Add</a> <div id="target"> <input type="file" id="file" name="file" onchange="addfile(event)" multiple/> </div> <span id="test">0</span> </body> </html>
The interface is relatively simple, just add a tag, and the input uploaded is hidden.
Display of upload progress of css file main rendering
#file { display: none; } .pro{ width:500px; } .pborder { position: relative; width: 500px; /* Width*/ border: 1px solid #B1D632; padding: 1px; } .drawpro { width: 0px; display: block; position: relative; background: #B1D632; color: #333333; height: 20px; /* Height*/ line-height: 20px; /* It must be consistent with the height before the text can be vertically centered*/ } .pspan { position: absolute; width: 500px; text-align: center; font-weight: bold; }Next is the front-end focus, js file
//The htmlvar upfile_html that displays upload information is '<div><div>' + '<span>0%</span></div></div><span name="path"></span><img src="common/upload/images/del.png" style="float:right" name="del" onclick=abortUpload(this)>';var targetDIV_id = "target";//The IDvar of the target div for uploading the file httpXML = null;//The XMLHttpRequest object that sends the upload request var httpProgress = null;//XMLHttpRequest object that sends request progress information var oldFileList = new Array();//Save the list of existing attachment information var uplist = new Array();//Save the list of uploaded files var f_input;//Upload the input object that uploads the file var flag = true;//Can the next file flag var uurl = "Upload";//Request for uploading the file urlvar gurl = "getProgress";//Upload the file urlvar cancelFlag = 0;//Cancel flag var timer, waittimer;//Timer var nowID = 0;//Idvar ID of the file being uploaded = 0;//id of the last file in the queue/** * File object*/function UploadFile(id, file) { this.id = id; this.file = file; this.state = 0; this.path = "";}/** * Initialization method*/window.onload = function init() { f_input = document.getElementById("file"); var tdiv = document.getElementById(targetDIV_id); var oldspan = tdiv.getElementsByTagName("SPAN"); for ( var i = 0; i < oldspan.length; i++) { oldFileList.push(oldspan[i].getAttribute("name")); }}/** * Select a file to upload */function addOne() { f_input.value = null; f_input.click();}/** * After selecting the file, add the file object to the queue and start uploading* */function addfile(evt) { var f = f_input.files[0]; if (f != undefined) { var uf = new UploadFile(ID, f); uplist.push(uf); var div = document.createElement("DIV"); div.setAttribute("id", "pro" + (ID)); div.setAttribute("class", "pro"); div.innerHTML = upfile_html; var targetDiv = document.getElementById(targetDIV_id); targetDiv.appendChild(div); div.getElementsByTagName("SPAN")[1].innerHTML = "File name:" + uplist[ID].file.name; waittimer = setInterval("upload()", 1000); ID++; }}/** * Upload the file in the queue*/function upload() { if (flag == true) { if (uplist.length > 0) { var uf; for ( var i = 0; i < uplist.length; i++) { if (uplist[i].state == 0) { uf = uplist[i]; uplist[i].state = 1; break; } } if (uf != undefined & uf != null) { flag = false; if (window.XMLHttpRequest) { httpUP = new XMLHttpRequest(); } else if (window.ActiveXObject) { httpUP = new ActiveXObject("Microsoft.XMLHTTP"); } var formData = new FormData(); formData.append("file", uf.file); httpUP.open("POST", uurl, true); httpUP.upload.addEventListener('progress', uploadProgress, false); httpUP.send(formData); nowID = uf.id; timer = setInterval("getP()", 50); } } }}/** * Get information such as upload progress*/function getP() { if (window.XMLHttpRequest) { httpProgress = new XMLHttpRequest(); } else if (window.ActiveXObject) { httpProgress = new ActiveXObject("Microsoft.XMLHTTP"); } httpProgress.onreadystatechange = onProgress; httpProgress.open("post", gurl, true); httpProgress.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); httpProgress.send("&timeStamp=" + (new Date()).getTime());}/** * Process the returned upload information and display it to the interface*/function onProgress() { if (httpProgress.readyState == 4 && httpProgress.status == 200) { result = httpProgress.responseText; var result = result.replace(/(^/s*)|(/s*$)/g, ""); var res = result.split(","); var now = parseInt(res[0]); var all = parseInt(res[1]); var err = res[2]; var state = res[3]; var path = res[4]; var per = (now / all * 100).toFixed(2); var prodiv = document.getElementById("pro" + nowID); if (prodiv != null & prodiv != undefined) { if (err != "" & err != null & err.length > 0) { window.clearInterval(timer); if (cancelFlag == 1) { err = "Upload Termination"; cancelFlag = 0; } prodiv.getElementsByTagName("DIV")[0].style.display = "none"; prodiv.getElementsByTagName("SPAN")[1].innerHTML = err; httpUP.abort(); flag = true; uplist[nowID].state = 3; return; } if (state == "OK") { prodiv.getElementsByTagName("DIV")[0].style.display = "none"; var tmpf = uplist[nowID].file; prodiv.getElementsByTagName("SPAN")[1].innerHTML = "File name:" + tmpf.name; window.clearInterval(timer); flag = true; uplist[nowID].state = 2; uplist[nowID].path = path; return; } prodiv.getElementsByTagName("DIV")[1].style.width = per * 5 + "px"; prodiv.getElementsByTagName("SPAN")[0].innerHTML = per + "%"; } }}/** * Method to cancel upload*/function abortUpload(obj) { var idStr = obj.parentNode.id; var id = idStr.slice(3); if (uplist[id].state == 1) { httpUP.abort(); flag = true; cancelFlag = 1; } else { uplist[id].state = 3; } document.getElementById(idStr).remove();}/** * Get the path to upload file* @returns Formatted string*/function getFileListStr() { var str = ""; if (oldFileList.length > 0) { for ( var i = 0; i < oldFileList.length; i++) { if (oldFileList[i] != null & oldFileList[i] != "" & oldFileList[i] != undefined) { str = str + oldFileList[i] + ","; } } } for ( var i = 0; i < uplist.length; i++) { var f = uplist[i]; if (f.state == 2) { str = str + f.path + ","; } } return str;}/** * Remove the old attachment that was already there when the modification* */function removeOld(btn) { var num = btn.getAttribute("name"); oldFileList[num - 1] = null; btn.parentNode.remove();} function uploadProgress(e) { if (e.lengthComputable) { var iBytesUploaded = e.loaded; var iBytesTotal = e.total; document.getElementById("test").innerHTML=iBytesUploaded+"/"+iBytesTotal; } } Use ajax to send upload files to get upload progress, results and other information.
The html5 file API used is so ie9 or above must be compatible. Firefox has a problem. Ajax requests do not return immediately. The same result will be returned until all ajax requests are sent, which will result in the upload progress not being displayed. However, you can also use the html5 file api to get it, which has a little code added. The value in the test div below the page is the progress obtained on the front end.
All uploaded files have been implemented, and then the uploaded temporary files are processed. Because the uuid named files are used, many files will be generated, and the useless ones need to be processed regularly. Using ServletContextListener:
There is a ServletContextListener interface in the Servlet API, which can listen for the life cycle of the ServletContext object, which is actually the life cycle of the web application.
When the Servlet container starts or terminates a web application, the ServletContextEvent event is triggered, which is handled by the ServletContextListener. Two methods are defined in the ServletContextListener interface to handle the ServletContextEvent event.
Using its features, the function of deleting temporary files regularly is realized. The code is as follows:
package util.upload;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.Properties;import java.util.Timer;import java.util.TimerTask;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;/** * Time Listener* * */ public class TempFileListener implements ServletContextListener { private Timer timer; private SystemTaskTest systemTask; private static String every_time_run; static { Properties prop = new Properties(); InputStream inStrem = TempFileManager.class.getClassLoader() .getResourceAsStream("tempfile.properties"); try { prop.load(inStrem); System.out.println(inStrem); every_time_run = prop.getProperty("every_time_run"); } catch (IOException e) { e.printStackTrace(); } finally { try { inStrem.close(); } catch (IOException e) { e.printStackTrace(); } } } // Listener initial method public void contextInitialized(ServletContextEvent scce) { timer = new Timer(); systemTask = new SystemTaskTest(sce.getServletContext() .getRealPath("/"), sce.getServletContext()); try { System.out.println("Timer started"); // The listener gets the root directory of the website String path = scce.getServletContext().getRealPath("/"); Long time = Long.parseLong(every_time_run) * 1000;// The time for loop execution System.out.println("time" + time); // The first parameter is the code to be run, the second parameter is when it starts running, and the third parameter is how often it is running. Repeat execution of timer.schedule(systemTask, 10000, time); System.out.println("Task Schedule has been added"); } catch (Exception e) { e.printStackTrace(); } } public void contextDestroyed(ServletContextEvent scaling) { try { timer.cancel(); } catch (Exception e) { } } } /** * Time Tasker* */ class SystemTaskTest extends TimerTask { private ServletContext context; private String path; public SystemTaskTest(String path, ServletContext context) { this.path = path; this.context = context; } /** * Put the task to be executed regularly in run*/ public void run() { TempFileManager etf; try { System.out.println("Start the task!"); // Code to be executed System.out.println(new Date().toLocaleString()); etf = new TempFileManager(path); etf.run(); System.out.println("Specify the task execution is completed!"); } catch (Exception e) { e.printStackTrace(); } } }The above is just a listener, which is responsible for calling the method to delete temporary files regularly. The specific implementation is the following class
package util.upload;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.Properties;/** * Delete the file on the server* */ public class TempFileManager implements Runnable { private String path;//path private static String RETENTION_TIME = "1440";// The time for file storage is one day static { Properties prop = new Properties(); InputStream inStrem = TempFileManager.class.getClassLoader() .getResourceAsStream("execl.properties"); try { prop.load(inStrem); RETENTION_TIME = prop.getProperty("file_retention_time"); } catch (IOException e) { e.printStackTrace(); } finally { try { inStrem.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Constructor. Initialization parameters * @param path */ public TempFileManager(String path) { this.path = path; } /** * Put the code that the thread wants to execute in run()*/ public void run() { System.out.println("File management start======================================; path; System.out.println("File management path====" + path); File file = new File(path); deletefiles(file); } /** * Batch delete files* * @param folder */ public void deletefiles(File folder) { if(folder.isDirectory()){ File[] files = folder.listFiles(); if(files.length<=0){ if(!folder.getAbsolutePath().equalsIgnoreCase(path)){ if(canDeleteFile(folder)){ if (folder.delete()) { System.out.println("Folder" + folder.getName() + "Delete Successfully!"); } else { System.out.println("Folder" + folder.getName() + "Delete failed! The files in this folder may be being used"); } } } } } for (int i = 0; i < files.length; i++) { if(files[i].isDirectory()) { deletefiles(files[i]); }else{ deleteFile(files[i]); } } } } } /** * Delete file* * @param file */ private void deleteFile(File file) { try { if (file.isFile()) { // Delete files that meet the criteria if (canDeleteFile(file)) { if (file.delete()) { System.out.println("File" + file.getName() + "Delete successfully!"); } else { System.out.println("File" + file.getName() + "Delete failed! This file may be being used"); } } else { } } else { System.out.println("No file to be deleted"); } } catch (Exception e) { System.out.println("Delete file failed============"); e.printStackTrace(); } } /** * Determine whether the file can be deleted*/ private boolean canDeleteFile(File file) { Date fileDate = getfileDate(file); Date date = new Date(); long time = (date.getTime() - fileDate.getTime()) / 1000 / 60 - Integer.parseInt(RETENTION_TIME);// The minutes between the current time and the file interval // System.out.println("time=="+time); if (time > 0) { return true; } else { return false; } } /** * Get the last modification time of the file* * @param file * @return */ private Date getfileDate(File file) { long modifiedTime = file.lastModified(); Date d = new Date(modifiedTime); return d; } }Determine whether the file timed out, and the folder can be deleted automatically if it timed out.
The above is all about this article, I hope it will be helpful for everyone to learn Java programming.