jsf 利用 fckeditor进行内容编辑和文件上传* If not specified the value of /UserFiles/ will be used.
jsf 利用 fckeditor进行内容编辑和文件上传
* If not specified the value of "/UserFiles/" will be used.<br>
* Also it retrieve all allowed and denied extensions to be handled.
*
*/
public void init() throws ServletException {
debug=(new Boolean(getInitParameter("debug"))).booleanValue();
if(debug) System.out.println("\r\n---- SimpleUploaderServlet initialization started ----");
baseDir=getInitParameter("baseDir");
enabled=(new Boolean(getInitParameter("enabled"))).booleanValue();
if(baseDir==null)
baseDir="/EpUserFiles/";
String realBaseDir=getServletContext().getRealPath(baseDir);
File baseFile=new File(realBaseDir);
if(!baseFile.exists()){
baseFile.mkdir();
}
allowedExtensions = new Hashtable(3);
deniedExtensions = new Hashtable(3);
allowedSize = new Hashtable(3);
allowedExtensions.put("File",stringToArrayList(getInitParameter("AllowedExtensionsFile")));
deniedExtensions.put("File",stringToArrayList(getInitParameter("DeniedExtensionsFile")));
allowedSize.put("File", new Long(getInitParameter("AllowedFileSize")).longValue());
allowedExtensions.put("Image",stringToArrayList(getInitParameter("AllowedExtensionsImage")));
deniedExtensions.put("Image",stringToArrayList(getInitParameter("DeniedExtensionsImage")));
allowedSize.put("Image", new Long(getInitParameter("AllowedImageSize")).longValue());
allowedExtensions.put("Flash",stringToArrayList(getInitParameter("AllowedExtensionsFlash")));
deniedExtensions.put("Flash",stringToArrayList(getInitParameter("DeniedExtensionsFlash")));
allowedSize.put("Flash", new Long(getInitParameter("AllowedFlashSize")).longValue());
if(debug) System.out.println("---- SimpleUploaderServlet initialization completed ----\r\n");
}
/**
* Manage the Upload requests.<br>
*
* The servlet accepts commands sent in the following format:<br>
* simpleUploader?Type=ResourceType<br><br>
* It store the file (renaming it in case a file with the same name exists) and then return an HTML file
* with a javascript command in it.
*
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (debug) System.out.println("--- BEGIN DOPOST ---");
response.setContentType("text/html; charset=UTF-8");
response.setHeader("Cache-Control","no-cache");
PrintWriter out = response.getWriter();
String typeStr=request.getParameter("Type");
String currentPath=baseDir+typeStr;
//在此为每个能上传图片的用片增加一个user目录,利用session来做
currentPath = currentPath+"/userName/";
String currentDirPath=getServletContext().getRealPath(currentPath);
currentPath=request.getContextPath()+currentPath;
File addUserNameFile=new File(currentDirPath);
if(!addUserNameFile.exists()){
addUserNameFile.mkdirs();
}
if (debug) System.out.println(currentDirPath);
String newName="";
String fileUrl="";
String errorMessage="";
if(enabled) {
DiskFileUpload upload = new DiskFileUpload();
upload.setHeaderEncoding("utf-8");
try {
List items = upload.parseRequest(request);
Map fields=new HashMap();
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
fields.put(item.getFieldName(),item.getString());
else
fields.put(item.getFieldName(),item);
}
FileItem uplFile=(FileItem)fields.get("NewFile");
String fileNameLong=uplFile.getName();
fileNameLong=fileNameLong.replace('\\','/');
String[] pathParts=fileNameLong.split("/");
String fileName=pathParts[pathParts.length-1];
long uploadFileSize = uplFile.getSize();
log.info("上传的文件大小为: ["+uploadFileSize+"]");
log.info("上传的文件名称为:["+fileName+"]");
String nameWithoutExt=getNameWithoutExtension(fileName);
String ext=getExtension(fileName);
File pathToSave=new File(currentDirPath,fileName);
//fileUrl=currentPath+"/"+fileName;
fileUrl=currentPath;
if(extIsAllowed(typeStr,ext,uploadFileSize)) {
//确保文件目录存在
if(!addUserNameFile.exists()){
addUserNameFile.mkdirs();
}
int counter=1;
newName = fileName;
while(pathToSave.exists()){
newName=nameWithoutExt+"("+counter+")"+"."+ext;
//fileUrl=currentPath+"/"+newName;
fileUrl=currentPath;
retVal="201";
pathToSave=new File(currentDirPath,newName);
counter++;
}
uplFile.write(pathToSave);
}
else {
//执行size过大提示信息
if(!retVal.equals("88")){
retVal="202";
errorMessage="";
if (debug) System.out.println("无效的文件类型: " + ext);
}else{
errorMessage="上传文件过大,该文件类型上传的大小允许在 "+typeAllowedSize+" 字节范围";
log.info("上传文件名称为:["+fileName+"]的文件大小为: ["+uploadFileSize+"] 超过 {"+typeAllowedSize+"}的允许范围!");
if (debug) System.out.println("上传文件过大: " + ext);
}
}
}catch (Exception ex) {
if (debug) ex.printStackTrace();
retVal="203";
}
}
else {
retVal="1";
errorMessage="无法上传文件. 请检查 WEB-INF/web.xml 文件!";
}
out.println("<script type="text/javascript">");
out.println("window.parent.OnUploadCompleted("+retVal+",'"+fileUrl+"','"+newName+"','"+errorMessage+"');");
out.println("</script>");
out.flush();
out.close();
if (debug) System.out.println("--- END DOPOST ---");
}
/*
* This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF bug #991489
*/
private static String getNameWithoutExtension(String fileName) {
return fileName.substring(0, fileName.lastIndexOf("."));
}
/*
* This method was fixed after Kris Barnhoorn (kurioskronic) submitted SF bug #991489
*/
private String getExtension(String fileName) {
return fileName.substring(fileName.lastIndexOf(".")+1);
}
/**
* Helper function to convert the configuration string to an ArrayList.
*/
private ArrayList stringToArrayList(String str) {
if(debug) System.out.println(str);
String[] strArr=str.split("\\|");
ArrayList tmp=new ArrayList();
if(str.length()>0) {
for(int i=0;i<strArr.length;++i) {
if(debug) System.out.println(i +" - "+strArr[i]);
tmp.add(strArr[i].toLowerCase());
}
}
return tmp;
}
/**
* Helper function to verify if a file extension is allowed or not allowed.
*/
private boolean extIsAllowed(String fileType, String ext,long size) {
ext=ext.toLowerCase();
ArrayList allowList=(ArrayList)allowedExtensions.get(fileType);
ArrayList denyList=(ArrayList)deniedExtensions.get(fileType);
long allowSize = ((Long)allowedSize.get(fileType)).longValue();
typeAllowedSize = allowSize;
if(allowList.size()==0){
if(denyList.contains(ext)){
return false;
}else{
if(size <= allowSize){
return true;
}
else{
retVal = "88"; //提示文件过大特殊符号
return false;
}
}
}
if(denyList.size()==0){
if(allowList.contains(ext)){
if(size <= allowSize){
return true;
}
else{
retVal = "88";
return false;
}
}
else{
return false;
}
}
return false;
}
}
2 楼 fangbiao23 2007-10-19 以上提供的只是个人经验所得,仅供参考 3 楼 mygol 2007-10-24 有个fck-faces可以作jsf应用,不过它是基于myfaces的,不知道有没有高人可以改成基于jsf-ri 4 楼 fangbiao23 2007-10-25 是的!!那個是可以用的,不過我沒用,聽說他有一些bug!在使用他時需要做一番修改!