FileUtils類上傳文件圖片


import java.io.File;
import java.util.Scanner;

import org.apache.commons.io.FileUtils;

public class UploadFile {

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String SourceLocation = sc.nextLine();
        String [] sources = SourceLocation.split("\\\\");
        int size = sources.length;
        String GoalLocation = new String("F:\\測試\\");
        String GoalFileName = sources[size-1];
        GoalLocation = GoalLocation + GoalFileName;
        File sourcefile = new File(SourceLocation);
        if(!checkUploadfileType(GoalFileName,new String[]{"png"}))
        {
        System.out.println("文件格式不對");
        return;
        }
        if(!checkUploadfileSize(sourcefile, 1024*1024))
            {
            System.out.println("文件太大");
            return;
            }
        
        try{
            File locationfile = new File(GoalLocation);
            FileUtils.copyFile(sourcefile, locationfile);
        }catch(Exception e){
            System.out.println("上傳失敗");
        }
    }
    public static boolean checkUploadfileSize(File file, int size){
        Boolean flag = true;
        if(file.length()>=size)
            flag = false;
        return flag;
    }
    public static boolean checkUploadfileType(String Filename,String[] types){
        Boolean flag = false;
        for(String type:types){
            if(Filename.toLowerCase().lastIndexOf(type)+type.length() == Filename.length()){
                flag = true;
            }
        }
        return flag;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM