下面我就为大家带来一篇Ajax 上传图片并预览的简单实现。现在就分享给大家,也给大家做个参考。
1. 直接上最简单的 一种 ajax 异步上传图片,并预览
html:
图片上传 | cookie file:
desc:
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 { Listitems = 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(); } }
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
Das obige ist der detaillierte Inhalt vonAjax 上传图片并预览的简单实现. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!