Simple implementation of Ajax upload and preview,
1. upload an image asynchronously and preview the most simple ajax method.
Html:
<! DOCTYPE html>
Servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); String path = request.getServletContext().getRealPath("/upload"); String name = null; try { List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if(item.isFormField()){ System.out.println(item.getFieldName() + ": " + item.getString()); } else { name = item.getName(); item.write(new File(path,name)); } } PrintWriter out = response.getWriter(); out.print("{"); out.print("url:\"" + name +"\""); out.print("}"); } catch (Exception e) { e.printStackTrace(); } }
2. Here An ajaxupload. js will be used, which is a lot more online.
The above is all the simple implementation of Ajax Image Upload and preview provided by xiaobian. I hope it will be helpful to you and provide more support for helping customers ~