java 怎么生成一个0-9,a-z的一个44位字符串作为上传文件的名字
PHP中文网
PHP中文网 2017-04-17 11:54:27
0
5
335

找到一个时间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);
    }
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!