MultipartRequest上传后文件改名

File fileDir = new File(this.getServletContext().getRealPath( "/upimg")); if (!fileDir.exists()) { fileDir.mkdirs(); } int inmaxPostSize = 10 * 1024 * 1024; MultipartRequest multirequest = null; try { multirequest = new MultipartRequest(request, fileDir .getAbsolutePath(), inmaxPostSize, "utf-8"); // utf-8中文编码模式上传文件 String name = multirequest.getParameter("name");// 获取普通信息 String typeCode = multirequest.getParameter("typeCode"); String info = multirequest.getParameter("info"); String newImgPath = ""; Enumeration<String> filedFileNames = multirequest .getFileNames(); String filedName = null; if (null != filedFileNames && name != null && !name.trim().equals("") && info != null && !info.trim().equals("")) { while (filedFileNames.hasMoreElements()) { filedName = filedFileNames.nextElement();// 文件文本框的名称 // 获取该文件框中上传的文件,即对应到上传到服务器中的文件 File uploadFile = multirequest.getFile(filedName); if (null != uploadFile && uploadFile.length() > 0) { String imgPath = uploadFile.getName(); //imgPath为原文件名 int idx = imgPath.lastIndexOf("."); //文件后缀 String extention= imgPath.substring(idx); java.util.Date dt = new java.util.Date(System .currentTimeMillis()); SimpleDateFormat fmt = new SimpleDateFormat( "yyyyMMddHHmmssSSS"); String time = fmt.format(dt); //新的文件名(日期+后缀) newImgPath = time + extention; File f = new File(fileDir + "/" + newImgPath); uploadFile.renameTo(f); } } Products products = new Products(); products.setpName(name); products.setInfo(info); products.setImgPath(newImgPath); products.setTypeCode(typeCode); products.setPubDate(new Date()); productsDao.addProducts(products); 。。。 } else { 。。。 } } catch (Exception e) { e.printStackTrace(); }

你可能感兴趣的:(exception,Date,String,服务器,File,null)