java 姓名生成_java 怎么生成一个0-9,a-z的一个44位字符串作为上传文件的名字

找到一个时间MD5加密的

package org.blog.controller;

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

import org.blog.dao.PicDAO;

import org.blog.model.Pic;

import org.blog.util.md5;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.multipart.MultipartFile;

@Controller

@RequestMapping(value = "/save")

public class UploadController {

@Autowired

PicDAO picDAO;

@RequestMapping(value = "/upload", method = RequestMethod.POST)

public String handleFormUpload(Pic pic,MultipartFile file,HttpServletRequest request) throws IOException{

String pic_path = request.getSession().getServletContext().getRealPath("/theme");

/* 从当时时间MD5强制重命名图片*/

String pic_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

String pic_type = file.getContentType();

String file_ture_name = md5.MD5(pic_time);

if(pic_type.equals("image/jpeg")){

file_ture_name = file_ture_name.concat(".jpg");

} else if (pic_type.equals("image/png")){

file_ture_name = file_ture_name.concat(".png");

} else if(pic_type.equals("image/bmp")){

file_ture_name = file_ture_name.concat(".bmp");

} else if(pic_type.equals("image/gif")){

file_ture_name = file_ture_name.concat(".gif");

} else file_ture_name = file_ture_name.concat(".jpg");

/*保存文件*/

FileUtils.copyInputStreamToFile(file.getInputStream(), new File(pic_path, file_ture_name));

pic.setPicFile(file_ture_name);

try {

picDAO.add(pic);

return "redirect:/admin/AllPic";

} catch (Exception e) {

e.printStackTrace();

return "redirect:/admin/error";

}

}

}

0-9,a-z的一个44位字符串怎么搞

找到一个方法

/**

* JAVA获得0-9,a-z,A-Z范围的随机数

* @param length 随机数长度

* @return String

*/

public static String getRandomChar(int length) {

char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',

'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

Random random = new Random();

StringBuffer buffer = new StringBuffer();

for (int i = 0; i < length; i++) {

buffer.append(chr[random.nextInt(62)]);

}

return buffer.toString();

}

public static String getRandomChar() {

return getRandomChar(10);

}

========================================================================

文件名要求是:

前面四十几位为0-9,a-z不要大写,

后面6位0-9,a-z,A-Z,需要大写,

中间用-连接,

格式:7b9194849dd989fa9e9df1332c8467111caf242c854f62-bJuK1X

下面这样是不是速度非常慢,还是这个本来就很快,不需要考虑速度

@Test

public void testRandomChar() {

for(int i=0;i<=100;i++){

System.out.println(getRandomChar2(44)+"-"+getRandomChar(6));

}

}

/**

* JAVA获得0-9,a-z,A-Z范围的随机数

* @param length 随机数长度

* @return String

*/

public static String getRandomChar(int length) {

char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',

'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

Random random = new Random();

StringBuffer buffer = new StringBuffer();

for (int i = 0; i < length; i++) {

buffer.append(chr[random.nextInt(62)]);

}

return buffer.toString();

}

public static String getRandomChar2(int length) {

char[] chr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',

'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

Random random = new Random();

StringBuffer buffer = new StringBuffer();

for (int i = 0; i < length; i++) {

buffer.append(chr[random.nextInt(36)]);

}

return buffer.toString();

}

public static String getRandomChar() {

return getRandomChar(10);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值