我试图生成带有特殊字符的随机数
我为简单整数做的是
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt();
Run Code Online (Sandbox Code Playgroud)
我能做些什么才能在随机数字中得到这样的东西
String salt = "Random$SaltValue#WithSpecialCharacters12@$@4&#%^$*";
Run Code Online (Sandbox Code Playgroud)
谢谢
我有点SecureRandom不清楚.以这里的代码为例:
import java.security.SecureRandom;
import java.math.BigInteger;
public final class SessionIdentifierGenerator{
private static SecureRandom random = new SecureRandom();
public static void main(String[] args) {
nextSessionId();
}
public static String nextSessionId(){
BigInteger ss = new BigInteger(130, random);
System.out.println(ss);
System.out.println(ss.toString(32));
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
一个例子的出局将是:
1107894191825737787218556533052298445977
q1fikf1m0jagvlrmbtdah0mh4p
由于BigInteger是整数,输出是安静可预测的,但我不明白的是,自从我应用toString()方法以来,随机字符串来自哪里,所以我认为这个字符串它将是相同的数字序列但是作为字符串,所以这种讨厌的方式和原因是什么?
谢谢.ps:我不知道以前是否曾经问过,但我没有找到任何东西......原始代码维基
我是java的新手.我大概3天前开始了.我想制作一行随机字符并将它们放入一个字符串中.谢谢.
import java.util.Random;
public class test{
public static void main (String[]args){
final String alphabet = "abcdefghigklmnopqrstuvwxyz";
final int N = alphabet.length();
Random r = new Random();
for (int i = 0; i < 50; i++) {
String s = alphabet.charAt(r.nextInt(N));
// System.out.println(alphabet.charAt(r.nextInt(N)));
}}}
Run Code Online (Sandbox Code Playgroud) 我在互联网上找到了这个:http://www.code2learn.com/2011/06/encryption-and-decryption-of-data-using.html
基本上,我想做的不是让密钥设置如此
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
Run Code Online (Sandbox Code Playgroud)
我希望它是随机的(但必须是16个字节),并且可以访问它(在屏幕上打印).还有,有没有办法将其设置为字符串,而不是字节类型?
我正在将文件上传到文件夹,我已将文件名指定为“1.jpg”,因此当我上传新文件时,它将覆盖现有文件,那么我如何为该文件提供随机文件名我正在上传
我的上传代码在这里
@RequestMapping(value = "/event/uploadFile",headers=("content-type=multipart/*"), method = RequestMethod.POST,consumes ={"application/x-www-form-urlencoded"})
//String quote_upload=C:\fakepath\images.jpg
public @ResponseBody
String uploadFileHandler(
@RequestParam MultipartFile file) {
System.out.println("Creating the directory to store file");
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
// Creating the directory to store file
String rootPath = System.getProperty("catalina.home");
File dir = new File(rootPath + File.separator + "tmpFiles");
if (!dir.exists())
dir.mkdirs();
// Create the file on server
File serverFile = new File(dir.getAbsolutePath()
+ File.separator+"1.jpg");
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
System.out.println("************Server …Run Code Online (Sandbox Code Playgroud) 我想使用以下代码生成唯一的25个字符和数字长字符串,
public static String generateString() {
String uuid = UUID.randomUUID().toString();
return uuid;
}
Run Code Online (Sandbox Code Playgroud)
如何设置生成的String的大小并删除-符号?
我试图从for循环的结果中创建一个变量.首先是这可能吗?如果有可能我该怎么办?到目前为止这是我的代码:
Random r = new Random();
Random n = new Random();
int p = (int)(n.nextInt(14));
for (int i = 0; i < p; i++) {
char c = (char) (r.nextInt(26) + 'a');
System.out.println(c);}
String word = ("output of forloop goes here");
Run Code Online (Sandbox Code Playgroud)
我想使用for循环将所有随机生成的字母放入一个单词中.我该怎么做呢?