上传问题分析--目录分离
- 為了防止同一個目錄下方上傳文件數量過多 ---- 必須采用目錄分離算法
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
?????? }
?
- 亂碼問題
普通編寫項 value屬性亂碼 ------------- fileItem.getString(編碼集);
上傳文件項 文件名亂碼 --------- fileupload.setHeaderEncoding(編碼集);
package cn.learn.utils;import java.io.File; import java.util.UUID;public class FileUploadUtils {// 得到上傳文件真實名稱 c:\a.txt a.txtpublic static String getRealName(String filename) {int index = filename.lastIndexOf("\\") + 1;return filename.substring(index);}// 獲取隨機名稱 a.txtpublic 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;}}?
總結
以上是生活随笔為你收集整理的上传问题分析--目录分离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上传问题分析2--文件重名
- 下一篇: 文件上传演示