随机生成文件名的函数-文件上传使用了

程序代码:
<% 
 Function Generator(Length) 
  dim i, tempS, v 
  dim c(39) 
  tempS = "" 
  c(1) = "a": c(2) = "b": c(3) = "c": c(4) = "d": c(5) = "e": c(6) = "f": c(7) = "g" 
  c(8) = "h": c(9) = "i": c(10) = "j": c(11) = "k": c(12) = "l": c(13) = "m": c(14) = "n" 
  c(15) = "o": c(16) = "p": c(17) = "q": c(18) = "r": c(19) = "s": c(20) = "t": c(21) = "u" 
  c(22) = "v": c(23) = "w": c(24) = "x": c(25) = "y": c(26) = "z": c(27) = "1": c(28) = "2" 
  c(29) = "3": c(30) = "4": c(31) = "5": c(32) = "6": c(33) = "7": c(34) = "8": c(35) = "9" 
  c(36) = "-": c(37) = "_": c(38) = "@": c(39) = "!" 
  If isNumeric(Length) = False Then 
   Response.Write "A numeric datatype was not submitted to this function." 
   Exit Function 
  End If 
  For i = 1 to Length 
   Randomize 
   v = Int((39 * Rnd) + 1) 
   tempS = tempS & c(v) 
  Next 
  Generator = tempS 
 End Function 
     
 For i = 1 to 20 
  Randomize 
  x = Int((20 * Rnd) + 1) + 10 
  Response.Write Generator(x) & "<br>" & vbnewline 
 Next 
%> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Snowflake算法生成分布式随机文件名,示例代码如下: ```java import java.io.File; import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.node.ObjectNode; public class DistributedRandomFileNameGenerator { private static final long EPOCH = 1420041600000L; // 2015-01-01 private static final int NODE_ID_BITS = 10; private static final int SEQUENCE_BITS = 12; private static final long MAX_NODE_ID = (1L << NODE_ID_BITS) - 1; private static final long MAX_SEQUENCE = (1L << SEQUENCE_BITS) - 1; private final long nodeId; private long lastTimestamp = -1L; private long sequence = 0L; public DistributedRandomFileNameGenerator(long nodeId) { if (nodeId < 0 || nodeId > MAX_NODE_ID) { throw new IllegalArgumentException(String.format("NodeId must be between 0 and %d", MAX_NODE_ID)); } this.nodeId = nodeId; } public synchronized String generate() { long timestamp = System.currentTimeMillis() - EPOCH; if (timestamp < lastTimestamp) { throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (timestamp == lastTimestamp) { sequence = (sequence + 1) & MAX_SEQUENCE; if (sequence == 0) { timestamp = tilNextMillis(lastTimestamp); } } else { sequence = 0L; } lastTimestamp = timestamp; long id = ((timestamp << (NODE_ID_BITS + SEQUENCE_BITS)) | (nodeId << SEQUENCE_BITS) | sequence) & Long.MAX_VALUE; return Long.toString(id, 36); } private long tilNextMillis(long lastTimestamp) { long timestamp = System.currentTimeMillis() - EPOCH; while (timestamp <= lastTimestamp) { timestamp = System.currentTimeMillis() - EPOCH; } return timestamp; } public static void main(String[] args) throws IOException { DistributedRandomFileNameGenerator generator = new DistributedRandomFileNameGenerator(1); String fileName = generator.generate(); System.out.println(fileName); // Save nodeId and lastTimestamp to file for recovery ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); ObjectNode node = mapper.createObjectNode(); node.put("nodeId", generator.nodeId); node.put("lastTimestamp", generator.lastTimestamp); mapper.writeValue(new File("generator.json"), node); } } ``` 上述代码中,`DistributedRandomFileNameGenerator`类使用Snowflake算法生成分布式随机文件名。在构造函数中指定节点ID,然后调用`generate()`方法即可生成随机文件名。为了防止时钟回拨等问题,需要在生成文件名时保证时间戳单调递增。如果需要在重启程序后恢复生成器状态,可以将节点ID和上一次生成文件名的时间戳保存到文件中,然后在程序启动时读取该文件,恢复生成器状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值