struts2 文件上传的问题,怎样判断知道上传的是文件地址,文件是否存在,文件不是空的

在用sturts2做文件上传的时候要知道上传路径是不是路径,文件是否存在,文件的最小的问题!

multipartRequest.getFile("headimage").[color=red]getSize()[/color]

其它方式你可以自己.getSize()看看,我印象中都有的。

我把自己的helper类贴出来,下面贴的是test版本,你可以复制对应部分组成自己的helper类。

[code="java"]
package com.utils.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileHelper {
private static String TEST_PATH="E:/foldertest";
private static String TEST_FILE="E:/foldertest/filetest/filetest/testfile.txt";

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    File dirfile=new File(TEST_PATH);
    File file=new File(TEST_FILE);

    FileHelper.deleteFolderOrFile(dirfile);
    FileHelper.deleteFolderOrFile(file);

    FileHelper.createFolder(dirfile);

    FileHelper.createFolder(file);
}

public static Boolean createFolder(File dirfile){
    Boolean result=false;
    if( (!dirfile.exists()) && (!dirfile.isFile())){//如果dirfile不是一个文件【不是文件就是路径】并且不存在
        result= dirfile.mkdirs();
        System.out.println("make directory : "+dirfile.getPath()+" "+dirfile.getName());
    }
    return result;
}

public static Boolean createFile(String filePath,File file,String fileName){
    Boolean result=false;
    File tempFile=new File(filePath);
    FileHelper.createFolder(tempFile);

    try{

           FileInputStream fis = new FileInputStream(file);
           FileOutputStream   fos   =   new   FileOutputStream(filePath+fileName); 
           byte[]   buff   =   new   byte[1024]; 
           int   readed   =   -1; 
           while((readed   =   fis.read(buff))   >   0) 
               fos.write(buff,   0,   readed); 
           fis.close(); 
           fos.close();  
           }catch(Exception e){ 
               e.printStackTrace();
               result=false;
           } 

    return result;
}

public static Boolean deleteFolderOrFile(File dirfile){
    Boolean result=false;
    // if not exists return
    if(!dirfile.exists()){
        System.out.println("not exists: "+dirfile.getPath());
        return false;
    }
    try{
    // not a directory 
    if(!dirfile.isDirectory()){
        result=dirfile.delete();
        System.out.println("delete file: "+dirfile.getPath()+" "+dirfile.getName());
        return result;
    }
    File[] childs=dirfile.listFiles();
    if(childs==null||childs.length==0){
        result = dirfile.delete();
    }else{
        for (int i = 0; i < childs.length; i++) {
            File filePath = childs[i];
            if(filePath.exists()&&filePath.isFile()){// if exists and is a file then delete
                if(filePath.delete()){
                    System.out.println("delete file: "+filePath.getPath()+" "+filePath.getName());
                    result=true;
                }else{
                    result=false;
                    break;
                }
            }else if(filePath.exists()&&filePath.isDirectory()){// if exists and is a file then delete
                if(deleteFolderOrFile(filePath)){
                    result=true;
                }else{
                    result=false;
                    break;
                }
            }else{
                result=false;
                break;
            }
        }
        dirfile.delete();
        System.out.println("delete file: "+dirfile.getPath()+" "+dirfile.getName());
    }
    }catch (Exception e) {
        e.printStackTrace();
        result= false;
    }finally{

    }
    return result;

}

}

[/code]

[code="java"]if( (!dirfile.exists()) && (!dirfile.isFile()))[/code]

File jdk1.6 API:
[quote]
exists
public boolean exists()测试此抽象路径名表示的文件或目录是否存在。

返回:
当且仅当此抽象路径名表示的文件或目录存在时,返回 true;否则返回 false
抛出:
SecurityException - 如果存在安全管理器,且其 SecurityManager.checkRead(java.lang.String) 方法拒绝对文件或目录进行写访问


isDirectory
public boolean isDirectory()测试此抽象路径名表示的文件是否是一个目录。

返回:
[color=red]当且仅当此抽象路径名表示的文件存在且 是一个目录时[/color],返回 true;否则返回 false
抛出:
SecurityException - 如果存在安全管理器,且其 SecurityManager.checkRead(java.lang.String) 方法拒绝对文件进行读访问


isFile
public boolean isFile()测试此抽象路径名表示的文件是否是一个标准文件。如果该文件不是一个目录,并且满足其他与系统有关的标准,那么该文件是标准 文件。由 Java 应用程序创建的所有非目录文件一定是标准文件。

返回:
当且仅当此抽象路径名表示的文件[color=red]存在且 是一个标准文件[/color]时,返回 true;否则返回 false
抛出:
SecurityException - 如果存在安全管理器,且其 SecurityManager.checkRead(java.lang.String) 方法拒绝对文件进行读访问

