O'Reilly cos上传组件的使用(3/3) - 重命名上传后的文件
upload.html:
<form method="post" action="cos_rename.jsp"
enctype="multipart/form-data">
文件1:<input type="file" name="file1" />
说明:<input type="text" name="file1_desc" />
<br />
文件2:<input type="file" name="file2" />
说明:<input type="text" name="file2_desc" />
<input type="submit" value="上传" />
</form>
enctype="multipart/form-data">
文件1:<input type="file" name="file1" />
说明:<input type="text" name="file1_desc" />
<br />
文件2:<input type="file" name="file2" />
说明:<input type="text" name="file2_desc" />
<input type="submit" value="上传" />
</form>
cos_rename.jsp:
<%@page contentType="text/html;charset=GBK"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="java.io.File,javax.servlet.ServletContext"%>
<%@page import="java.util.Enumeration,java.io.IOException"%>
<%@page import="com.oreilly.servlet.multipart.FileRenamePolicy"%>
<%@page import="java.text.SimpleDateFormat"%>
O'Reilly cos上传组件的使用(3/3) - 重命名上传后的文件<br />
<%!
/**
*文件重命名策略类:当前文件名后加上时间和日期,防上重名
*/
class MyFileRenamePolicy implements FileRenamePolicy{
public File rename(File file){
String newName = addInNameExt(file.getName() , getDateStr());
File result = new File(file.getParentFile() , newName);
return result;
}
//返回一个当前时间的字符串表示
private String getDateStr(){
String pattern ="yyyy-MM-dd_HH-mm-ss_SSS";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String dateStr = sdf.format(new java.util.Date());
return dateStr;
}
//分离完整文件名的文件名和后缀,并在中间加入字符串后返回
private String addInNameExt(String fullName , String add){
String name = "";//文件名
String ext = ""; //后缀
char point = '.';
int index = fullName.lastIndexOf(point);
if(index != -1){//如果有后缀
name = fullName.substring(0 , index);
ext = fullName.substring(index + 1);
}
else{
name = fullName;
}
return name + "_" + add + point + ext;
}
}
%>
<%
/*设置上传文件路径*/
ServletContext context = getServletContext();
String realPath = context.getRealPath("/");
String saveDir = realPath + "ch9\\fileupload\\upload\\";
//out.println(saveDir + "<br />");//输出实际路径
File dir = new File(saveDir);
if(!dir.exists())
dir.mkdirs();
int maxPostSize = 1*1024*1024;//1MB
String encoding = "GBK";
FileRenamePolicy rename = new MyFileRenamePolicy();
MultipartRequest multi = null;
try{
multi = new MultipartRequest(request ,
saveDir , maxPostSize , encoding , rename);
}catch(IOException e){
//上传大小超过maxPostSize就会出现IOException
out.println(e.getMessage());
return;
}
//至此文件已经上传完成!
//以下为获取上传文件的信息,并不是必需的。
%>
<br /><br />
<%
//获取文件信息
Enumeration fileNames = multi.getFileNames();
while(fileNames.hasMoreElements()){
//指表单中的name值
String name = (String)fileNames.nextElement();
String original = multi.getOriginalFileName(name);
if(original == null)continue; //只上传了一个文件时,另外的文件为null
String sysname = multi.getFilesystemName(name);
String type = multi.getContentType(name);
out.println("Request Name:" + name +"<br />");
out.println("getOriginalFileName:" + original +"<br />");
out.println("getFilesystemName:" + saveDir + sysname +"<br />");
out.println("ContentType:" + type +"<br /><br />");
}
%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="java.io.File,javax.servlet.ServletContext"%>
<%@page import="java.util.Enumeration,java.io.IOException"%>
<%@page import="com.oreilly.servlet.multipart.FileRenamePolicy"%>
<%@page import="java.text.SimpleDateFormat"%>
O'Reilly cos上传组件的使用(3/3) - 重命名上传后的文件<br />
<%!
/**
*文件重命名策略类:当前文件名后加上时间和日期,防上重名
*/
class MyFileRenamePolicy implements FileRenamePolicy{
public File rename(File file){
String newName = addInNameExt(file.getName() , getDateStr());
File result = new File(file.getParentFile() , newName);
return result;
}
//返回一个当前时间的字符串表示
private String getDateStr(){
String pattern ="yyyy-MM-dd_HH-mm-ss_SSS";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String dateStr = sdf.format(new java.util.Date());
return dateStr;
}
//分离完整文件名的文件名和后缀,并在中间加入字符串后返回
private String addInNameExt(String fullName , String add){
String name = "";//文件名
String ext = ""; //后缀
char point = '.';
int index = fullName.lastIndexOf(point);
if(index != -1){//如果有后缀
name = fullName.substring(0 , index);
ext = fullName.substring(index + 1);
}
else{
name = fullName;
}
return name + "_" + add + point + ext;
}
}
%>
<%
/*设置上传文件路径*/
ServletContext context = getServletContext();
String realPath = context.getRealPath("/");
String saveDir = realPath + "ch9\\fileupload\\upload\\";
//out.println(saveDir + "<br />");//输出实际路径
File dir = new File(saveDir);
if(!dir.exists())
dir.mkdirs();
int maxPostSize = 1*1024*1024;//1MB
String encoding = "GBK";
FileRenamePolicy rename = new MyFileRenamePolicy();
MultipartRequest multi = null;
try{
multi = new MultipartRequest(request ,
saveDir , maxPostSize , encoding , rename);
}catch(IOException e){
//上传大小超过maxPostSize就会出现IOException
out.println(e.getMessage());
return;
}
//至此文件已经上传完成!
//以下为获取上传文件的信息,并不是必需的。
%>
<br /><br />
<%
//获取文件信息
Enumeration fileNames = multi.getFileNames();
while(fileNames.hasMoreElements()){
//指表单中的name值
String name = (String)fileNames.nextElement();
String original = multi.getOriginalFileName(name);
if(original == null)continue; //只上传了一个文件时,另外的文件为null
String sysname = multi.getFilesystemName(name);
String type = multi.getContentType(name);
out.println("Request Name:" + name +"<br />");
out.println("getOriginalFileName:" + original +"<br />");
out.println("getFilesystemName:" + saveDir + sysname +"<br />");
out.println("ContentType:" + type +"<br /><br />");
}
%>