COS组件上传附件的代码样例

来源:互联网 发布:绅士文学总是网络异常 编辑:程序博客网 时间:2024/05/21 21:41

上传类的代码:

/**
     * 使用cos组件上传
   * 速度很快,最后选用该组件
   * 
@param request HttpServletRequest
     * 
@return String
     * 
@throws Exception
     
*/

    
public String uploadfiles(HttpServletRequest request) throws Exception {
        
//文件上传后,保存在c:/upload
        String saveDirectory = request.getRealPath(""+ "/upload";
        String name 
= "";
        
//上传文件的大小限制
        int maxPostSize = 10 * 10 * 1024 * 1024;
        
try {
            
//定义命名策略
            RenamePolicyCos cospolicy = new RenamePolicyCos();
            
//response的编码为"gbk",同时采用缺省的文件名冲突解决策略,实现上传
            MultipartRequest multi = new MultipartRequest(request,
                    saveDirectory, maxPostSize, 
"gbk", cospolicy);
            
//System.out.println("1:");
            
//输出反馈信息
            Enumeration files = multi.getFileNames();
            
while (files.hasMoreElements()) {
                name 
= name + "/" +
                       multi.getFilesystemName(
                               (String) files.nextElement());
            }

        }
 catch (IOException e) {
            
//超过最大文件限制
            System.out.print(e.toString());
        }

        
return name;
    }

  以下是命名策略类的代码:

import java.io.File;
import com.oreilly.servlet.multipart.FileRenamePolicy;
import java.util.Random;

/**
 * Cos组件的重命名策略
 *
 *
 
*/

public class RenamePolicyCos implements FileRenamePolicy {

    
/**
     * 重载该方法,命名规则为在文件名后面加上"_随即数"
     * 用于cos组件
     * 
@param file File
     * 
@return File
     
*/

    
public File rename(File file) {
        
//System.out.println(new String file.getName().getBytes("iso-8859-1"));
        file = new File(file.getParent(), getNewFileName(file.getName()));
        
return file;
    }


    
/**
     * 防止文件名重复,将文件重命名
     * 用于cos组件
     * 
@param fileName String
     * 
@param filerealpath String
     * 
@return String
     
*/

    
public String getNewFileName(String fileName) {
            String fType 
= getFiletype(fileName); //文件类型
            String ffrontname = getFilefrontname(fileName); //文件名前缀
            
//替换前缀名中包含的特殊字符,视具体情况而定
      ffrontname = ffrontname.replace(',','_');
            ffrontname 
= ffrontname.replace('#','_');
            ffrontname 
= ffrontname.replace('&','_');
            ffrontname 
= ffrontname.replace('%','');
            
//System.out.println(ffrontname);
            Random rand = new Random();
            String newFileName 
= ffrontname + "_" + rand.nextInt(1000+ "." +
                                 fType;

            
return newFileName;
    }


    
/**
     * 取文件名后缀
     * 用于cos组件
     * 
@param fileName String
     * 
@return String
     
*/

    
public static String getFiletype(String fileName) {
        String type 
= "";
        
if (fileName == null || fileName.equals("")) {
            
return type;
        }

        
int position = fileName.lastIndexOf(".");
        
if (position != -1{
            type 
= fileName.substring(position + 1, fileName.length());
        }

        
return type;
    }


    
/**
     * 取文件名前缀
     * 用于cos组件
     * 
@param fileName String
     * 
@return String
     
*/

    
public static String getFilefrontname(String fileName) {
        String name 
= "";
        
if (fileName == null || fileName.equals("")) {
            
return name;
        }

        
int position = fileName.lastIndexOf(".");
        
if (position != -1{
            name 
= fileName.substring(0, position);
        }

        String newname 
= "";
        newname 
= name.trim().replaceAll(" ","");
        newname 
= newname.replaceAll("%","_");
        newname 
= newname.replaceAll("/+","_");  // + 模式匹配规则表达式,需要用/转义
        return newname;
    }


}

原创粉丝点击