upload.jsp 5.5 KB

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