Jsp代码 注意事项
UploadUtil工具类
工具类目的:
1.获取上传文件的真实名称,例如1.jpg
2.生成uuid文件名称,因为用户可能重复上传,不然会覆盖
3.在指定路径下生成随机的路径
import java.util.Random;
import java.util.UUID;
public class UploadUtils {
/**
* 获取文件真实名称
* 由于浏览器的不同获取的名称可能为:c:/upload/1.jpg或者1.jpg
* 最终获取的为 1.jpg
* @param name 上传上来的文件名称
* @return 真实名称
*/
public static String getRealName(String name){
//获取最后一个"/"
int index = name.lastIndexOf("\\");
return name.substring(index+1);
}
/**
* 获取随机名称
* @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 UUID.randomUUID().toString().replace("-", "").toUpperCase()+realName.substring(index);
}
}
/**
* 获取文件目录,可以获取256个随机目录
* @return 随机目录
*/
public static String getDir(){
String s="0123456789ABCDEF";
Random r = new Random();
return "/"+s.charAt(r.nextInt(16))+"/"+s.charAt(r.nextInt(16));
}
public static void main(String[] args) {
String s="G:\\day17-基础加强\\resource\\1.png";
//String s="1.jgp";
String realName = getRealName(s);
System.out.println(realName);
String uuidName = getUUIDName(realName);
System.out.println(uuidName);
String dir = getDir();
System.out.println(dir);
}
}
添加商品代码
public class ProductServlet extends BaseServlet {
/**
* 添加商品信息
*
* @param request
* @param response
* @throws Exception
*/
public void addProduct(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, String> map1 = null;
try {
// 1.获取磁盘文件项工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2.创建核心上传对象
ServletFileUpload upload = new ServletFileUpload(factory);
// 3.通过核心上传对象解析请求 获取文件项集合
List<FileItem> list = upload.parseRequest(request);
for (FileItem fi : list) {
// 普通文件项
if (fi.isFormField()) {
// 获取请求携带的name属性的名
String name = fi.getFieldName();
// 获取请求携带的value属性的值
String val = fi.getString("utf-8");
//将数据封装到map中
map.put(name, val);
} else {// 文件上传项
// 获取请求携带的name属性的名称
String fieldName = fi.getFieldName();
// 获取文件上传的名称
String name = fi.getName();
// 获取真实名称,例如:123.jpg
String realName = UploadUtils.getRealName(name);
// 将上面的名称改成uuid名称,例如:AWE13Q1W3.jpg,主要是为了存放图片时,不重复现象
String uuidName = UploadUtils.getUUIDName(realName);
// 通过uploadUtils生成一个随机路径
String dir = UploadUtils.getDir();
// 获取ServletContext
ServletContext context = getServletContext();
// 获取文件需要存放的绝对路径
File file = new File(context.getRealPath("/upload")+dir);
if (!file.exists()) {
file.mkdirs();
}
// 获取文件输入流
InputStream is = fi.getInputStream();
FileOutputStream os = new FileOutputStream(file + "/" + uuidName);
// 复制文件
IOUtils.copy(is, os);
// 清除缓存
fi.delete();
map.put(fieldName, "upload" + dir + "/" + uuidName);
}
}
// map和文件复制好之后,需要把数据信息存到数据库
Category cate = new Category();
// 将map信息封装到分类中
BeanUtils.populate(cate, map);
Product pro = new Product();
// 将map信息封装到pro实体中
BeanUtils.populate(pro, map);
// 将cate实体信息再封装到pro中
pro.setCategory(cate);
ProductService service = (ProductService) BeanFactory.getBean("ProductService");
//调用service层添加封装好的商品信息
service.addProduct(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
}