Upload an instance with a progress bar Effect Based on the fileUpload file (mandatory ).
During file upload, if we can see a better progress bar, the idea is that the server listens to the progress in real time with the listener and saves it to the session. The client asynchronously requests the server to obtain the upload progress and perform effect rendering.
:
Server servlet:
Public class UploadServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// retrieve the progress information saved by the listener MyProgress in the session String progress = (String) req. getSession (). getAttribute ("progress"); // response resp. getWriter (). print (progress); // clear the data stored in the session // req. getSession (). removeAttribute ("progress");} @ Override protected voi D doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req. setCharacterEncoding ("UTF-8"); DiskFileItemFactory factory = new DiskFileItemFactory (); ServletFileUpload upload = new ServletFileUpload (factory); upload. setProgressListener (new MyProgressListener (req); try {List <FileItem> list = upload. parseRequest (req); for (FileItem fileItem: list) {if (fileIte M. isFormField () {// normal form} else {// Upload File String path = req. getRealPath ("uploads"); String fileName = fileItem. getName (); File file = new File (path, fileName); fileItem. write (file); System. out. println ("successfully uploaded file:" + fileName) ;}} catch (Exception e) {System. out. println ("File Upload error! "); E. printStackTrace ();}}}
Server listener:
Public class MyProgressListener implements ProgressListener {private HttpSession session; public MyProgressListener (HttpServletRequest request) {session = request. getSession () ;}@ Override public void update (long pBytesRead, long pContentLength, int pItems) {// format the data. // convert the read data from byte to M double readM = pBytesRead/1024.0/1024.0; // read data is converted from byte to M double totalM = pContentLength/1024.0/1024.0; // read double percent = readM/totalM; // format the data // read String readf = dataFormat (pBytesRead); // the total size of String totalf = dataFormat (pContentLength); // progress NumberFormat format = NumberFormat. getPercentInstance (); String progress = format. format (percent); // Save the information to the session. setAttribute ("progress", progress); // print the message to the console System. out. println ("pBytesRead ==>" + pBytesRead); System. out. println ("pContentLength =>" + pContentLength); System. out. println ("pItems ==>" + pItems); System. out. println ("readf --->" + readf); System. out. println ("totalf --->" + totalf); System. out. println ("progress --->" + progress);}/*** display of formatted read data * @ param data the unit of data to be formatted byte * @ return formatted data, if the unit is smaller than 1 MB, the unit is KB. if the unit is larger than 1 MB, the unit is MB */public String dataFormat (double data) {String formdata = ""; if (data> = 1024*1024) {// greater than or equal to 1 M formdata = Double. toString (data/1024/1024) + "M";} else if (data >=1024) {// greater than or equal to 1KB formdata = Double. toString (data/1024) + "KB";} else {// less than 1KB formdata = Double. toString (data) + "byte";} return formdata. substring (0, formdata. indexOf (". ") + 2 );}}
Client:
<Html>
Note:To prevent the page from being redirected after the upload, We can redirect the form to a hidden iframe.
The above example based on the fileUpload file upload with progress bar effect (mandatory) is all the content shared by the editor. I hope you can give us a reference and support for the house of friends.