[Java教程]Ajax 上传图片并预览
0 2016-09-21 01:00:08
1. 直接上最简单的 一种 ajax 异步上传图片,并预览
html:1 2 3
4 5 图片上传 | cookie 6 7 8 file:9 desc:
10 11 12 15 16 32 33
servlet:1 protected void doPost(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3 DiskFileItemFactory factory = new DiskFileItemFactory(); 4 5 ServletFileUpload upload = new ServletFileUpload(factory); 6 7 String path = request.getServletContext().getRealPath("/upload"); 8 String name = null; 9 try {10 List items = upload.parseRequest(request);11 for (FileItem item : items) {12 if(item.isFormField()){13 System.out.println(item.getFieldName() + ": " + item.getString());14 } else {15 name = item.getName();16 item.write(new File(path,name));17 }18 }19 PrintWriter out = response.getWriter();20 out.print("{");21 out.print("url:\"" + name +"\"");22 out.print("}");23 24 } catch (Exception e) {25 e.printStackTrace();26 }27 }
3. 这里会 用到一个 ajaxupload.js, 网上多得很,实在找不到,也可以私我,给你们
本文网址:http://www.shaoqun.com/a/251111.html
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。
ajax
0