public String addProduct (HttpServletRequest request, HttpServletResponse response) throws Exception {
HashMap<String, String> map = new HashMap<String, String>();
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
List<FileItem> list = upload.parseRequest(request);
//遍历lsit
for(FileItem item:list){
if(item.isFormField()){
//如果是普通项目,获取名称和值
String fieldName = item.getFieldName();
String value = item.getString("GBK");
//放入到map中
map.put(fieldName, value);
}else{
//如果为上传项目,获取名称和文件名称
String fieldName = item.getFieldName();
String oldName = item.getName();
//改变文件名称 //放入到map中
String newName = UploadUtils.getUUIDName(oldName);
//获取真实目录路径
String realPath = this.getServletContext().getRealPath("products/3/");
System.out.println(realPath);
//生成随机目录
String rdDir = UploadUtils.getDir(newName);
File dir = new File(realPath,rdDir);
if(!dir.exists()){
//创建文件目录
dir.mkdirs();
}
//创建文件
File file = new File(dir,newName);
if(!file.exists()){
//创建文件
file.createNewFile();
}
map.put(fieldName, "products/3/"+rdDir+"/"+newName);
//System.out.println("products/3/"+rdDir+"/"+newName);
//获取上传项二进制流
InputStream in = item.getInputStream();
FileOutputStream out = new FileOutputStream(file);
//将输入流 copy 到输出流
IOUtils.copy(in, out);
//关闭流连接
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
Product product = new Product();
//map 赋值到 Product对象
DateConverter converter = new DateConverter();
converter.setPattern(new String("yyyy-MM-dd"));
ConvertUtils.register(converter,Date.class);
BeanUtils.populate(product, map);
product.setPid(UUIDUtils.getId());
product.setPdate(new Date());
product.setPflag(0);
//service 保存 Product对象
ProductService service = new ProductServiceImpl();
service.addProduct(product);
//重定向到商品分页查询的servlet
response.sendRedirect("/store/AdminProductServlet?method=findProductWithPage¤tPageNum=1");
return null;
}
public class UploadUtils {
/**
* 获取随机名称
* @param realName 真实名称
* @return uuid
*/
public static String getUUIDName(String realName){
//realname 可能是 1.jpg 也可能是 1
//获取后缀名
int index = realName.lastIndexOf(".");
if(index==-1){
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}else{
return UUIDUtils.getId()+realName.substring(index);
}
}
/**
* 获取文件真实名称
* @param name
* @return
*/
public static String getRealName(String name){
// c:/upload/1.jpg 1.jpg
//获取最后一个"/"
int index = name.lastIndexOf("\\");
return name.substring(index+1);
}
/**
* 获取文件目录
* @param name 文件名称
* @return 目录
*/
public static String getDir(String name){
//任意一个对象都有一个hash码 131313213
int i = name.hashCode();
//将hash码转成16禁止的字符串
String hex = Integer.toHexString(i);
//System.out.println(hex);
int j=hex.length();
for(int k=0;k<8-j;k++){
hex="0"+hex;
}
return "/"+hex.charAt(0)+"/"+hex.charAt(1)+"/"+hex.charAt(2)+"/"+hex.charAt(3)+"/"+hex.charAt(4)+"/"+hex.charAt(5)+"/"+hex.charAt(6)+"/"+hex.charAt(7);
}
@SuppressWarnings("unused")
public static void main(String[] args) {
//String s="G:\\day17-基础加强\\resource\\1.jpg";
//String s="1.jgp";
//String realName = getRealName(s);
//System.out.println(realName);
//String uuidName = getUUIDName(realName);
//System.out.println(uuidName);
String dir = getDir("ASDFSADF");
// /b/b/5/6/3/b/a/1/ 234234.jpg
// /f/e/d/c/4/9/8/4/ 2341242314321.bmp
System.out.println(dir);
}
}