[Grails] Grails如何实现上传多个文件!~~有知道麻烦告诉下!谢谢了

snbco 2009-10-29
Grails如何实现上传多个文件!~~有知道麻烦告诉下!谢谢了
ssy8110 2009-11-03
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import groovy.xml.MarkupBuilder;

UploadFileProperties result = new UploadFileProperties();//自定义的一个对像
        def file = request.getFile(fieldName)
        if(file != null){
            def CommonsMultipartFile cmfile = (CommonsMultipartFile)file

            String oldFileName = cmfile.getOriginalFilename()
            //如果未获取到原文件名,则表示没有文件被上传
            if(oldFileName != null && oldFileName != ""){        
                //检查 exsistFileName 文件名是否为空,不为空则删除原有文件
                if(exsistFileName != null && exsistFileName != ""){
                    deleteFile(basePath,exsistFileName);
                }
               
                //获取文件名的后缀,生成新的文件名组成完整的文件名
                String  postfix = oldFileName.substring(oldFileName.lastIndexOf("."));
                def newFileName = UUID.randomUUID().toString() +  postfix;
   
                //为返回对象设置文件信息参数
                def path = savePath+"/"+newFileName;               
                result.fileSize = cmfile.getSize();
                result.fileName = newFileName
                result.originalFilename  = oldFileName
                result.mimeType = cmfile.contentType
                result.filePath = path
                result.postfix = postfix

                //创建文件并写入磁盘
                def newFile = new File(basePath+path)
                if(!newFile.getParentFile().exists()){
                    newFile.mkdirs();
                }
                file.transferTo(newFile)
            }
        }
写的一个测试
li4li5li6 2009-11-03
Grails通过Spring的MultipartHttpServletRequest接口来支持文件上传.
Global site tag (gtag.js) - Google Analytics