Upload Analysis - directory Separation

同一目录下文件过多	

	只需要分目录就可以.
		1) 按照上传时间进行目录分离 (周、月 )
		2) 按照上传用户进行目录分离 ----- 为每个用户建立单独目录 
		3) 按照固定数量进行目录分离 ------ 假设每个目录只能存放3000个文件 ,
		   每当一个目录存满3000个文件后,创建一个新的目录
		4)按照文件名的hashcode进行目录分离.
			
		public static String generateRandomDir(String uuidFileName) {
			// 获得唯一文件名的hashcode
			int hashcode = uuidFileName.hashCode();
			// 获得一级目录
			int d1 = hashcode & 0xf;       
			// 获得二级目录
			int d2 = (hashcode >>> 4) & 0xf;

			return "/" + d2 + "/" + d1;// 共有256目录l
  • In order to prevent an excessive number of the same directory under the directory must be used to upload files ---- separation algorithm

1) for directory Separation (weeks, months) in accordance Update

2) Upload Directory user ----- isolated as a separate directory for each user to establish

3) carried out according to a fixed number of directories separated ------ assume that each file directory can only store 3000, whenever a directory is full 3000 file, create a new directory

4) were isolated as directory hashcode unique file name 

       public static String generateRandomDir(String uuidFileName) {

              // Get hashcode unique file name

              int hashcode = uuidFileName.hashCode();

              // Get a directory

              int d1 = hashcode & 0xf;      

              // get secondary directory

              int d2 = (hashcode >>> 4) & 0xf;

 

              return "/" + d2 + "/" + d1; // a total of 256 l directory

       }

 

  • Garbage problem

Ordinary write attribute value term distortion ------------- fileItem.getString (codeset);

Upload files in file name garbled --------- fileupload.setHeaderEncoding (code sets);

package cn.learn.utils;

import java.io.File;
import java.util.UUID;

public class FileUploadUtils {

	// 得到上传文件真实名称 c:\a.txt a.txt
	public static String getRealName(String filename) {

		int index = filename.lastIndexOf("\\") + 1;

		return filename.substring(index);

	}

	// 获取随机名称 a.txt
	public static String getUUIDFileName(String filename) {
		int index = filename.lastIndexOf(".");
		if (index != -1) {

			return UUID.randomUUID() + filename.substring(index);
		} else {
			return UUID.randomUUID().toString();
		}
	}

	// 目录分离算法
	public static String getRandomDirectory(String filename) {

		// int hashcode = filename.hashCode();
		//
		// // System.out.println(hashcode);
		//
		// // int类型数据在内存中占32位。转换成16进制数,就得到8个16进制数
		// String hex = Integer.toHexString(hashcode);
		//
		// // System.out.println(hex); // 056d9363
		//
		// return "/" + hex.charAt(0) + "/" + hex.charAt(1);

		int hashcode = filename.hashCode();

		System.out.println(Integer.toBinaryString(hashcode));

		int a = hashcode & 0xf;

		hashcode = hashcode >>> 4;

		int b = hashcode & 0xf;

		return "/" + a + "/" + b;

	}

}

 

Released 2417 original articles · won praise 62 · Views 200,000 +

Guess you like

Origin blog.csdn.net/Leon_Jinhai_Sun/article/details/105157216