struts2 空文件上传解决

/**
	 * 文件上传
	 * @throws Exception
	 */
	protected List<File> uploadFile()throws Exception{
		//得到当前工程的目录,然后自己再定义下保存的路径   
        //打印路径看是否存在   
        System.out.println(realpath);   
        List<File> saveFiles = new ArrayList<File>();
        if(files != null){   
            File savedir = new File(realpath) ;   
            if(!savedir.exists()){   
                savedir.mkdirs() ;   
            }   
            for(int i=0;i<files.length;i++){   
            	String uuid = StringUtil.getUUID();
            	String fileName = filesFileName[i];
            	//fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
            	//log.error("shangchuan fileRealName 1==="+fileName);
            	fileName = StringUtil.urlencode(fileName);
            	//log.error("shangchuan fileRealName 2==="+fileName);
            	String srcFileName = "split"+uuid+"split"+fileName;
            	srcFileName = StringUtil.filterDangerString(srcFileName);
            	
            	
            	File file = files[i];
            	//如果文件为null(这种情况是因为文件大小为0)
            	if(!file.exists()){
            		 File savefile = new File(savedir,srcFileName) ;
            		 savefile.createNewFile();
            		 saveFiles.add(savefile);
            	}else{
                File savefile = new File(savedir,srcFileName) ;   
                FileUtils.copyFile(files[i], savefile) ;   
                saveFiles.add(savefile);
            	}
            }   
        }   
        return saveFiles;

	}

 

你可能感兴趣的:(struts2)