upload.jsp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <%--
  2. 温馨提示:
  3. 在flash的参数名upload_url中可自行定义一些参数(请求方式:POST),定义后在服务器端获取即可,比如可以应用到用户验证,文件的保存名等。
  4. 本示例未作极致的用户体验与严谨的安全设计(如用户直接访问此页时该如何,万一客户端数据不可信时验证文件的大小、类型等),只保证正常情况下无误,请阁下注意。
  5. --%>
  6. <%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
  7. <%@ page import="java.io.*"%>
  8. <%@ page import="java.util.*"%>
  9. <%@ page import="java.text.*"%>
  10. <%@ page import="org.apache.commons.fileupload.*"%>
  11. <%@ page import="org.apache.commons.fileupload.disk.*"%>
  12. <%@ page import="org.apache.commons.fileupload.servlet.*"%>
  13. <%@ page import="org.apache.commons.fileupload.util.*"%>
  14. <%@ page import="com.alibaba.fastjson.*"%>
  15. <%
  16. String contentType = request.getContentType();
  17. if ( contentType.indexOf("multipart/form-data") >= 0 )
  18. {
  19. Result result = new Result();
  20. result.avatarUrls = new ArrayList();
  21. result.success = false;
  22. result.msg = "Failure!";
  23. FileItemFactory factory = new DiskFileItemFactory();
  24. ServletFileUpload upload = new ServletFileUpload(factory);
  25. FileItemIterator fileItems = upload.getItemIterator(request);
  26. //定义一个变量用以储存当前头像的序号
  27. int avatarNumber = 1;
  28. //取服务器时间+8位随机码作为部分文件名,确保文件名无重复。
  29. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssS");
  30. String fileName = simpleDateFormat.format(new Date());
  31. Random random = new Random();
  32. String randomCode = "";
  33. for ( int i = 0; i < 8; i++ )
  34. {
  35. randomCode += Integer.toString(random.nextInt(36), 36);
  36. }
  37. fileName = fileName + randomCode;
  38. //基于原图的初始化参数
  39. String initParams = "";
  40. BufferedInputStream inputStream;
  41. BufferedOutputStream outputStream;
  42. //遍历表单域
  43. while( fileItems.hasNext() )
  44. {
  45. FileItemStream fileItem = fileItems.next();
  46. String fieldName = fileItem.getFieldName();
  47. //是否是原始图片 file 域的名称(默认的 file 域的名称是__source,可在插件配置参数中自定义。参数名:src_field_name)
  48. Boolean isSourcePic = fieldName.equals("__source");
  49. //文件名,如果是本地或网络图片为原始文件名(不含扩展名)、如果是摄像头拍照则为 *FromWebcam
  50. //String name = fileItem.getName();
  51. //当前头像基于原图的初始化参数(即只有上传原图时才会发送该数据),用于修改头像时保证界面的视图跟保存头像时一致,提升用户体验度。
  52. //修改头像时设置默认加载的原图url为当前原图url+该参数即可,可直接附加到原图url中储存,不影响图片呈现。
  53. if ( fieldName.equals("__initParams") )
  54. {
  55. inputStream = new BufferedInputStream(fileItem.openStream());
  56. byte[] bytes = new byte [inputStream.available()];
  57. inputStream.read(bytes);
  58. initParams = new String(bytes, "UTF-8");
  59. inputStream.close();
  60. }
  61. //如果是原始图片 file 域的名称或者以默认的头像域名称的部分“__avatar”打头
  62. else if ( isSourcePic || fieldName.startsWith("__avatar") )
  63. {
  64. String virtualPath = "/upload/jsp_avatar" + avatarNumber + "_" + fileName + ".jpg";
  65. //原始图片(默认的 file 域的名称是__source,可在插件配置参数中自定义。参数名:src_field_name)。
  66. if( isSourcePic )
  67. {
  68. result.sourceUrl = virtualPath = "/upload/jsp_source_" + fileName + ".jpg";
  69. }
  70. //头像图片(默认的 file 域的名称:__avatar1,2,3...,可在插件配置参数中自定义,参数名:avatar_field_names)。
  71. else
  72. {
  73. result.avatarUrls.add(virtualPath);
  74. avatarNumber++;
  75. }
  76. inputStream = new BufferedInputStream(fileItem.openStream());
  77. outputStream = new BufferedOutputStream(new FileOutputStream(application.getRealPath("/") + virtualPath.replace("/", "\\")));
  78. Streams.copy(inputStream, outputStream, true);
  79. inputStream.close();
  80. outputStream.flush();
  81. outputStream.close();
  82. }
  83. /*else
  84. {
  85. 附加在接口中的其他参数...
  86. 如下代码在上传接口upload.jsp中定义了一个user=xxx的参数:
  87. var swf = new fullAvatarEditor("swf", {
  88. id: "swf",
  89. upload_url: "Upload.asp?user=xxx"
  90. });
  91. 即可如下获取user的值xxx
  92. inputStream = new BufferedInputStream(fileItem.openStream());
  93. byte[] bytes = new byte [inputStream.available()];
  94. inputStream.read(bytes);
  95. String user = new String(bytes, "UTF-8");
  96. inputStream.close();
  97. }*/
  98. }
  99. if ( result.sourceUrl != null )
  100. {
  101. result.sourceUrl += initParams;
  102. }
  103. result.success = true;
  104. result.msg = "Success!";
  105. /*
  106. To Do...可在此处处理储存事项
  107. */
  108. //返回图片的保存结果(返回内容为json字符串,可自行构造,该处使用fastjson构造)
  109. out.println(JSON.toJSONString(result));
  110. }
  111. %>
  112. <%!
  113. /**
  114. * 表示上传的结果。
  115. */
  116. private class Result
  117. {
  118. /**
  119. * 表示图片是否已上传成功。
  120. */
  121. public Boolean success;
  122. /**
  123. * 自定义的附加消息。
  124. */
  125. public String msg;
  126. /**
  127. * 表示原始图片的保存地址。
  128. */
  129. public String sourceUrl;
  130. /**
  131. * 表示所有头像图片的保存地址,该变量为一个数组。
  132. */
  133. public ArrayList avatarUrls;
  134. }
  135. %>