WebUtils.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package cn.keking.utils;
  2. import io.mola.galimatias.GalimatiasParseException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.net.URLEncoder;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. /**
  10. * @author : kl
  11. * create : 2020-12-27 1:30 上午
  12. **/
  13. public class WebUtils {
  14. /**
  15. * 获取标准的URL
  16. * @param urlStr url
  17. * @return 标准的URL
  18. */
  19. public static URL normalizedURL(String urlStr) throws GalimatiasParseException, MalformedURLException {
  20. return io.mola.galimatias.URL.parse(urlStr).toJavaURL();
  21. }
  22. /**
  23. * 获取url中的参数
  24. *
  25. * @param url url
  26. * @param name 参数名
  27. * @return 参数值
  28. */
  29. public static String getUrlParameterReg(String url, String name) {
  30. Map<String, String> mapRequest = new HashMap<>();
  31. String strUrlParam = truncateUrlPage(url);
  32. if (strUrlParam == null) {
  33. return "";
  34. }
  35. //每个键值为一组
  36. String[] arrSplit = strUrlParam.split("[&]");
  37. for (String strSplit : arrSplit) {
  38. String[] arrSplitEqual = strSplit.split("[=]");
  39. //解析出键值
  40. if (arrSplitEqual.length > 1) {
  41. //正确解析
  42. mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
  43. } else if (!arrSplitEqual[0].equals("")) {
  44. //只有参数没有值,不加入
  45. mapRequest.put(arrSplitEqual[0], "");
  46. }
  47. }
  48. return mapRequest.get(name);
  49. }
  50. /**
  51. * 去掉url中的路径,留下请求参数部分
  52. *
  53. * @param strURL url地址
  54. * @return url请求参数部分
  55. */
  56. private static String truncateUrlPage(String strURL) {
  57. String strAllParam = null;
  58. strURL = strURL.trim();
  59. String[] arrSplit = strURL.split("[?]");
  60. if (strURL.length() > 1) {
  61. if (arrSplit.length > 1) {
  62. if (arrSplit[1] != null) {
  63. strAllParam = arrSplit[1];
  64. }
  65. }
  66. }
  67. return strAllParam;
  68. }
  69. /**
  70. * 从url中剥离出文件名
  71. *
  72. * @param url 格式如:http://www.com.cn/20171113164107_月度绩效表模板(新).xls?UCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=I D1NOFtAJSPT16E6imv6JWuq0k=
  73. * @return 文件名
  74. */
  75. public static String getFileNameFromURL(String url) {
  76. if (url.toLowerCase().startsWith("file:")) {
  77. try {
  78. URL urlObj = new URL(url);
  79. url = urlObj.getPath().substring(1);
  80. } catch (MalformedURLException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. // 因为url的参数中可能会存在/的情况,所以直接url.lastIndexOf("/")会有问题
  85. // 所以先从?处将url截断,然后运用url.lastIndexOf("/")获取文件名
  86. String noQueryUrl = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
  87. return noQueryUrl.substring(noQueryUrl.lastIndexOf("/") + 1);
  88. }
  89. /**
  90. * 从url中获取文件后缀
  91. *
  92. * @param url url
  93. * @return 文件后缀
  94. */
  95. public static String suffixFromUrl(String url) {
  96. String nonPramStr = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
  97. String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
  98. return KkFileUtils.suffixFromFileName(fileName);
  99. }
  100. /**
  101. * 对url中的文件名进行UTF-8编码
  102. *
  103. * @param url url
  104. * @return 文件名编码后的url
  105. */
  106. public static String encodeUrlFileName(String url) {
  107. String encodedFileName;
  108. String fullFileName = WebUtils.getUrlParameterReg(url, "fullfilename");
  109. if (fullFileName != null && fullFileName.length() > 0) {
  110. try {
  111. encodedFileName = URLEncoder.encode(fullFileName, "UTF-8");
  112. } catch (UnsupportedEncodingException e) {
  113. return null;
  114. }
  115. String noQueryUrl = url.substring(0, url.indexOf("?"));
  116. String parameterStr = url.substring(url.indexOf("?"));
  117. parameterStr = parameterStr.replaceFirst(fullFileName, encodedFileName);
  118. return noQueryUrl + parameterStr;
  119. }
  120. String noQueryUrl = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
  121. int fileNameStartIndex = noQueryUrl.lastIndexOf('/') + 1;
  122. int fileNameEndIndex = noQueryUrl.lastIndexOf('.');
  123. try {
  124. encodedFileName = URLEncoder.encode(noQueryUrl.substring(fileNameStartIndex, fileNameEndIndex), "UTF-8");
  125. } catch (UnsupportedEncodingException e) {
  126. return null;
  127. }
  128. return url.substring(0, fileNameStartIndex) + encodedFileName + url.substring(fileNameEndIndex);
  129. }
  130. }