[/quote]

注意红色部分,比如isFile(),有两个意思:1.是一个文件不是路径 2.存在。

方便你copy,再发一个引用的:
[quote]
package com.utils.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FileHelper {
private static String TEST_PATH="E:/foldertest";
private static String TEST_FILE="E:/foldertest/filetest/filetest/testfile.txt";

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    File dirfile=new File(TEST_PATH);
    File file=new File(TEST_FILE);

    FileHelper.deleteFolderOrFile(dirfile);
    FileHelper.deleteFolderOrFile(file);

    FileHelper.createFolder(dirfile);

    FileHelper.createFolder(file);
}

public static Boolean createFolder(File dirfile){
    Boolean result=false;
    if( (!dirfile.exists()) && (!dirfile.isFile())){//如果dirfile不是一个文件【不是文件就是路径】并且不存在
        result= dirfile.mkdirs();
        System.out.println("make directory : "+dirfile.getPath()+" "+dirfile.getName());
    }
    return result;
}

public static Boolean createFile(String filePath,File file,String fileName){
    Boolean result=false;
    File tempFile=new File(filePath);
    FileHelper.createFolder(tempFile);

    try{

           FileInputStream fis = new FileInputStream(file);
           FileOutputStream   fos   =   new   FileOutputStream(filePath+fileName); 
           byte[]   buff   =   new   byte[1024]; 
           int   readed   =   -1; 
           while((readed   =   fis.read(buff))   >   0) 
               fos.write(buff,   0,   readed); 
           fis.close(); 
           fos.close();  
           }catch(Exception e){ 
               e.printStackTrace();
               result=false;
           } 

    return result;
}

public static Boolean deleteFolderOrFile(File dirfile){
    Boolean result=false;
    // if not exists return
    if(!dirfile.exists()){
        System.out.println("not exists: "+dirfile.getPath());
        return false;
    }
    try{
    // not a directory 
    if(!dirfile.isDirectory()){
        result=dirfile.delete();
        System.out.println("delete file: "+dirfile.getPath()+" "+dirfile.getName());
        return result;
    }
    File[] childs=dirfile.listFiles();
    if(childs==null||childs.length==0){
        result = dirfile.delete();
    }else{
        for (int i = 0; i < childs.length; i++) {
            File filePath = childs[i];
            if(filePath.exists()&&filePath.isFile()){// if exists and is a file then delete
                if(filePath.delete()){
                    System.out.println("delete file: "+filePath.getPath()+" "+filePath.getName());
                    result=true;
                }else{
                    result=false;
                    break;
                }
            }else if(filePath.exists()&&filePath.isDirectory()){// if exists and is a file then delete
                if(deleteFolderOrFile(filePath)){
                    result=true;
                }else{
                    result=false;
                    break;
                }
            }else{
                result=false;
                break;
            }
        }
        dirfile.delete();
        System.out.println("delete file: "+dirfile.getPath()+" "+dirfile.getName());
    }
    }catch (Exception e) {
        e.printStackTrace();
        result= false;
    }finally{

    }
    return result;

}

}

[/quote]

至于size:看你是用那种方式,用commons-upload或者spring的multipart都封装了对文件大小的判断。

如果个是struts2的方式,在类里定义一个File file。会自动封装,通过Long size= file.length();可以获得文件大小,
[quote]
Long size= uploadpic.length();
if(size>50000){// if too larger
results.put("success",false);
results.put("info","size is too larger!");
return SUCCESS;
}
[/quote]

其它方式:
spring方式:节选部分代码
[code="java"]
CommonsMultipartResolver cmr = new CommonsMultipartResolver(request
.getSession().getServletContext());
cmr.setDefaultEncoding("utf-8");
if (cmr.isMultipart(request)) {
MultipartHttpServletRequest multipartRequest = cmr
.resolveMultipart(request);
String id = multipartRequest.getParameter("id");
if (null != id && !"".equals(id)) {
User user = userService.getUserById(new Long(id));// 根据id 查询会员
if color=red.getSize() > 0) {[/color]
String userImage = user.getPersonalInfo().getImage();
if ((!AppConstants.DEFAULT_TOUXIANG_MALE.equals(userImage))
&& (!AppConstants.DEFAULT_TOUXIANG_FEMALE
.equals(userImage))) {
// 删除原来的图片

                    FileUtils.delFileItem(userImage);
                    if (userImage.startsWith("/file")) {// 避免删除默认头像
                        FileUtils.delFileItem(userImage);
                        logger.debug("USER头像IMGAGE:" + userImage + "已删除");
                    }
                }

[/code]