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;
}
}
package cn.learn.web.servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import cn.learn.utils.FileUploadUtils;
public class Upload4Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
// 1.创建 DiskFileItemFactory
File file = new File(this.getServletContext().getRealPath("/temp"));// 获取temp目录部署到tomcat后的绝对磁盘路径
DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 100, file); // 使用默认的.
// 2.创建ServletFileUpload
ServletFileUpload upload = new ServletFileUpload(factory);
boolean flag = upload.isMultipartContent(request); // 用于判断是否是上传操作.
if (flag) {
// 解决上传文件名称中文乱码
upload.setHeaderEncoding("utf-8");
// 设置上传文件大小
// upload.setSizeMax(1024 * 1024 * 10);// 总大小为10m
try {
List<FileItem> items = upload.parseRequest(request);// 解决request,得到所有的上传项FileItem
// 3.得到所有上传项
for (FileItem item : items) {
if (!item.isFormField()) {
// 上传组件
String name = item.getName(); // 上传文件名称
// 得到上传文件真实名称
String filename = FileUploadUtils.getRealName(name);
// 得到随机名称
String uuidname = FileUploadUtils
.getUUIDFileName(filename);
// 得到随机目录
String randomDirectory = FileUploadUtils
.getRandomDirectory(filename);
// 注意:随机目录可能不存在,需要创建.
String parentPath = this.getServletContext()
.getRealPath("/upload");
File rd = new File(parentPath, randomDirectory);
if (!rd.exists()) {
rd.mkdirs();
}
IOUtils.copy(item.getInputStream(),
new FileOutputStream(new File(rd, uuidname)));
// 删除临时文件
item.delete();
}
}
} catch (FileUploadException e) {
// e.printStackTrace();
response.getWriter().write(e.getMessage());
return;
}
} else {
response.getWriter().write("不是上传操作");
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}