照片筛选与上传功能

现在对于软件的体验是越来越深了,其实你给客户做一个软件并不是说你将客户要的功能实现了就可以了,并不是那么按部就班的实现功能即可,例如:添加用户信息(管理员进行添加的),需要进行用户照片的上传,除了照片上传的其他信息很好解决只需要用excel进行批量导入即可,但照片你总不可能让管理员一张一张进行上传吧,那种数据量谁还会来使用你的系统阿,所以系统并不是说实现了某个功能即可,而是要根据客户的实际情况,对功能进行适当的完善,以解决用户实际问题以此来增加用户体验;

最近给一个学院做的一个校友系统,需要将该学院的第一届毕业生到现在的所有校友信息进行录入,那个数据量确实很大,主要的问题就存在于照片的上传问题上,照片是从教务处那边拿过来的,但教务处那边并没有分学院来存储照片,而是整个学校的照片都放在一起的。好在照片的名字都是以学生的身份证就行命名的。

现在存在的一个就是必须将照片进行删选,并且对应学生存入数据库;

下面就是我实现的一个简单的删选图片方法并且将图片上传到指定服务器(数据库存储方面这里先不涉及)

    /**
     * 进行照片的筛选测试
     * @throws FileNotFoundException 
     * */
    public String select() throws FileNotFoundException{
        String temporaryPath = "e:\\photo";
        String newPath = ServletActionContext.getServletContext().getRealPath("/stuPhoto");;
        List<String> selectName = new ArrayList<String>();
        selectName.add("1");
        selectName.add("2");
        String[] prefixs = {".jpg", ".png",".gif"};
        GetSelectFileName(temporaryPath, newPath, selectName, prefixs);
        return SUCCESS;
    }
    
    /**
     * @param fileAbsolutePath 需要遍历的文件目录
     * @param newFilePath 放符合文件的路径
     * @param photoName 符合条件的图片名称
     * @param prefixs 符合遍历条件的后缀名
     * @throws FileNotFoundException 
     * 
     * 
     * */
    public void GetSelectFileName(String fileAbsolutePath, String newFilePath, List<String> selectName, String[] prefixs) throws FileNotFoundException
    {
         File file = new File(fileAbsolutePath);
         //文件内的文件
         File[] subFile = file.listFiles();
         //对所有的文件进行遍历
         for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++)
         {
             //判断是否为文件夹
             if (!subFile[iFileLength].isDirectory())
             {
                 String tempName = subFile[iFileLength].getName();
                 //先判断该文件名是否符合扩展名
                 for(int prefixLength = 0; prefixLength < prefixs.length; prefixLength++){
                     //如果符合指定的后缀
                     if(tempName.toLowerCase().endsWith(prefixs[prefixLength])){
                         String[] tempNameSplit = tempName.split("\\.");
                         //是否符合筛选的要求
                         for(String name: selectName){
                             if (name.equals(tempNameSplit[0]))
                             {
                                //将符合条件的文件放入另一个指定的文件夹
                                 Copy(fileAbsolutePath+"/"+tempName, newFilePath); 
                             }
                         }
                     }
                 }
             }
         }
    }
    /**
     * 用流的方式进行文件的复制
     * */
    public void Copy(String oldPath, String newPath)  
    {  
    try {  
        int bytesum = 0;  
        int byteread = 0;
        File newFile = new File(newPath);
        if(!newFile.exists()){
            newFile.mkdirs();
        }
        File oldfile = new File(oldPath);  
        if (oldfile.exists()) {  
            InputStream is = new FileInputStream(oldPath);  
            
            FileOutputStream fs = new FileOutputStream(newPath+"/"+oldfile.getName());  
            byte[] buffer = new byte[1444];  
            while ( (byteread = is.read(buffer)) != -1) {  
                bytesum += byteread;  
                System.out.println(bytesum);  
                fs.write(buffer, 0, byteread);  
            }  
            is.close();
            fs.close();
        }  
    }  

这样就基本实现了照片的删选和上传功能,大家可以改进下,改造成适合自己的方案。

posted on 2012-07-01 14:26  发表是最好的记忆  阅读(558)  评论(0编辑  收藏  举报