enctype="multipart/form-data">
文件:
名字:
需要将cos.jar导入
String saveDir = "E:/";
int maxPostSize = 2000*1024*1024;//1MB
String encoding = "GBK";
//存放文件描述 String fileDescription[] = {null,null};
//上传文件名 String fileName = null;
//上传文件数 int count = 0;
MultipartRequest multi = null;
try{
multi = new MultipartRequest(request ,
saveDir , maxPostSize ,encoding);
}catch(IOException e){
//上传大小超过maxPostSize就会出现IOException
out.println(e.getMessage());
return;
}
//取得文件描述
if(multi.getParameter("aa")!=null){
fileDescription[0] = multi.getParameter("aa");
}else{
fileDescription[0] = "";
}
//取得所有上传文件名称
String ContentType="";
Enumeration filesname = multi.getFileNames();
while(filesname.hasMoreElements()){
String name = (String)filesname.nextElement();
fileName = multi.getFilesystemName(name);
File f = multi.getFile(name);
ContentType = fileName.substring(fileName.lastIndexOf(".")+1);
if(fileName!=null){
count++;
%>
您上传的第个文件:
文件名:
文件类型:
文件描述:
}//end if
}//end while
File file=new File(saveDir+fileName);//旧文件路径
SimpleDateFormat sdFormat = new SimpleDateFormat( "yyyyMMddhhmmssSSS ");
Date currentTime = new Date();
String myTime = sdFormat.format(currentTime);
if(file.exists()){
file.renameTo(new File(saveDir+myTime+"."+ContentType));
}else{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
out.println("操作完成!");
%>